material_app.dart 4.42 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 'dart:async';
6 7 8
import 'dart:ui' as ui;

import 'package:flutter/rendering.dart';
9 10
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
11

12
import 'page.dart';
13
import 'theme.dart';
14 15 16 17 18 19 20

const TextStyle _errorTextStyle = const TextStyle(
  color: const Color(0xD0FF0000),
  fontFamily: 'monospace',
  fontSize: 48.0,
  fontWeight: FontWeight.w900,
  textAlign: TextAlign.right,
21
  decoration: TextDecoration.underline,
22 23 24 25
  decorationColor: const Color(0xFFFF00),
  decorationStyle: TextDecorationStyle.double
);

Adam Barth's avatar
Adam Barth committed
26 27 28 29 30 31 32 33 34
AssetBundle _initDefaultBundle() {
  if (rootBundle != null)
    return rootBundle;
  const String _kAssetBase = '/packages/material_design_icons/icons/';
  return new NetworkAssetBundle(Uri.base.resolve(_kAssetBase));
}

final AssetBundle _defaultBundle = _initDefaultBundle();

35 36 37 38 39 40
class RouteArguments {
  const RouteArguments({ this.context });
  final BuildContext context;
}
typedef Widget RouteBuilder(RouteArguments args);

41 42
typedef Future<LocaleQueryData> LocaleChangedCallback(ui.Locale locale);

Adam Barth's avatar
Adam Barth committed
43 44
class MaterialApp extends StatefulComponent {
  MaterialApp({
45 46 47
    Key key,
    this.title,
    this.theme,
48
    this.routes: const <String, RouteBuilder>{},
49 50
    this.onGenerateRoute,
    this.onLocaleChanged
51 52 53 54
  }) : super(key: key) {
    assert(routes != null);
    assert(routes.containsKey(Navigator.defaultRouteName) || onGenerateRoute != null);
  }
55 56 57 58

  final String title;
  final ThemeData theme;
  final Map<String, RouteBuilder> routes;
59
  final RouteFactory onGenerateRoute;
60
  final LocaleChangedCallback onLocaleChanged;
61

Adam Barth's avatar
Adam Barth committed
62
  _MaterialAppState createState() => new _MaterialAppState();
63 64
}

65
class _MaterialAppState extends State<MaterialApp> implements BindingObserver {
66 67 68

  GlobalObjectKey _navigator;

69
  Size _size;
70
  LocaleQueryData _localeData;
71

72 73
  void initState() {
    super.initState();
74
    _navigator = new GlobalObjectKey(this);
75
    _size = ui.window.size;
76
    didChangeLocale(ui.window.locale);
Ian Hickson's avatar
Ian Hickson committed
77
    WidgetFlutterBinding.instance.addObserver(this);
78 79 80
  }

  void dispose() {
Ian Hickson's avatar
Ian Hickson committed
81
    WidgetFlutterBinding.instance.removeObserver(this);
82 83 84
    super.dispose();
  }

85
  bool didPopRoute() {
86
    assert(mounted);
87 88
    NavigatorState navigator = _navigator.currentState;
    assert(navigator != null);
Hixie's avatar
Hixie committed
89 90 91 92
    navigator.openTransaction((NavigatorTransaction transaction) {
      if (!transaction.pop())
        activity.finishCurrentActivity();
    });
93
    return true;
94 95
  }

96
  void didChangeSize(Size size) => setState(() { _size = size; });
97

98 99 100 101 102 103 104 105 106
  void didChangeLocale(ui.Locale locale) {
    if (config.onLocaleChanged != null) {
      config.onLocaleChanged(locale).then((LocaleQueryData data) {
        if (mounted)
          setState(() { _localeData = data; });
      });
    }
  }

107 108
  void didChangeAppLifecycleState(ui.AppLifecycleState state) { }

Adam Barth's avatar
Adam Barth committed
109
  final HeroController _heroController = new HeroController();
110

111
  Route _generateRoute(RouteSettings settings) {
112 113 114 115 116 117 118 119 120 121 122 123
    RouteBuilder builder = config.routes[settings.name];
    if (builder != null) {
      return new MaterialPageRoute(
        builder: (BuildContext context) {
          return builder(new RouteArguments(context: context));
        },
        settings: settings
      );
    }
    if (config.onGenerateRoute != null)
      return config.onGenerateRoute(settings);
    return null;
124 125
  }

126
  Widget build(BuildContext context) {
127 128 129 130 131 132
    if (config.onLocaleChanged != null && _localeData == null) {
      // If the app expects a locale but we don't yet know the locale, then
      // don't build the widgets now.
      return new Container();
    }

133
    ThemeData theme = config.theme ?? new ThemeData.fallback();
134 135
    return new MediaQuery(
      data: new MediaQueryData(size: _size),
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
      child: new LocaleQuery(
        data: _localeData,
        child: new Theme(
          data: theme,
          child: new DefaultTextStyle(
            style: _errorTextStyle,
            child: new DefaultAssetBundle(
              bundle: _defaultBundle,
              child: new Title(
                title: config.title,
                color: theme.primaryColor,
                child: new Navigator(
                  key: _navigator,
                  initialRoute: ui.window.defaultRouteName,
                  onGenerateRoute: _generateRoute,
                  observer: _heroController
                )
Adam Barth's avatar
Adam Barth committed
153
              )
Adam Barth's avatar
Adam Barth committed
154
            )
155 156 157 158 159 160
          )
        )
      )
    );
  }

161
}