main.dart 3.8 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:intl/intl.dart';
17

18
import 'i18n/stock_messages_all.dart';
19 20 21 22 23 24
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';
25

26
class StocksApp extends StatefulWidget {
27
  @override
28 29
  StocksAppState createState() => new StocksAppState();
}
30

31
class StocksAppState extends State<StocksApp> {
32

Hixie's avatar
Hixie committed
33 34
  final Map<String, Stock> _stocks = <String, Stock>{};
  final List<String> _symbols = <String>[];
35

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

49
  @override
50 51
  void initState() {
    super.initState();
52
    new StockDataFetcher((StockData data) {
53
      setState(() {
Hixie's avatar
Hixie committed
54
        data.appendTo(_stocks, _symbols);
55
      });
56
    });
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
    final List<String> path = settings.name.split('/');
Hixie's avatar
Hixie committed
84 85 86 87 88
    if (path[0] != '')
      return null;
    if (path[1] == 'stock') {
      if (path.length != 3)
        return null;
89
      if (_stocks.containsKey(path[2])) {
90
        return new MaterialPageRoute<Null>(
91 92 93 94
          settings: settings,
          builder: (BuildContext context) => new StockSymbolPage(stock: _stocks[path[2]])
        );
      }
Hixie's avatar
Hixie committed
95 96 97 98
    }
    return null;
  }

99
  Future<LocaleQueryData> _onLocaleChanged(Locale locale) async {
100
    final String localeString = locale.toString();
Seth Ladd's avatar
Seth Ladd committed
101 102 103
    await initializeMessages(localeString);
    Intl.defaultLocale = localeString;
    return StockStrings.instance;
104 105
  }

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

void main() {
133
  runApp(new StocksApp());
134
}