-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathmain_navigate.dart
104 lines (87 loc) · 2.68 KB
/
main_navigate.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import 'package:async_redux/async_redux.dart';
import 'package:flutter/material.dart';
late Store<AppState> store;
final navigatorKey = GlobalKey<NavigatorState>();
void main() async {
NavigateAction.setNavigatorKey(navigatorKey);
store = Store<AppState>(initialState: AppState());
runApp(MyApp());
}
final routes = {
'/': (BuildContext context) => Page1Connector(),
"/myRoute": (BuildContext context) => Page2Connector(),
};
class AppState {}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StoreProvider<AppState>(
store: store,
child: MaterialApp(
routes: routes,
navigatorKey: navigatorKey,
),
);
}
}
class Page extends StatelessWidget {
final Color? color;
final String? text;
final VoidCallback onChangePage;
Page({this.color, this.text, required this.onChangePage});
@override
Widget build(BuildContext context) => ElevatedButton(
style: ElevatedButton.styleFrom(backgroundColor: color),
child: Text(text!),
onPressed: onChangePage,
);
}
class Page1Connector extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StoreConnector<AppState, ViewModel1>(
vm: () => Factory1(),
builder: (BuildContext context, ViewModel1 vm) => Page(
color: Colors.red,
text: "Tap me to push a new route!",
onChangePage: vm.onChangePage,
),
);
}
}
/// Factory that creates a view-model for the StoreConnector.
class Factory1 extends VmFactory<AppState, Page1Connector, ViewModel1> {
@override
ViewModel1 fromStore() =>
ViewModel1(onChangePage: () => dispatch(NavigateAction.pushNamed("/myRoute")));
}
/// The view-model holds the part of the Store state the dumb-widget needs.
class ViewModel1 extends Vm {
final VoidCallback onChangePage;
ViewModel1({required this.onChangePage});
}
class Page2Connector extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StoreConnector<AppState, ViewModel2>(
vm: () => Factory2(),
builder: (BuildContext context, ViewModel2 vm) => Page(
color: Colors.blue,
text: "Tap me to pop this route!",
onChangePage: vm.onChangePage,
),
);
}
}
/// Factory that creates a view-model for the StoreConnector.
class Factory2 extends VmFactory<AppState, Page1Connector, ViewModel2> {
@override
ViewModel2 fromStore() => ViewModel2(
onChangePage: () => dispatch(NavigateAction.pop()),
);
}
/// The view-model holds the part of the Store state the dumb-widget needs.
class ViewModel2 extends Vm {
final VoidCallback onChangePage;
ViewModel2({required this.onChangePage});
}