Skip to content

Instantly share code, notes, and snippets.

@kitoko552
Last active February 10, 2020 04:00
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/d7c7fa87fb0fb008d5fb798b34c05b9b to your computer and use it in GitHub Desktop.
Save kitoko552/d7c7fa87fb0fb008d5fb798b34c05b9b to your computer and use it in GitHub Desktop.
class Handler {
Handler({
this.increment,
});
final VoidCallback increment;
}
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: Provider(
create: (context) {
return Handler(
increment: () {
final store = StoreProvider.of<AppState>(context);
store.dispatch(UpdateCount(store.state.count + 1));
},
);
},
child: const MyHomePage(),
),
),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
final handler = Provider.of<Handler>(context, listen: false);
return Scaffold(
appBar: AppBar(
title: Text('Demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('You have pushed the button this many times:'),
StoreConnector<AppState, int>(
distinct: true,
converter: (store) => store.state.count,
builder: (context, count) {
return Text(
'$count',
style: Theme.of(context).textTheme.display1,
);
},
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: handler.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