Line data Source code
1 : import 'package:flutter/foundation.dart';
2 : import 'package:flutter/material.dart';
3 : import 'package:get/get.dart';
4 : import 'package:get/src/instance/get_instance.dart';
5 : import 'package:get/src/routes/get_route.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.translationsKeys,
22 : this.translations,
23 : this.title = '',
24 : this.onGenerateTitle,
25 : this.color,
26 : this.customTransition,
27 : this.onInit,
28 : this.onDispose,
29 : this.theme,
30 : this.darkTheme,
31 : this.themeMode = ThemeMode.system,
32 : this.locale,
33 : this.localizationsDelegates,
34 : this.localeListResolutionCallback,
35 : this.localeResolutionCallback,
36 : this.supportedLocales = const <Locale>[Locale('en', 'US')],
37 : this.debugShowMaterialGrid = false,
38 : this.showPerformanceOverlay = false,
39 : this.checkerboardRasterCacheImages = false,
40 : this.checkerboardOffscreenLayers = false,
41 : this.showSemanticsDebugger = false,
42 : this.debugShowCheckedModeBanner = true,
43 : this.shortcuts,
44 : this.smartManagement = SmartManagement.full,
45 : this.initialBinding,
46 : this.unknownRoute,
47 : this.routingCallback,
48 : this.defaultTransition,
49 : // this.actions,
50 : this.getPages,
51 : this.opaqueRoute,
52 : this.enableLog,
53 : this.popGesture,
54 : this.transitionDuration,
55 : this.defaultGlobalState,
56 1 : }) : assert(routes != null),
57 1 : assert(navigatorObservers != null),
58 1 : assert(title != null),
59 2 : assert(debugShowMaterialGrid != null),
60 1 : assert(showPerformanceOverlay != null),
61 1 : assert(checkerboardRasterCacheImages != null),
62 1 : assert(checkerboardOffscreenLayers != null),
63 1 : assert(showSemanticsDebugger != null),
64 1 : assert(debugShowCheckedModeBanner != null),
65 3 : super(key: key);
66 :
67 : final GlobalKey<NavigatorState> navigatorKey;
68 : final Widget home;
69 : final Map<String, WidgetBuilder> routes;
70 : final String initialRoute;
71 : final RouteFactory onGenerateRoute;
72 : final InitialRouteListFactory onGenerateInitialRoutes;
73 : final RouteFactory onUnknownRoute;
74 : final List<NavigatorObserver> navigatorObservers;
75 : final TransitionBuilder builder;
76 : final String title;
77 : final GenerateAppTitle onGenerateTitle;
78 : final ThemeData theme;
79 : final ThemeData darkTheme;
80 : final ThemeMode themeMode;
81 : final CustomTransition customTransition;
82 : final Color color;
83 : final Map<String, Map<String, String>> translationsKeys;
84 : final Translations translations;
85 : final Locale locale;
86 : final Iterable<LocalizationsDelegate<dynamic>> localizationsDelegates;
87 : final LocaleListResolutionCallback localeListResolutionCallback;
88 : final LocaleResolutionCallback localeResolutionCallback;
89 : final Iterable<Locale> supportedLocales;
90 : final bool showPerformanceOverlay;
91 : final bool checkerboardRasterCacheImages;
92 : final bool checkerboardOffscreenLayers;
93 : final bool showSemanticsDebugger;
94 : final bool debugShowCheckedModeBanner;
95 : final Map<LogicalKeySet, Intent> shortcuts;
96 : // final Map<LocalKey, ActionFactory> actions;
97 : final bool debugShowMaterialGrid;
98 : final Function(Routing) routingCallback;
99 : final Transition defaultTransition;
100 : final bool opaqueRoute;
101 : final VoidCallback onInit;
102 : final VoidCallback onDispose;
103 : final bool enableLog;
104 : final bool popGesture;
105 : final SmartManagement smartManagement;
106 : final Bindings initialBinding;
107 : final Duration transitionDuration;
108 : final bool defaultGlobalState;
109 : final List<GetPage> getPages;
110 : final GetPage unknownRoute;
111 :
112 1 : Route<dynamic> generator(RouteSettings settings) {
113 4 : final match = Get.routeTree.matchRoute(settings.name);
114 3 : Get.parameters = match?.parameters;
115 :
116 1 : if (match?.route == null) {
117 0 : return GetPageRoute(
118 0 : page: unknownRoute.page,
119 0 : parameter: unknownRoute.parameter,
120 : settings:
121 0 : RouteSettings(name: settings.name, arguments: settings.arguments),
122 0 : curve: unknownRoute.curve,
123 0 : opaque: unknownRoute.opaque,
124 0 : customTransition: match.route.customTransition,
125 0 : binding: unknownRoute.binding,
126 0 : bindings: unknownRoute.bindings,
127 0 : duration: (transitionDuration ?? unknownRoute.transitionDuration),
128 0 : transition: unknownRoute.transition,
129 0 : popGesture: unknownRoute.popGesture,
130 0 : fullscreenDialog: unknownRoute.fullscreenDialog,
131 : );
132 : }
133 :
134 1 : return GetPageRoute(
135 2 : page: match.route.page,
136 2 : parameter: match.route.parameter,
137 : settings:
138 3 : RouteSettings(name: settings.name, arguments: settings.arguments),
139 2 : curve: match.route.curve,
140 2 : opaque: match.route.opaque,
141 2 : customTransition: match.route.customTransition,
142 2 : binding: match.route.binding,
143 2 : bindings: match.route.bindings,
144 3 : duration: (transitionDuration ?? match.route.transitionDuration),
145 2 : transition: match.route.transition,
146 2 : popGesture: match.route.popGesture,
147 2 : fullscreenDialog: match.route.fullscreenDialog,
148 : );
149 : }
150 :
151 1 : List<Route<dynamic>> initialRoutesGenerate(String name) {
152 3 : final match = Get.routeTree.matchRoute(name);
153 3 : Get.parameters = match?.parameters;
154 :
155 1 : return [
156 1 : GetPageRoute(
157 2 : page: match.route.page,
158 2 : parameter: match.route.parameter,
159 1 : settings: RouteSettings(name: name, arguments: null),
160 2 : curve: match.route.curve,
161 2 : opaque: match.route.opaque,
162 2 : binding: match.route.binding,
163 2 : bindings: match.route.bindings,
164 3 : duration: (transitionDuration ?? match.route.transitionDuration),
165 2 : transition: match.route.transition,
166 2 : popGesture: match.route.popGesture,
167 2 : fullscreenDialog: match.route.fullscreenDialog,
168 : )
169 : ];
170 : }
171 :
172 3 : @override
173 : Widget build(BuildContext context) {
174 3 : return GetBuilder<GetMaterialController>(
175 6 : init: Get.getxController,
176 3 : dispose: (d) {
177 3 : onDispose?.call();
178 : },
179 3 : initState: (i) {
180 3 : if (locale != null) {
181 0 : Get.locale = locale;
182 : }
183 :
184 3 : if (translations != null) {
185 0 : Get.translations = translations.keys;
186 3 : } else if (translationsKeys != null) {
187 0 : Get.translations = translationsKeys;
188 : }
189 :
190 9 : Get.customTransition = customTransition;
191 :
192 3 : initialBinding?.dependencies();
193 9 : Get.addPages(getPages);
194 3 : GetConfig.smartManagement = smartManagement;
195 3 : onInit?.call();
196 :
197 6 : Get.config(
198 3 : enableLog: enableLog ?? GetConfig.isLogEnable,
199 9 : defaultTransition: defaultTransition ?? Get.defaultTransition,
200 9 : defaultOpaqueRoute: opaqueRoute ?? Get.isOpaqueRouteDefault,
201 9 : defaultPopGesture: popGesture ?? Get.isPopGestureEnable,
202 : defaultDurationTransition:
203 9 : transitionDuration ?? Get.defaultDurationTransition,
204 9 : defaultGlobalState: defaultGlobalState ?? Get.defaultGlobalState,
205 : );
206 : },
207 3 : builder: (_) {
208 3 : return MaterialApp(
209 3 : key: key,
210 : navigatorKey:
211 9 : (navigatorKey == null ? Get.key : Get.addKey(navigatorKey)),
212 3 : home: home,
213 3 : routes: routes ?? const <String, WidgetBuilder>{},
214 3 : initialRoute: initialRoute,
215 7 : onGenerateRoute: (getPages != null ? generator : onGenerateRoute),
216 4 : onGenerateInitialRoutes: (getPages == null || home != null)
217 3 : ? onGenerateInitialRoutes
218 1 : : initialRoutesGenerate,
219 3 : onUnknownRoute: onUnknownRoute,
220 3 : navigatorObservers: (navigatorObservers == null
221 0 : ? <NavigatorObserver>[GetObserver(routingCallback)]
222 9 : : <NavigatorObserver>[GetObserver(routingCallback)]
223 6 : ..addAll(navigatorObservers)),
224 3 : builder: builder,
225 3 : title: title ?? '',
226 3 : onGenerateTitle: onGenerateTitle,
227 3 : color: color,
228 9 : theme: _.theme ?? theme ?? ThemeData.fallback(),
229 3 : darkTheme: darkTheme,
230 6 : themeMode: _.themeMode ?? themeMode ?? ThemeMode.system,
231 9 : locale: Get.locale ?? locale,
232 3 : localizationsDelegates: localizationsDelegates,
233 3 : localeListResolutionCallback: localeListResolutionCallback,
234 3 : localeResolutionCallback: localeResolutionCallback,
235 : supportedLocales:
236 3 : supportedLocales ?? const <Locale>[Locale('en', 'US')],
237 3 : debugShowMaterialGrid: debugShowMaterialGrid ?? false,
238 3 : showPerformanceOverlay: showPerformanceOverlay ?? false,
239 : checkerboardRasterCacheImages:
240 3 : checkerboardRasterCacheImages ?? false,
241 3 : checkerboardOffscreenLayers: checkerboardOffscreenLayers ?? false,
242 3 : showSemanticsDebugger: showSemanticsDebugger ?? false,
243 3 : debugShowCheckedModeBanner: debugShowCheckedModeBanner ?? true,
244 3 : shortcuts: shortcuts,
245 : // actions: actions,
246 : );
247 : });
248 : }
249 : }
250 :
251 : abstract class Translations {
252 : Map<String, Map<String, String>> get keys;
253 : }
254 :
255 : extension Trans on String {
256 0 : String get tr {
257 0 : if (Get.locale?.languageCode == null) return this;
258 0 : if (Get.translations
259 0 : .containsKey("${Get.locale.languageCode}_${Get.locale.countryCode}")) {
260 0 : return Get.translations[
261 0 : "${Get.locale.languageCode}_${Get.locale.countryCode}"][this];
262 0 : } else if (Get.translations.containsKey(Get.locale.languageCode)) {
263 0 : return Get.translations[Get.locale.languageCode][this];
264 0 : } else if (Get.translations.isNotEmpty) {
265 0 : return Get.translations.values.first[this];
266 : } else {
267 : return this;
268 : }
269 : }
270 :
271 0 : String trArgs([List<String> args]) {
272 0 : String key = tr;
273 : if (args != null) {
274 0 : args.forEach((arg) {
275 0 : key = key.replaceFirst(RegExp(r'%s'), arg.toString());
276 : });
277 : }
278 : return key;
279 : }
280 : }
|