Skip to content

Commit 395ce60

Browse files
新增轻量级路由框架FRouter
新增路由框架FRouter,让路由更好处理
1 parent 0fc7b6e commit 395ce60

File tree

2 files changed

+435
-0
lines changed

2 files changed

+435
-0
lines changed

FRouter/FRouter.dart

Lines changed: 336 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,336 @@
1+
import 'package:flutter/gestures.dart';
2+
import 'package:flutter/material.dart';
3+
4+
class FRouter extends StatefulWidget {
5+
// 是否是首页 默认为非首页
6+
bool isFirstPage;
7+
8+
// 是否显示AppBar
9+
bool isShowAppBar;
10+
11+
Widget child;
12+
String title;
13+
ThemeData theme;
14+
ThemeData darkTheme;
15+
Color color;
16+
GlobalKey<NavigatorState> navigatorKey;
17+
GenerateAppTitle onGenerateTitle;
18+
19+
// 路由集合 所有的路由跳转都放在这里集中管理
20+
Map<String, WidgetBuilder> routes;
21+
ThemeMode themeMode;
22+
23+
// 默认初始路由
24+
String initialRoute = '/';
25+
RouteFactory onGenerateRoute;
26+
RouteFactory onUnknownRoute;
27+
List<NavigatorObserver> navigatorObservers;
28+
TransitionBuilder builder;
29+
Locale locale;
30+
Iterable<LocalizationsDelegate<dynamic>> localizationsDelegates;
31+
LocaleListResolutionCallback localeListResolutionCallback;
32+
LocaleResolutionCallback localeResolutionCallback;
33+
Iterable<Locale> supportedLocales;
34+
bool debugShowMaterialGrid;
35+
bool showPerformanceOverlay;
36+
bool checkerboardRasterCacheImages;
37+
bool checkerboardOffscreenLayers;
38+
bool showSemanticsDebugger;
39+
bool debugShowCheckedModeBanner;
40+
41+
// AppBar有关
42+
Widget appBarleading;
43+
bool automaticallyImplyLeading;
44+
Widget appBarTitle;
45+
List<Widget> appBarActions;
46+
Widget appBarFlexibleSpace;
47+
PreferredSizeWidget appBarBottom;
48+
double appBarElevation;
49+
ShapeBorder appBarShape;
50+
Color appBarBackgroundColor;
51+
Brightness appBarBrightness;
52+
IconThemeData appBarIconTheme;
53+
IconThemeData appBarActionsIconTheme;
54+
TextTheme appBarTextTheme;
55+
bool appBarPrimary;
56+
bool appBarCenterTitle;
57+
double appBarTitleSpacing = NavigationToolbar.kMiddleSpacing;
58+
double appBarToolbarOpacity;
59+
double appBarBottomOpacity;
60+
61+
// Scaffod有关
62+
Widget body;
63+
Widget floatingActionButton;
64+
FloatingActionButtonLocation floatingActionButtonLocation;
65+
FloatingActionButtonAnimator floatingActionButtonAnimator;
66+
List<Widget> persistentFooterButtons;
67+
Widget drawer;
68+
Widget endDrawer;
69+
Color drawerScrimColor;
70+
double drawerEdgeDragWidth;
71+
DragStartBehavior drawerDragStartBehavior;
72+
Color backgroundColor;
73+
Widget bottomNavigationBar;
74+
Widget bottomSheet;
75+
bool resizeToAvoidBottomPadding;
76+
bool resizeToAvoidBottomInset;
77+
bool primary;
78+
79+
bool extendBody;
80+
81+
FRouter({
82+
Key key,
83+
this.isFirstPage = false,
84+
this.isShowAppBar = false,
85+
this.child,
86+
this.title = '',
87+
this.theme,
88+
this.darkTheme,
89+
this.themeMode = ThemeMode.system,
90+
this.color,
91+
// 路由有关
92+
this.navigatorKey,
93+
this.onGenerateTitle,
94+
this.routes = const <String, WidgetBuilder>{},
95+
this.initialRoute,
96+
this.onGenerateRoute,
97+
this.onUnknownRoute,
98+
this.navigatorObservers = const <NavigatorObserver>[],
99+
this.builder,
100+
// 本地化有关
101+
this.locale,
102+
this.localizationsDelegates,
103+
this.localeListResolutionCallback,
104+
this.localeResolutionCallback,
105+
this.supportedLocales = const <Locale>[Locale('en', 'US')],
106+
// debug有关
107+
this.debugShowMaterialGrid = false,
108+
this.showPerformanceOverlay = false,
109+
this.checkerboardRasterCacheImages = false,
110+
this.checkerboardOffscreenLayers = false,
111+
this.showSemanticsDebugger = false,
112+
this.debugShowCheckedModeBanner = true,
113+
114+
// AppBar有关
115+
this.appBarleading,
116+
this.automaticallyImplyLeading = true,
117+
this.appBarTitle,
118+
this.appBarActions,
119+
this.appBarFlexibleSpace,
120+
this.appBarBottom,
121+
this.appBarElevation,
122+
this.appBarShape,
123+
this.appBarBackgroundColor,
124+
this.appBarBrightness,
125+
this.appBarIconTheme,
126+
this.appBarActionsIconTheme,
127+
this.appBarTextTheme,
128+
this.appBarPrimary = true,
129+
this.appBarCenterTitle,
130+
this.appBarTitleSpacing = NavigationToolbar.kMiddleSpacing,
131+
this.appBarToolbarOpacity = 1.0,
132+
this.appBarBottomOpacity = 1.0,
133+
134+
// Scaffod有关
135+
this.floatingActionButton,
136+
this.floatingActionButtonLocation,
137+
this.floatingActionButtonAnimator,
138+
this.persistentFooterButtons,
139+
this.drawer,
140+
this.endDrawer,
141+
this.bottomNavigationBar,
142+
this.bottomSheet,
143+
this.backgroundColor,
144+
this.resizeToAvoidBottomPadding,
145+
this.resizeToAvoidBottomInset,
146+
this.primary = true,
147+
this.drawerDragStartBehavior = DragStartBehavior.start,
148+
this.extendBody = false,
149+
this.drawerScrimColor,
150+
this.drawerEdgeDragWidth,
151+
}) : assert(isFirstPage != null),
152+
assert(isShowAppBar != null),
153+
// MaterialApp 有关
154+
assert(routes != null),
155+
assert(navigatorObservers != null),
156+
assert(title != null),
157+
assert(debugShowMaterialGrid != null),
158+
assert(showPerformanceOverlay != null),
159+
assert(checkerboardRasterCacheImages != null),
160+
assert(checkerboardOffscreenLayers != null),
161+
assert(showSemanticsDebugger != null),
162+
assert(debugShowCheckedModeBanner != null),
163+
// appbar有关
164+
assert(automaticallyImplyLeading != null),
165+
assert(appBarElevation == null || appBarElevation >= 0.0),
166+
assert(appBarPrimary != null),
167+
assert(appBarTitleSpacing != null),
168+
assert(appBarToolbarOpacity != null),
169+
assert(appBarBottomOpacity != null),
170+
// Scaffold 有关
171+
assert(primary != null),
172+
assert(extendBody != null),
173+
assert(drawerDragStartBehavior != null),
174+
super(key: key);
175+
176+
@override
177+
FRouterState createState() => FRouterState();
178+
179+
Map<String, WidgetBuilder> datas = {
180+
'/pageone': (builder) {
181+
return PageOne('数据1');
182+
},
183+
'/pagetwo': (builder) => PageTwo('数据2'),
184+
'/pagethree': (builder) => PageThree('数据3'),
185+
};
186+
187+
/// 发送路由到新页面
188+
static sendRouterPage(BuildContext context, Widget page) {
189+
Navigator.of(context)
190+
.push(MaterialPageRoute(builder: (BuildContext context) {
191+
return page;
192+
}));
193+
}
194+
195+
/// 发送命名路由 这个需要配合 [routes] 属性使用
196+
static sendRouter(BuildContext context, String routeName) {
197+
Navigator.of(context).pushNamed(routeName);
198+
}
199+
200+
get getDatas => routes;
201+
202+
set setDatas(Map<String, WidgetBuilder> datas) => datas = routes;
203+
204+
/// 返回上个页面的路由 参数2:要传递给上个页面的数据
205+
static backPageRouter<T extends Object>(BuildContext context, {T backData}) {
206+
Navigator.of(context).pop(backData);
207+
}
208+
209+
/// 接收路由返回的数据 参数2:路由名称
210+
Future<T> receiverRouterData<T extends Object>(
211+
BuildContext context, String routeName) async {
212+
var data = await Navigator.of(context).pushNamed(routeName);
213+
return data;
214+
}
215+
}
216+
217+
class FRouterState extends State<FRouter> {
218+
@override
219+
Widget build(BuildContext context) {
220+
if (widget.isFirstPage == true) {
221+
return MaterialApp(
222+
home: widget.isShowAppBar == true
223+
? Scaffold(
224+
appBar: AppBar(
225+
key: widget.key,
226+
leading: widget.appBarleading,
227+
automaticallyImplyLeading: widget.automaticallyImplyLeading,
228+
title: widget.appBarTitle,
229+
actions: widget.appBarActions,
230+
flexibleSpace: widget.appBarFlexibleSpace,
231+
bottom: widget.appBarBottom,
232+
elevation: widget.appBarElevation,
233+
shape: widget.appBarShape,
234+
backgroundColor: widget.appBarBackgroundColor,
235+
brightness: widget.appBarBrightness,
236+
iconTheme: widget.appBarIconTheme,
237+
actionsIconTheme: widget.appBarActionsIconTheme,
238+
textTheme: widget.appBarTextTheme,
239+
primary: widget.appBarPrimary,
240+
centerTitle: widget.appBarCenterTitle,
241+
titleSpacing: widget.appBarTitleSpacing,
242+
toolbarOpacity: widget.appBarToolbarOpacity,
243+
bottomOpacity: widget.appBarBottomOpacity,
244+
),
245+
body: widget.body,
246+
floatingActionButton: widget.floatingActionButton,
247+
floatingActionButtonLocation:
248+
widget.floatingActionButtonLocation,
249+
floatingActionButtonAnimator:
250+
widget.floatingActionButtonAnimator,
251+
persistentFooterButtons: widget.persistentFooterButtons,
252+
drawer: widget.drawer,
253+
endDrawer: widget.endDrawer,
254+
bottomNavigationBar: widget.bottomNavigationBar,
255+
bottomSheet: widget.bottomSheet,
256+
backgroundColor: widget.backgroundColor,
257+
resizeToAvoidBottomPadding: widget.resizeToAvoidBottomPadding,
258+
resizeToAvoidBottomInset: widget.resizeToAvoidBottomInset,
259+
primary: widget.primary,
260+
drawerDragStartBehavior: widget.drawerDragStartBehavior,
261+
extendBody: widget.extendBody,
262+
drawerScrimColor: widget.drawerScrimColor,
263+
drawerEdgeDragWidth: widget.drawerEdgeDragWidth,
264+
)
265+
: widget.child,
266+
title: widget.title,
267+
theme: widget.theme,
268+
darkTheme: widget.darkTheme,
269+
themeMode: widget.themeMode,
270+
color: widget.color,
271+
navigatorKey: widget.navigatorKey,
272+
onGenerateTitle: widget.onGenerateTitle,
273+
routes: widget.routes,
274+
initialRoute: widget.initialRoute,
275+
onGenerateRoute: widget.onGenerateRoute,
276+
onUnknownRoute: widget.onUnknownRoute,
277+
navigatorObservers: widget.navigatorObservers,
278+
builder: widget.builder,
279+
locale: widget.locale,
280+
localizationsDelegates: widget.localizationsDelegates,
281+
localeListResolutionCallback: widget.localeListResolutionCallback,
282+
localeResolutionCallback: widget.localeResolutionCallback,
283+
supportedLocales: widget.supportedLocales,
284+
debugShowMaterialGrid: widget.debugShowMaterialGrid,
285+
showPerformanceOverlay: widget.showPerformanceOverlay,
286+
checkerboardRasterCacheImages: widget.checkerboardRasterCacheImages,
287+
checkerboardOffscreenLayers: widget.checkerboardOffscreenLayers,
288+
showSemanticsDebugger: widget.showSemanticsDebugger,
289+
debugShowCheckedModeBanner: widget.debugShowCheckedModeBanner,
290+
);
291+
} else {
292+
widget.isShowAppBar == true
293+
? Scaffold(
294+
appBar: AppBar(
295+
key: widget.key,
296+
leading: widget.appBarleading,
297+
automaticallyImplyLeading: widget.automaticallyImplyLeading,
298+
title: widget.appBarTitle,
299+
actions: widget.appBarActions,
300+
flexibleSpace: widget.appBarFlexibleSpace,
301+
bottom: widget.appBarBottom,
302+
elevation: widget.appBarElevation,
303+
shape: widget.appBarShape,
304+
backgroundColor: widget.appBarBackgroundColor,
305+
brightness: widget.appBarBrightness,
306+
iconTheme: widget.appBarIconTheme,
307+
actionsIconTheme: widget.appBarActionsIconTheme,
308+
textTheme: widget.appBarTextTheme,
309+
primary: widget.appBarPrimary,
310+
centerTitle: widget.appBarCenterTitle,
311+
titleSpacing: widget.appBarTitleSpacing,
312+
toolbarOpacity: widget.appBarToolbarOpacity,
313+
bottomOpacity: widget.appBarBottomOpacity,
314+
),
315+
body: widget.body,
316+
floatingActionButton: widget.floatingActionButton,
317+
floatingActionButtonLocation: widget.floatingActionButtonLocation,
318+
floatingActionButtonAnimator: widget.floatingActionButtonAnimator,
319+
persistentFooterButtons: widget.persistentFooterButtons,
320+
drawer: widget.drawer,
321+
endDrawer: widget.endDrawer,
322+
bottomNavigationBar: widget.bottomNavigationBar,
323+
bottomSheet: widget.bottomSheet,
324+
backgroundColor: widget.backgroundColor,
325+
resizeToAvoidBottomPadding: widget.resizeToAvoidBottomPadding,
326+
resizeToAvoidBottomInset: widget.resizeToAvoidBottomInset,
327+
primary: widget.primary,
328+
drawerDragStartBehavior: widget.drawerDragStartBehavior,
329+
extendBody: widget.extendBody,
330+
drawerScrimColor: widget.drawerScrimColor,
331+
drawerEdgeDragWidth: widget.drawerEdgeDragWidth,
332+
)
333+
: widget.child;
334+
}
335+
}
336+
}

0 commit comments

Comments
 (0)