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