Commit 03f92106 authored by Hans Muller's avatar Hans Muller

Refactored MaterialApp (#3475)

* Refactored MaterialApp
parent a6532cc7
......@@ -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<String, WidgetBuilder> routes: const <String, WidgetBuilder>{},
RouteFactory onGenerateRoute,
LocaleChangedCallback onLocaleChanged,
this.title,
this.theme,
this.home,
this.routes: const <String, WidgetBuilder>{},
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<Null>(
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<String, WidgetBuilder> 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<MaterialApp> {
class _MaterialAppState extends State<MaterialApp> {
final HeroController _heroController = new HeroController();
@override
NavigatorObserver get navigatorObserver => _heroController;
Route<dynamic> _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<Null>(
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<MaterialApp> {
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<MaterialApp> {
}
return true;
});
return result;
}
}
......@@ -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<T extends WidgetsApp> extends State<T> 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<T extends WidgetsApp> extends State<T> implements WidgetsB
key: _navigator,
initialRoute: ui.window.defaultRouteName,
onGenerateRoute: config.onGenerateRoute,
observer: navigatorObserver
observer: config.navigatorObserver
)
)
)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment