main.dart 4.16 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

17 18 19 20 21 22
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';
23

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

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

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

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

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

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

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

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

82
  Route<Null> _getRoute(RouteSettings settings) {
83
    // Routes, by convention, are split on slashes, like filesystem paths.
84
    final List<String> path = settings.name.split('/');
85 86
    // We only support paths that start with a slash, so bail if
    // the first component is not empty:
Hixie's avatar
Hixie committed
87 88
    if (path[0] != '')
      return null;
89 90 91 92 93 94
    // 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
95
        return null;
96 97 98 99 100 101 102
      // 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
103
    }
104
    // The other paths we support are in the routes table.
Hixie's avatar
Hixie committed
105 106 107
    return null;
  }

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

void main() {
137
  runApp(new StocksApp());
138
}