app.dart 3.05 KB
Newer Older
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:flutter/rendering.dart';
6
import 'package:flutter/widgets.dart';
7

8
import 'colors.dart';
9
import 'page.dart';
10
import 'theme.dart';
11

12 13
export 'dart:ui' show Locale;

14 15 16 17 18 19
const TextStyle _errorTextStyle = const TextStyle(
  color: const Color(0xD0FF0000),
  fontFamily: 'monospace',
  fontSize: 48.0,
  fontWeight: FontWeight.w900,
  textAlign: TextAlign.right,
20
  decoration: TextDecoration.underline,
21
  decorationColor: const Color(0xFFFFFF00),
22 23 24
  decorationStyle: TextDecorationStyle.double
);

25

26
class MaterialApp extends WidgetsApp {
Adam Barth's avatar
Adam Barth committed
27
  MaterialApp({
28
    Key key,
29 30 31 32 33
    String title,
    ThemeData theme,
    Map<String, RouteBuilder> routes: const <String, RouteBuilder>{},
    RouteFactory onGenerateRoute,
    LocaleChangedCallback onLocaleChanged,
34
    this.debugShowMaterialGrid: false,
35 36 37 38 39 40 41 42 43 44 45 46 47
    bool showPerformanceOverlay: false,
    bool showSemanticsDebugger: false,
    bool debugShowCheckedModeBanner: true
  }) : theme = theme,
       super(
    key: key,
    title: title,
    textStyle: _errorTextStyle,
    color: theme?.primaryColor ?? Colors.blue[500], // blue[500] is the primary color of the default theme
    routes: routes,
    onGenerateRoute: (RouteSettings settings) {
      RouteBuilder builder = routes[settings.name];
      if (builder != null) {
48
        return new MaterialPageRoute<Null>(
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
          builder: (BuildContext context) {
            return builder(new RouteArguments(context: context));
          },
          settings: settings
        );
      }
      if (onGenerateRoute != null)
        return onGenerateRoute(settings);
      return null;
    },
    onLocaleChanged: onLocaleChanged,
    showPerformanceOverlay: showPerformanceOverlay,
    showSemanticsDebugger: showSemanticsDebugger,
    debugShowCheckedModeBanner: debugShowCheckedModeBanner
  ) {
Ian Hickson's avatar
Ian Hickson committed
64
    assert(debugShowMaterialGrid != null);
65
  }
66

67
  /// The colors to use for the application's widgets.
68
  final ThemeData theme;
69 70 71 72 73

  /// Turns on a [GridPaper] overlay that paints a baseline grid
  /// Material apps:
  /// https://www.google.com/design/spec/layout/metrics-keylines.html
  /// Only available in checked mode.
Ian Hickson's avatar
Ian Hickson committed
74
  final bool debugShowMaterialGrid;
75

Adam Barth's avatar
Adam Barth committed
76
  _MaterialAppState createState() => new _MaterialAppState();
77 78
}

79
class _MaterialAppState extends WidgetsAppState<MaterialApp> {
80

Adam Barth's avatar
Adam Barth committed
81
  final HeroController _heroController = new HeroController();
82
  NavigatorObserver get navigatorObserver => _heroController;
83

84
  Widget build(BuildContext context) {
85
    ThemeData theme = config.theme ?? new ThemeData.fallback();
86 87 88 89
    Widget result = new AnimatedTheme(
      data: theme,
      duration: kThemeAnimationDuration,
      child: super.build(context)
90
    );
Ian Hickson's avatar
Ian Hickson committed
91 92 93 94 95 96 97 98 99 100 101 102 103
    assert(() {
      if (config.debugShowMaterialGrid) {
        result = new GridPaper(
          color: const Color(0xE0F9BBE0),
          interval: 8.0,
          divisions: 2,
          subDivisions: 1,
          child: result
        );
      }
      return true;
    });
    return result;
104 105
  }

106
}