app.dart 2.25 KB
Newer Older
1 2 3 4 5 6 7 8
// 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.

import 'dart:sky' as sky;

import 'package:sky/material.dart';
import 'package:sky/painting.dart';
9
import 'package:sky/services.dart';
10 11 12 13 14 15
import 'package:sky/src/widgets/basic.dart';
import 'package:sky/src/widgets/binding.dart';
import 'package:sky/src/widgets/framework.dart';
import 'package:sky/src/widgets/navigator.dart';
import 'package:sky/src/widgets/theme.dart';
import 'package:sky/src/widgets/title.dart';
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

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
);

class App extends StatefulComponent {
  App({
    Key key,
    this.title,
    this.theme,
Hixie's avatar
Hixie committed
33 34
    this.routes,
    this.onGenerateRoute
35 36 37 38 39
  }): super(key: key);

  final String title;
  final ThemeData theme;
  final Map<String, RouteBuilder> routes;
Hixie's avatar
Hixie committed
40
  final RouteGenerator onGenerateRoute;
41 42 43 44 45 46 47 48

  AppState createState() => new AppState();
}

class AppState extends State<App> {

  GlobalObjectKey _navigator;

49 50
  void initState() {
    super.initState();
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    _navigator = new GlobalObjectKey(this);
    WidgetFlutterBinding.instance.addEventListener(_backHandler);
  }

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

  void _backHandler(sky.Event event) {
    assert(mounted);
    if (event.type == 'back') {
      NavigatorState navigator = _navigator.currentState;
      assert(navigator != null);
      if (navigator.hasPreviousRoute)
        navigator.pop();
67 68
      else
        activity.finishCurrentActivity();
69 70 71 72 73
    }
  }

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

89
}