Created
May 28, 2020 08:48
-
-
Save hayabusabusa/d19419f1c6d101271c05b5c9af921dbf to your computer and use it in GitHub Desktop.
Bloc パターンで State を更新する際に何が比較されているのか確認するサンプル。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:bloc/bloc.dart'; | |
import 'package:equatable/equatable.dart'; | |
import 'package:flutter_bloc/flutter_bloc.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
visualDensity: VisualDensity.adaptivePlatformDensity, | |
), | |
home: MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
// MARK: - Entity | |
class CountEntity extends Equatable { | |
final int count; | |
final DateTime time; | |
const CountEntity(this.count, this.time); | |
// NOTE: やっぱり props に何も指定しなければ | |
// 当たり前だが、型のみが評価されてしまうため、メンバーが変更されても考慮されない. | |
@override | |
List<Object> get props => []; | |
//List<Object> get props => [count, time]; | |
} | |
// MARK: - Event | |
abstract class CounterEvent extends Equatable { | |
const CounterEvent(); | |
@override | |
List<Object> get props => []; | |
} | |
class CounterIncrement extends CounterEvent { | |
const CounterIncrement(); | |
@override | |
List<Object> get props => []; | |
} | |
// MARK: - State | |
abstract class CounterState extends Equatable { | |
const CounterState(); | |
@override | |
List<Object> get props => []; | |
} | |
class CounterInitState extends CounterState { | |
final CountEntity entity; | |
const CounterInitState(this.entity); | |
@override | |
List<Object> get props => [entity]; | |
} | |
class CounterUpdatedState extends CounterState { | |
final CountEntity entity; | |
const CounterUpdatedState(this.entity); | |
@override | |
List<Object> get props => [entity]; | |
} | |
// MARK: - BLoC | |
class CounterBloc extends Bloc<CounterEvent, CounterState> { | |
CounterBloc(); | |
@override | |
CounterState get initialState => CounterInitState(CountEntity(0, DateTime.now())); | |
@override | |
Stream<CounterState> mapEventToState(CounterEvent event) async* { | |
if (event is CounterIncrement) { | |
if (state is CounterInitState) { | |
final _state = state as CounterInitState; | |
yield CounterUpdatedState(CountEntity(_state.entity.count + 1, DateTime.now())); | |
} else if (state is CounterUpdatedState) { | |
final _state = state as CounterUpdatedState; | |
yield CounterUpdatedState(CountEntity(_state.entity.count + 1, DateTime.now())); | |
} | |
} | |
} | |
} | |
// MARK: - Screen | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key, this.title}) : super(key: key); | |
final String title; | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
final CounterBloc _bloc = CounterBloc(); | |
@override | |
void dispose() { | |
_bloc.close(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: BlocBuilder( | |
bloc: _bloc, | |
builder: (context, state) { | |
if (state is CounterInitState) { | |
return Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text('You have pushed the button this many times:',), | |
Text( | |
'${state.entity.count}', | |
style: Theme.of(context).textTheme.headline4, | |
), | |
], | |
), | |
); | |
} | |
if (state is CounterUpdatedState) { | |
return Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text( | |
'${state.entity.count}', | |
style: Theme.of(context).textTheme.headline4, | |
), | |
Text( | |
'last updated ${state.entity.time}', | |
style: TextStyle(color: Colors.grey), | |
), | |
], | |
), | |
); | |
} | |
return Center( | |
child: Text('Unkown state.'), | |
); | |
}, | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () => _bloc.add(CounterIncrement()), | |
tooltip: 'Increment', | |
child: Icon(Icons.add), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment