From 03f9210641542adeca5ad1e19f95356185d99758 Mon Sep 17 00:00:00 2001 From: Hans Muller Date: Thu, 21 Apr 2016 17:14:22 -0700 Subject: [PATCH] Refactored MaterialApp (#3475) * Refactored MaterialApp --- packages/flutter/lib/src/material/app.dart | 123 +++++++++++++-------- packages/flutter/lib/src/widgets/app.dart | 8 +- 2 files changed, 82 insertions(+), 49 deletions(-) diff --git a/packages/flutter/lib/src/material/app.dart b/packages/flutter/lib/src/material/app.dart index fca718ac10..bb9403b1a3 100644 --- a/packages/flutter/lib/src/material/app.dart +++ b/packages/flutter/lib/src/material/app.dart @@ -25,68 +25,45 @@ const TextStyle _errorTextStyle = const TextStyle( /// An application that uses material design. /// /// A convenience widget that wraps a number of widgets that are commonly -/// required for material design applications. It builds upon +/// required for material design applications. It builds upon a /// [WidgetsApp] by adding material-design specific functionality, such as /// [AnimatedTheme] and [GridPaper]. This widget also configures the top-level -/// [Navigator] to perform [Hero] animations. +/// [Navigator]'s observer to perform [Hero] animations. /// /// See also: /// /// * [WidgetsApp] /// * [Scaffold] /// * [MaterialPageRoute] -class MaterialApp extends WidgetsApp { +class MaterialApp extends StatefulWidget { + /// Creates a MaterialApp. /// /// At least one of [home], [routes], or [onGenerateRoute] must be /// given. If only [routes] is given, it must include an entry for /// the [Navigator.defaultRouteName] (`'/'`). /// - /// See also the [new WidgetsApp] constructor (which this extends). + /// This class creates an instance of [WidgetsApp]. MaterialApp({ Key key, - String title, - ThemeData theme, - Widget home, - Map routes: const {}, - RouteFactory onGenerateRoute, - LocaleChangedCallback onLocaleChanged, + this.title, + this.theme, + this.home, + this.routes: const {}, + this.onGenerateRoute, + this.onLocaleChanged, this.debugShowMaterialGrid: false, - bool showPerformanceOverlay: false, - bool showSemanticsDebugger: false, - bool debugShowCheckedModeBanner: true - }) : theme = theme, - home = home, - routes = routes, - super( - key: key, - title: title, - textStyle: _errorTextStyle, - color: theme?.primaryColor ?? Colors.blue[500], // blue[500] is the primary color of the default theme - onGenerateRoute: (RouteSettings settings) { - WidgetBuilder builder = routes[settings.name]; - if (builder == null && home != null && settings.name == Navigator.defaultRouteName) - builder = (BuildContext context) => home; - if (builder != null) { - return new MaterialPageRoute( - builder: builder, - settings: settings - ); - } - if (onGenerateRoute != null) - return onGenerateRoute(settings); - return null; - }, - onLocaleChanged: onLocaleChanged, - showPerformanceOverlay: showPerformanceOverlay, - showSemanticsDebugger: showSemanticsDebugger, - debugShowCheckedModeBanner: debugShowCheckedModeBanner - ) { + this.showPerformanceOverlay: false, + this.showSemanticsDebugger: false, + this.debugShowCheckedModeBanner: true + }) : super(key: key) { assert(debugShowMaterialGrid != null); assert(routes != null); assert(!routes.containsKey(Navigator.defaultRouteName) || (home == null)); assert(routes.containsKey(Navigator.defaultRouteName) || (home != null) || (onGenerateRoute != null)); - } + } + /// A one-line description of this app for use in the window manager. + final String title; /// The colors to use for the application's widgets. final ThemeData theme; @@ -124,6 +101,37 @@ class MaterialApp extends WidgetsApp { /// build the page instead. final Map routes; + /// The route generator callback used when the app is navigated to a + /// named route. + final RouteFactory onGenerateRoute; + + /// Callback that is invoked when the operating system changes the + /// current locale. + final LocaleChangedCallback onLocaleChanged; + + /// Turns on a performance overlay. + /// https://flutter.io/debugging/#performanceoverlay + final bool showPerformanceOverlay; + + /// Turns on an overlay that shows the accessibility information + /// reported by the framework. + final bool showSemanticsDebugger; + + /// Turns on a little "SLOW MODE" banner in checked mode to indicate + /// that the app is in checked mode. This is on by default (in + /// checked mode), to turn it off, set the constructor argument to + /// false. In release mode this has no effect. + /// + /// To get this banner in your application if you're not using + /// WidgetsApp, include a [CheckedModeBanner] widget in your app. + /// + /// This banner is intended to deter people from complaining that your + /// app is slow when it's in checked mode. In checked mode, Flutter + /// enables a large number of expensive diagnostics to aid in + /// development, and so performance in checked mode is not + /// representative of what will happen in release mode. + final bool debugShowCheckedModeBanner; + /// Turns on a [GridPaper] overlay that paints a baseline grid /// Material apps: /// https://www.google.com/design/spec/layout/metrics-keylines.html @@ -134,11 +142,23 @@ class MaterialApp extends WidgetsApp { _MaterialAppState createState() => new _MaterialAppState(); } -class _MaterialAppState extends WidgetsAppState { +class _MaterialAppState extends State { final HeroController _heroController = new HeroController(); - @override - NavigatorObserver get navigatorObserver => _heroController; + Route _onGenerateRoute(RouteSettings settings) { + WidgetBuilder builder = config.routes[settings.name]; + if (builder == null && config.home != null && settings.name == Navigator.defaultRouteName) + builder = (BuildContext context) => config.home; + if (builder != null) { + return new MaterialPageRoute( + builder: builder, + settings: settings + ); + } + if (config.onGenerateRoute != null) + return config.onGenerateRoute(settings); + return null; + } @override Widget build(BuildContext context) { @@ -146,8 +166,19 @@ class _MaterialAppState extends WidgetsAppState { Widget result = new AnimatedTheme( data: theme, duration: kThemeAnimationDuration, - child: super.build(context) + child: new WidgetsApp( + title: config.title, + textStyle: _errorTextStyle, + color: theme?.primaryColor ?? Colors.blue[500], // blue[500] is the primary color of the default theme + navigatorObserver: _heroController, + onGenerateRoute: _onGenerateRoute, + onLocaleChanged: config.onLocaleChanged, + showPerformanceOverlay: config.showPerformanceOverlay, + showSemanticsDebugger: config.showSemanticsDebugger, + debugShowCheckedModeBanner: config.debugShowCheckedModeBanner + ) ); + assert(() { if (config.debugShowMaterialGrid) { result = new GridPaper( @@ -160,7 +191,7 @@ class _MaterialAppState extends WidgetsAppState { } return true; }); + return result; } - } diff --git a/packages/flutter/lib/src/widgets/app.dart b/packages/flutter/lib/src/widgets/app.dart index 6e890a3d20..968efdeb06 100644 --- a/packages/flutter/lib/src/widgets/app.dart +++ b/packages/flutter/lib/src/widgets/app.dart @@ -37,6 +37,7 @@ class WidgetsApp extends StatefulWidget { this.title, this.textStyle, this.color, + this.navigatorObserver, this.onGenerateRoute, this.onLocaleChanged, this.showPerformanceOverlay: false, @@ -92,6 +93,9 @@ class WidgetsApp extends StatefulWidget { /// representative of what will happen in release mode. final bool debugShowCheckedModeBanner; + /// The observer for the Navigator created for this app. + final NavigatorObserver navigatorObserver; + static bool showPerformanceOverlayOverride = false; @override @@ -151,8 +155,6 @@ class WidgetsAppState extends State implements WidgetsB @override void didChangeAppLifecycleState(AppLifecycleState state) { } - NavigatorObserver get navigatorObserver => null; - @override Widget build(BuildContext context) { if (config.onLocaleChanged != null && _localeData == null) { @@ -176,7 +178,7 @@ class WidgetsAppState extends State implements WidgetsB key: _navigator, initialRoute: ui.window.defaultRouteName, onGenerateRoute: config.onGenerateRoute, - observer: navigatorObserver + observer: config.navigatorObserver ) ) )