Line data Source code
1 : import 'package:flutter/material.dart';
2 : import 'package:get/get.dart';
3 : import 'package:get/src/routes/get_route.dart';
4 : import 'package:get/src/routes/utils/parse_arguments.dart';
5 : import 'root_controller.dart';
6 : import 'smart_management.dart';
7 :
8 : class GetMaterialApp extends StatelessWidget {
9 4 : const GetMaterialApp({
10 : Key key,
11 : this.navigatorKey,
12 : this.home,
13 : this.routes = const <String, WidgetBuilder>{},
14 : this.initialRoute,
15 : this.onGenerateRoute,
16 : this.onGenerateInitialRoutes,
17 : this.onUnknownRoute,
18 : this.navigatorObservers = const <NavigatorObserver>[],
19 : this.builder,
20 : this.title = '',
21 : this.onGenerateTitle,
22 : this.color,
23 : this.onInit,
24 : this.onDispose,
25 : this.theme,
26 : this.darkTheme,
27 : this.themeMode = ThemeMode.system,
28 : this.locale,
29 : this.localizationsDelegates,
30 : this.localeListResolutionCallback,
31 : this.localeResolutionCallback,
32 : this.supportedLocales = const <Locale>[Locale('en', 'US')],
33 : this.debugShowMaterialGrid = false,
34 : this.showPerformanceOverlay = false,
35 : this.checkerboardRasterCacheImages = false,
36 : this.checkerboardOffscreenLayers = false,
37 : this.showSemanticsDebugger = false,
38 : this.debugShowCheckedModeBanner = true,
39 : this.shortcuts,
40 : this.smartManagement = SmartManagement.full,
41 : this.initialBinding,
42 : this.routingCallback,
43 : this.defaultTransition,
44 : // this.actions,
45 : this.opaqueRoute,
46 : this.enableLog,
47 : this.popGesture,
48 : this.transitionDuration,
49 : this.defaultGlobalState,
50 : this.namedRoutes,
51 : this.unknownRoute,
52 1 : }) : assert(routes != null),
53 1 : assert(navigatorObservers != null),
54 1 : assert(title != null),
55 2 : assert(debugShowMaterialGrid != null),
56 1 : assert(showPerformanceOverlay != null),
57 1 : assert(checkerboardRasterCacheImages != null),
58 1 : assert(checkerboardOffscreenLayers != null),
59 1 : assert(showSemanticsDebugger != null),
60 1 : assert(debugShowCheckedModeBanner != null),
61 3 : super(key: key);
62 :
63 : final GlobalKey<NavigatorState> navigatorKey;
64 : final Widget home;
65 : final Map<String, WidgetBuilder> routes;
66 : final String initialRoute;
67 : final RouteFactory onGenerateRoute;
68 : final InitialRouteListFactory onGenerateInitialRoutes;
69 : final RouteFactory onUnknownRoute;
70 : final List<NavigatorObserver> navigatorObservers;
71 : final TransitionBuilder builder;
72 : final String title;
73 : final GenerateAppTitle onGenerateTitle;
74 : final ThemeData theme;
75 : final ThemeData darkTheme;
76 : final ThemeMode themeMode;
77 : final Color color;
78 : final Locale locale;
79 : final Iterable<LocalizationsDelegate<dynamic>> localizationsDelegates;
80 : final LocaleListResolutionCallback localeListResolutionCallback;
81 : final LocaleResolutionCallback localeResolutionCallback;
82 : final Iterable<Locale> supportedLocales;
83 : final bool showPerformanceOverlay;
84 : final bool checkerboardRasterCacheImages;
85 : final bool checkerboardOffscreenLayers;
86 : final bool showSemanticsDebugger;
87 : final bool debugShowCheckedModeBanner;
88 : final Map<LogicalKeySet, Intent> shortcuts;
89 : // final Map<LocalKey, ActionFactory> actions;
90 : final bool debugShowMaterialGrid;
91 : final Function(Routing) routingCallback;
92 : final Transition defaultTransition;
93 : final bool opaqueRoute;
94 : final VoidCallback onInit;
95 : final VoidCallback onDispose;
96 : final bool enableLog;
97 : final bool popGesture;
98 : final SmartManagement smartManagement;
99 : final Bindings initialBinding;
100 : final Duration transitionDuration;
101 : final bool defaultGlobalState;
102 : final Map<String, GetRoute> namedRoutes;
103 : final GetRoute unknownRoute;
104 :
105 1 : Route<dynamic> namedRoutesGenerate(RouteSettings settings) {
106 1 : Get.setSettings(settings);
107 :
108 : /// onGenerateRoute to FlutterWeb is Broken on Dev/Master. This is a temporary
109 : /// workaround until they fix it, because the problem is with the 'Flutter engine',
110 : /// which changes the initial route for an empty String, not the main Flutter,
111 : /// so only Team can fix it.
112 4 : var parsedString = Get.getController.parse.split(
113 3 : (settings.name == '' || settings.name == null)
114 0 : ? (initialRoute ?? '/')
115 1 : : settings.name);
116 :
117 : if (parsedString == null) {
118 0 : parsedString = AppRouteMatch();
119 0 : parsedString.route = settings.name;
120 : }
121 :
122 1 : String settingsName = parsedString.route;
123 1 : Map<String, GetRoute> newNamedRoutes = {};
124 :
125 3 : namedRoutes.forEach((key, value) {
126 5 : String newName = Get.getController.parse.split(key).route;
127 2 : newNamedRoutes.addAll({newName: value});
128 : });
129 :
130 1 : if (newNamedRoutes.containsKey(settingsName)) {
131 2 : Get.setParameter(parsedString.parameters);
132 :
133 1 : return GetRouteBase(
134 2 : page: newNamedRoutes[settingsName].page,
135 2 : title: newNamedRoutes[settingsName].title,
136 1 : parameter: parsedString.parameters,
137 : settings:
138 3 : RouteSettings(name: settings.name, arguments: settings.arguments),
139 2 : maintainState: newNamedRoutes[settingsName].maintainState,
140 2 : curve: newNamedRoutes[settingsName].curve,
141 2 : alignment: newNamedRoutes[settingsName].alignment,
142 2 : opaque: newNamedRoutes[settingsName].opaque,
143 2 : binding: newNamedRoutes[settingsName].binding,
144 1 : transitionDuration: (transitionDuration == null
145 2 : ? newNamedRoutes[settingsName].transitionDuration
146 0 : : transitionDuration),
147 2 : transition: newNamedRoutes[settingsName].transition,
148 2 : popGesture: newNamedRoutes[settingsName].popGesture,
149 2 : fullscreenDialog: newNamedRoutes[settingsName].fullscreenDialog,
150 : );
151 : } else {
152 0 : return ((unknownRoute == null
153 0 : ? GetRouteBase(
154 0 : page: Scaffold(
155 0 : body: Center(
156 0 : child: Text("Route not found :("),
157 : ),
158 : ))
159 0 : : GetRouteBase(
160 0 : page: unknownRoute.page,
161 0 : title: unknownRoute.title,
162 0 : settings: unknownRoute.settings,
163 0 : maintainState: unknownRoute.maintainState,
164 0 : curve: unknownRoute.curve,
165 0 : alignment: unknownRoute.alignment,
166 0 : parameter: unknownRoute.parameter,
167 0 : opaque: unknownRoute.opaque,
168 0 : binding: unknownRoute.binding,
169 0 : transitionDuration: unknownRoute.transitionDuration,
170 0 : popGesture: unknownRoute.popGesture,
171 0 : transition: unknownRoute.transition,
172 0 : fullscreenDialog: unknownRoute.fullscreenDialog,
173 : )));
174 : }
175 : }
176 :
177 3 : @override
178 : Widget build(BuildContext context) {
179 3 : return GetBuilder<GetMaterialController>(
180 6 : init: Get.getController,
181 3 : dispose: (d) {
182 3 : onDispose?.call();
183 : },
184 3 : initState: (i) {
185 3 : initialBinding?.dependencies();
186 9 : Get.smartManagement = smartManagement;
187 3 : onInit?.call();
188 3 : if (namedRoutes != null) {
189 3 : namedRoutes.forEach((key, value) {
190 4 : Get.getController.parse.addRoute(key);
191 : });
192 : }
193 3 : Get.config(
194 6 : enableLog: enableLog ?? Get.isLogEnable,
195 6 : defaultTransition: defaultTransition ?? Get.defaultTransition,
196 6 : defaultOpaqueRoute: opaqueRoute ?? Get.isOpaqueRouteDefault,
197 6 : defaultPopGesture: popGesture ?? Get.isPopGestureEnable,
198 : defaultDurationTransition:
199 6 : transitionDuration ?? Get.defaultDurationTransition,
200 6 : defaultGlobalState: defaultGlobalState ?? Get.defaultGlobalState,
201 : );
202 : },
203 3 : builder: (_) {
204 3 : return MaterialApp(
205 3 : key: key,
206 : navigatorKey:
207 6 : (navigatorKey == null ? Get.key : Get.addKey(navigatorKey)),
208 3 : home: home,
209 3 : routes: routes ?? const <String, WidgetBuilder>{},
210 3 : initialRoute: initialRoute,
211 4 : onGenerateRoute: (namedRoutes == null || onUnknownRoute != null
212 3 : ? onGenerateRoute
213 1 : : namedRoutesGenerate),
214 3 : onGenerateInitialRoutes: onGenerateInitialRoutes,
215 3 : onUnknownRoute: onUnknownRoute,
216 3 : navigatorObservers: (navigatorObservers == null
217 0 : ? <NavigatorObserver>[GetObserver(routingCallback)]
218 9 : : <NavigatorObserver>[GetObserver(routingCallback)]
219 6 : ..addAll(navigatorObservers)),
220 3 : builder: builder,
221 3 : title: title ?? '',
222 3 : onGenerateTitle: onGenerateTitle,
223 3 : color: color,
224 9 : theme: _.theme ?? theme ?? ThemeData.fallback(),
225 3 : darkTheme: darkTheme,
226 6 : themeMode: _.themeMode ?? themeMode ?? ThemeMode.system,
227 3 : locale: locale,
228 3 : localizationsDelegates: localizationsDelegates,
229 3 : localeListResolutionCallback: localeListResolutionCallback,
230 3 : localeResolutionCallback: localeResolutionCallback,
231 : supportedLocales:
232 3 : supportedLocales ?? const <Locale>[Locale('en', 'US')],
233 3 : debugShowMaterialGrid: debugShowMaterialGrid ?? false,
234 3 : showPerformanceOverlay: showPerformanceOverlay ?? false,
235 : checkerboardRasterCacheImages:
236 3 : checkerboardRasterCacheImages ?? false,
237 3 : checkerboardOffscreenLayers: checkerboardOffscreenLayers ?? false,
238 3 : showSemanticsDebugger: showSemanticsDebugger ?? false,
239 3 : debugShowCheckedModeBanner: debugShowCheckedModeBanner ?? true,
240 3 : shortcuts: shortcuts,
241 : // actions: actions,
242 : );
243 : });
244 : }
245 : }
|