main.dart 4.43 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
library stocks;

7
import 'dart:async';
8

9
import 'package:flutter/material.dart';
10 11 12 13 14 15
import 'package:flutter/rendering.dart' show
  debugPaintSizeEnabled,
  debugPaintBaselinesEnabled,
  debugPaintLayerBordersEnabled,
  debugPaintPointersEnabled,
  debugRepaintRainbowEnabled;
16
import 'package:flutter_localizations/flutter_localizations.dart';
17

18 19 20 21 22 23
import 'stock_data.dart';
import 'stock_home.dart';
import 'stock_settings.dart';
import 'stock_strings.dart';
import 'stock_symbol_viewer.dart';
import 'stock_types.dart';
24

25 26 27 28 29 30 31 32
class _StocksLocalizationsDelegate extends LocalizationsDelegate<StockStrings> {
  @override
  Future<StockStrings> load(Locale locale) => StockStrings.load(locale);

  @override
  bool shouldReload(_StocksLocalizationsDelegate old) => false;
}

33
class StocksApp extends StatefulWidget {
34
  @override
35 36
  StocksAppState createState() => new StocksAppState();
}
37

38
class StocksAppState extends State<StocksApp> {
39
  StockData stocks;
40

41 42 43
  StockConfiguration _configuration = new StockConfiguration(
    stockMode: StockMode.optimistic,
    backupMode: BackupMode.enabled,
44 45
    debugShowGrid: false,
    debugShowSizes: false,
46 47 48 49
    debugShowBaselines: false,
    debugShowLayers: false,
    debugShowPointers: false,
    debugShowRainbow: false,
Hixie's avatar
Hixie committed
50 51
    showPerformanceOverlay: false,
    showSemanticsDebugger: false
52 53
  );

54
  @override
55 56
  void initState() {
    super.initState();
57
    stocks = new StockData();
58 59
  }

60
  void configurationUpdater(StockConfiguration value) {
61
    setState(() {
62
      _configuration = value;
63 64 65
    });
  }

66
  ThemeData get theme {
67
    switch (_configuration.stockMode) {
68 69
      case StockMode.optimistic:
        return new ThemeData(
70
          brightness: Brightness.light,
71 72 73 74
          primarySwatch: Colors.purple
        );
      case StockMode.pessimistic:
        return new ThemeData(
75
          brightness: Brightness.dark,
76
          accentColor: Colors.redAccent
77
        );
78
    }
pq's avatar
pq committed
79
    assert(_configuration.stockMode != null);
pq's avatar
pq committed
80
    return null;
81
  }
82

83
  Route<Null> _getRoute(RouteSettings settings) {
84
    // Routes, by convention, are split on slashes, like filesystem paths.
85
    final List<String> path = settings.name.split('/');
86 87
    // We only support paths that start with a slash, so bail if
    // the first component is not empty:
Hixie's avatar
Hixie committed
88 89
    if (path[0] != '')
      return null;
90 91 92 93 94 95
    // If the path is "/stock:..." then show a stock page for the
    // specified stock symbol.
    if (path[1].startsWith('stock:')) {
      // We don't yet support subpages of a stock, so bail if there's
      // any more path components.
      if (path.length != 2)
Hixie's avatar
Hixie committed
96
        return null;
97 98 99 100 101 102 103
      // Extract the symbol part of "stock:..." and return a route
      // for that symbol.
      final String symbol = path[1].substring(6);
      return new MaterialPageRoute<Null>(
        settings: settings,
        builder: (BuildContext context) => new StockSymbolPage(symbol: symbol, stocks: stocks),
      );
Hixie's avatar
Hixie committed
104
    }
105
    // The other paths we support are in the routes table.
Hixie's avatar
Hixie committed
106 107 108
    return null;
  }

109
  @override
110
  Widget build(BuildContext context) {
111 112
    assert(() {
      debugPaintSizeEnabled = _configuration.debugShowSizes;
113 114 115 116
      debugPaintBaselinesEnabled = _configuration.debugShowBaselines;
      debugPaintLayerBordersEnabled = _configuration.debugShowLayers;
      debugPaintPointersEnabled = _configuration.debugShowPointers;
      debugRepaintRainbowEnabled = _configuration.debugShowRainbow;
117
      return true;
118
    }());
Adam Barth's avatar
Adam Barth committed
119
    return new MaterialApp(
120 121
      title: 'Stocks',
      theme: theme,
122
      localizationsDelegates: <LocalizationsDelegate<dynamic>>[
123
        new _StocksLocalizationsDelegate(),
124 125
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
126
      ],
127 128 129 130
      supportedLocales: const <Locale>[
        const Locale('en', 'US'),
        const Locale('es', 'ES'),
      ],
131
      debugShowMaterialGrid: _configuration.debugShowGrid,
132
      showPerformanceOverlay: _configuration.showPerformanceOverlay,
Hixie's avatar
Hixie committed
133
      showSemanticsDebugger: _configuration.showSemanticsDebugger,
134
      routes: <String, WidgetBuilder>{
135
         '/':         (BuildContext context) => new StockHome(stocks, _configuration, configurationUpdater),
136
         '/settings': (BuildContext context) => new StockSettings(_configuration, configurationUpdater)
Hixie's avatar
Hixie committed
137
      },
138
      onGenerateRoute: _getRoute,
139 140
    );
  }
141 142 143
}

void main() {
144
  runApp(new StocksApp());
145
}