main.dart 3.7 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 27 28
class StocksApp extends StatefulComponent {
  StocksAppState createState() => new StocksAppState();
}
29

30
class StocksAppState extends State<StocksApp> {
31

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

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

48 49
  void initState() {
    super.initState();
50
    new StockDataFetcher((StockData data) {
51
      setState(() {
Hixie's avatar
Hixie committed
52
        data.appendTo(_stocks, _symbols);
53
      });
54
    });
55 56
  }

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

63
  ThemeData get theme {
64
    switch (_configuration.stockMode) {
65 66 67 68 69 70 71 72 73 74
      case StockMode.optimistic:
        return new ThemeData(
          brightness: ThemeBrightness.light,
          primarySwatch: Colors.purple
        );
      case StockMode.pessimistic:
        return new ThemeData(
          brightness: ThemeBrightness.dark,
          accentColor: Colors.redAccent[200]
        );
75
    }
76
  }
77

78
  Route _getRoute(RouteSettings settings) {
79
    List<String> path = settings.name.split('/');
Hixie's avatar
Hixie committed
80 81 82 83 84
    if (path[0] != '')
      return null;
    if (path[1] == 'stock') {
      if (path.length != 3)
        return null;
85 86 87 88 89 90
      if (_stocks.containsKey(path[2])) {
        return new MaterialPageRoute(
          settings: settings,
          builder: (BuildContext context) => new StockSymbolPage(stock: _stocks[path[2]])
        );
      }
Hixie's avatar
Hixie committed
91 92 93 94
    }
    return null;
  }

95
  Future<LocaleQueryData> _onLocaleChanged(Locale locale) async {
96
    String localeString = locale.toString();
Seth Ladd's avatar
Seth Ladd committed
97 98 99
    await initializeMessages(localeString);
    Intl.defaultLocale = localeString;
    return StockStrings.instance;
100 101
  }

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

void main() {
128
  runApp(new StocksApp());
129
}