/
main.dart
145 lines (142 loc) 路 5 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
import 'package:flutter/material.dart';
import 'package:r_router/r_router.dart';
import 'package:example/src/page/page_five.dart';
import 'package:example/src/page/page_four.dart';
import 'src/page/my_page.dart';
import 'src/page/page_one.dart';
import 'src/page/page_three.dart';
import 'src/page/page_two.dart';
void main() {
initRouter();
runApp(MyApp());
}
void initRouter() {
// first setting
RRouter.setPathStrategy(true)
.setErrorPage(ErrorPageWrapper(
error: (BuildContext context,
FlutterErrorDetails flutterErrorDetails) =>
Center(
child: Text(
'Exception Page (${flutterErrorDetails.exceptionAsString()})',
),
),
notFound: (BuildContext context, Context ctx) => Material(
child: Center(
child: Text('Page Not found:${ctx.path}'),
),
)))
.addRoute(NavigatorRoute(
'/',
(ctx) => MyHomePage(
title: 'My Home',
)))
.addRoute(NavigatorRoute('/one', (ctx) => PageOne()))
.addRoute(NavigatorRoute(
'/two',
(ctx) => PageTwo(
param: ctx.body != null ? ctx.body['param'] : '',
)))
.addRoute(NavigatorRoute('/three', (ctx) => PageThree(),
defaultPageTransaction: CupertinoPageTransitionsBuilder()))
.addRoute(NavigatorRoute('/four', (ctx) => PageFour(),
defaultPageTransaction: ZoomPageTransitionsBuilder()))
.addRoute(NavigatorRoute('/five', (ctx) => PageFive()))
.addRoute(NavigatorRoute('/five/:id', (ctx) => PageFive()))
.addRoute(NavigatorRoute('/print', (ctx) async {
return Future.value('璋冪敤鍑芥暟');
}, responseProcessor: (c, p) async {
print(p);
return 'hello';
}))
.addRoute(NavigatorRoute('/showDialog', (ctx) async {
return null;
}, responseProcessor: (c, p) async {
await RRouter.showDialog(
routeSettings: RouteSettings(name: c.path, arguments: c.body),
builder: (context) => AlertDialog(
title: Text('title'),
content: Text('content'),
actions: [
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('纭畾')),
],
));
return c.isDirectly == true ? Redirect(path: '/') : null;
}))
.setPageBuilder((ctx, builder, pageTransitionsBuilder) =>
CustomPage<dynamic>(
child: Builder(
builder: (BuildContext context) => builder.call(context)),
buildCustomRoute:
(BuildContext context, CustomPage<dynamic> page) =>
PageBasedCustomPageRoute(
page: page,
pageTransitionsBuilder: pageTransitionsBuilder),
key: ValueKey(ctx.at.microsecondsSinceEpoch),
name: ctx.path,
arguments: ctx.toJson(),
transitionDuration: Duration(milliseconds: 100),
restorationId: ctx.path))
.setPopHome(() {
return Future.value(true);
});
// or
// RRouter.addRoutes([
// NavigatorRoute('/', (ctx) => MyHomePage(title: 'Flutter Demo Home Page')),
// NavigatorRoute('/one', (ctx) => PageOne()),
// NavigatorRoute(
// '/two',
// (ctx) => PageTwo(
// param: ctx?.body != null ? ctx.body['param'] : '',
// )),
// NavigatorRoute('/three', (ctx) => PageThree(),
// defaultPageTransaction: CupertinoPageTransitionsBuilder()),
// NavigatorRoute('/four', (ctx) => PageFour(),
// defaultPageTransaction: ZoomPageTransitionsBuilder()),
// NavigatorRoute('/five', (ctx) => PageFive()),
// NavigatorRoute('/five/:id', (ctx) => PageFive()),
// ]).addInterceptor((ctx) async {
// if (ctx.path == '/other') {
// RRouter.navigateTo('/five', body: ctx.body);
// return true;
// }
// return false;
// }).addInterceptor((ctx) async {
// if (ctx.path == '/two') {
// RRouter.navigateTo('/one', body: ctx.body);
// return true;
// }
// return false;
// });
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
//Navigator1.0
// return MaterialApp(
// title: 'Flutter Demo',
// theme: ThemeData(
// primarySwatch: Colors.blue,
// ),
// navigatorObservers: [
// RRouter.observer,
// ],
// home: MyHomePage(title: 'Flutter Demo Home Page'),
// );
//Navigator2.0
return MaterialApp.router(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
routerDelegate: RRouter.delegate,
routeInformationParser: RRouter.informationParser,
);
}
}
// use navigate
// RRouter.navigateTo('/three', arguments: {'pageThree': 'hello world!'});