Skip to content

Instantly share code, notes, and snippets.

@kitoko552
Last active February 7, 2020 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kitoko552/ae5a31490a44ed98af27a99f5a1bb6e1 to your computer and use it in GitHub Desktop.
Save kitoko552/ae5a31490a44ed98af27a99f5a1bb6e1 to your computer and use it in GitHub Desktop.
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