material_app.dart 2.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 6
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
7 8 9

import 'theme.dart';
import 'title.dart';
10 11 12 13 14 15 16 17 18 19 20 21

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

Adam Barth's avatar
Adam Barth committed
22 23
class MaterialApp extends StatefulComponent {
  MaterialApp({
24 25 26
    Key key,
    this.title,
    this.theme,
Hixie's avatar
Hixie committed
27 28
    this.routes,
    this.onGenerateRoute
29
  }) : super(key: key) {
Adam Barth's avatar
Adam Barth committed
30
    assert(routes != null);
31
  }
32 33 34 35

  final String title;
  final ThemeData theme;
  final Map<String, RouteBuilder> routes;
Hixie's avatar
Hixie committed
36
  final RouteGenerator onGenerateRoute;
37

Adam Barth's avatar
Adam Barth committed
38
  _MaterialAppState createState() => new _MaterialAppState();
39 40
}

Adam Barth's avatar
Adam Barth committed
41
class _MaterialAppState extends State<MaterialApp> {
42 43 44

  GlobalObjectKey _navigator;

45 46
  void initState() {
    super.initState();
47 48 49 50 51 52 53 54 55
    _navigator = new GlobalObjectKey(this);
    WidgetFlutterBinding.instance.addEventListener(_backHandler);
  }

  void dispose() {
    WidgetFlutterBinding.instance.removeEventListener(_backHandler);
    super.dispose();
  }

56
  void _backHandler(InputEvent event) {
57 58 59 60 61 62
    assert(mounted);
    if (event.type == 'back') {
      NavigatorState navigator = _navigator.currentState;
      assert(navigator != null);
      if (navigator.hasPreviousRoute)
        navigator.pop();
63 64
      else
        activity.finishCurrentActivity();
65 66 67 68 69
    }
  }

  Widget build(BuildContext context) {
    return new Theme(
Hixie's avatar
Hixie committed
70
      data: config.theme ?? new ThemeData.fallback(),
71 72 73 74 75 76
      child: new DefaultTextStyle(
        style: _errorTextStyle,
        child: new Title(
          title: config.title,
          child: new Navigator(
            key: _navigator,
Hixie's avatar
Hixie committed
77 78
            routes: config.routes,
            onGenerateRoute: config.onGenerateRoute
79 80 81 82 83 84
          )
        )
      )
    );
  }

85
}