main.dart 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import 'package:flutter/material.dart';
  2. import 'package:async_redux/async_redux.dart';
  3. class GlobalState {
  4. String name;
  5. GlobalState({
  6. @required this.name,
  7. });
  8. }
  9. class NameSetAction extends ReduxAction<GlobalState> {
  10. final String newName;
  11. NameSetAction({@required this.newName}) : assert(newName != null);
  12. @override
  13. GlobalState reduce() {
  14. state.name = newName;
  15. return state;
  16. }
  17. }
  18. GlobalState initialState = new GlobalState(
  19. name: '',
  20. );
  21. var store = Store<GlobalState>(initialState: initialState);
  22. void main() {
  23. runApp(Gmus(store: store));
  24. }
  25. class Gmus extends StatelessWidget {
  26. final Store<GlobalState> store;
  27. Gmus({
  28. Key key,
  29. @required this.store,
  30. }) : super(key: key);
  31. @override
  32. Widget build(BuildContext context) => StoreProvider<GlobalState>(
  33. store: this.store,
  34. child: MaterialApp(
  35. title: 'gmus-mobile',
  36. home: GmusConnector(),
  37. ),
  38. );
  39. }
  40. class GmusConnector extends StatelessWidget {
  41. GmusConnector({Key key}) : super(key: key);
  42. @override
  43. Widget build(BuildContext context) {
  44. return StoreConnector<GlobalState, ViewModel>(
  45. vm: () => Factory(this),
  46. builder: (BuildContext context, ViewModel vm) => GmusHome(
  47. state: vm.state,
  48. onSetName: vm.onSetName,
  49. ),
  50. );
  51. }
  52. }
  53. class Factory extends VmFactory<GlobalState, GmusConnector> {
  54. Factory(widget) : super(widget);
  55. @override
  56. ViewModel fromStore() => ViewModel(
  57. state: state,
  58. onSetName: (String newName) => dispatch(NameSetAction(newName: newName)),
  59. );
  60. }
  61. class ViewModel extends Vm {
  62. final GlobalState state;
  63. final Function(String) onSetName;
  64. ViewModel({
  65. @required this.state,
  66. @required this.onSetName,
  67. }) : super(equals: [state]);
  68. }
  69. class GmusHome extends StatelessWidget {
  70. final GlobalState state;
  71. final Function(String) onSetName;
  72. GmusHome({
  73. Key key,
  74. this.state,
  75. this.onSetName,
  76. }) : super(key: key);
  77. @override
  78. Widget build(BuildContext context) {
  79. return Scaffold(
  80. appBar: AppBar(
  81. title: Text('gmus'),
  82. ),
  83. body: Center(
  84. child: Text("name: ${this.state.name}"),
  85. ),
  86. );
  87. }
  88. }