Last active
February 7, 2020 07:50
-
-
Save kitoko552/ae5a31490a44ed98af27a99f5a1bb6e1 to your computer and use it in GitHub Desktop.
This file contains 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
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: StoreProvider( | |
store: Store<AppState>( | |
reducer, | |
initialState: AppState.init(), | |
), | |
child: const MyHomePage(), | |
), | |
); | |
} | |
} | |
class ViewModel { | |
ViewModel({ | |
this.state, | |
this.increment, | |
}); | |
final AppState state; | |
final VoidCallback increment; | |
@override | |
bool operator ==(Object other) => | |
identical(this, other) || | |
other is ViewModel && | |
runtimeType == other.runtimeType && | |
state == other.state; | |
@override | |
int get hashCode => state.hashCode; | |
} | |
class MyHomePage extends StatelessWidget { | |
const MyHomePage({Key key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return StoreConnector<AppState, ViewModel>( | |
distinct: true, | |
converter: (store) { | |
return ViewModel( | |
state: store.state, | |
increment: () { | |
store.dispatch(UpdateCount(store.state.count + 1)); | |
}, | |
); | |
}, | |
builder: (context, viewModel) { | |
return MyHomePageContent(viewModel); | |
}, | |
); | |
} | |
} | |
class MyHomePageContent extends StatelessWidget { | |
const MyHomePageContent(this.viewModel); | |
final ViewModel viewModel; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Redux with ViewModel'), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text('You have pushed the button this many times:'), | |
Text( | |
'${viewModel.state.count}', | |
style: Theme.of(context).textTheme.display1, | |
), | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: viewModel.increment, | |
tooltip: 'Increment', | |
child: Icon(Icons.add), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment