/
main.dart
170 lines (156 loc) · 5.72 KB
/
main.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import 'package:dynamic_routes/dynamic_routes/mixins/initiator.dart';
import 'package:example/pages/mixed_page/mixed_page.dart';
import 'package:example/pages/participator_page.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Dynamic Routes Test',
theme: ThemeData(
primarySwatch: Colors.pink,
),
// One might argue: "What's new with this? We can already do this using
// the normal Navigator class".
//
// Well, that's right, but what you can't do is that do exactly this, but
// deciding which page to be shown
//
// or which place to be swapped at runtime, all from just one place without
// the pages even knowing where they are going,
// the only thing they do is pushing the next thing in the array.
home: MyHomePage(title: 'Dynamic Routes Test', pageWidgets: [
const ParticipatorPage(title: "Page 1"),
const ParticipatorPage(title: "Page 2"),
// A page that can both continue the flow and branch off into a new flow.
// In real applications, you'd of course use route predicates to pop back
// to something.
//
// For simplicity's sake, we'll just do pop multiple times for this example.
MixedPage(
// This isSubSubFlow has nothing to do with the library, it's just that
// I'm too lazy to change the title of subsubflow pages.
isSubSubFlow: false,
subFlowSet1: const [
ParticipatorPage(title: "SubFlow 1 Sub page 1"),
ParticipatorPage(title: "SubFlow 1 Sub page 2"),
ParticipatorPage(title: "SubFlow 1 Sub page 3"),
],
subFlowSet1Callback: (context) {
Navigator.of(context).pop();
Navigator.of(context).pop();
Navigator.of(context).pop();
},
subFlowSet2: [
const ParticipatorPage(title: "SubFlow 1 Sub page 1"),
const ParticipatorPage(title: "SubFlow 1 Sub page 2"),
const ParticipatorPage(title: "SubFlow 1 Sub page 3"),
// A sub flow within sub flow....sub-ception!
MixedPage(
// This isSubSubFlow has nothing to do with the library, it's
// just that I'm too lazy to change the title of subsubflow pages.
isSubSubFlow: true,
subFlowSet1: const [
ParticipatorPage(title: "SubSubflow 1 Sub Page 1"),
ParticipatorPage(title: "SubSubflow 1 Sub Page 2"),
],
subFlowSet1Callback: (context) {
Navigator.of(context).pop();
Navigator.of(context).pop();
},
subFlowSet2: const [
ParticipatorPage(title: "SubSubflow 2 Sub page 1"),
ParticipatorPage(title: "SubSubflow 2 Sub page 2"),
],
subFlowSet2Callback: (context) {
Navigator.of(context).pop();
Navigator.of(context).pop();
}),
const ParticipatorPage(title: "SubFlow 1 Sub page 5"),
],
subFlowSet2Callback: (context) {
Navigator.of(context).pop();
Navigator.of(context).pop();
Navigator.of(context).pop();
Navigator.of(context).pop();
Navigator.of(context).pop();
},
),
const ParticipatorPage(title: "Page 4"),
const ParticipatorPage(title: "Page 5"),
const ParticipatorPage(title: "Page 6"),
]),
);
}
}
class MyHomePage extends StatefulWidget {
final List<Widget> pageWidgets;
const MyHomePage({
Key? key,
required this.title,
required this.pageWidgets,
}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> with DynamicRoutesInitiator {
late List<Widget> _widgets = widget.pageWidgets;
@override
void dispose() {
dynamicRoutesInitiator.dispose();
super.dispose();
}
@override
void initState() {
dynamicRoutesInitiator.setCache(0);
super.initState();
}
@override
Widget build(BuildContext context) {
final value = dynamicRoutesInitiator.getCache();
return Scaffold(
floatingActionButton: Column(
crossAxisAlignment: CrossAxisAlignment.end,
mainAxisSize: MainAxisSize.min,
children: [
ElevatedButton(
child: const Text("Shuffle page order"),
onPressed: () {
final newWidgets = [..._widgets]..shuffle();
_widgets = newWidgets;
},
),
ElevatedButton(
onPressed: () =>
setState(() => dynamicRoutesInitiator.setCache(value + 1)),
child: Text("Increment cached value: $value")),
],
),
appBar: AppBar(
title: Text(widget.title),
),
body: const Center(child: Text("Dynamic Routes test")),
bottomNavigationBar: Padding(
padding: const EdgeInsets.all(16),
child: TextButton(
child: const Text("Enter flow"),
onPressed: () {
dynamicRoutesInitiator.initializeRoutes(_widgets,
lastPageCallback: (newContext) {
dynamicRoutesInitiator.popUntilInitiatorPage(context);
});
dynamicRoutesInitiator.pushFirst(context).then((_) {
// Call setState to refresh the displayed cached value.
setState(() {});
});
},
),
),
);
}
}