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