main.dart 3.19 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
import 'package:flutter/rendering.dart' show debugPaintSizeEnabled;
11
import 'package:intl/intl.dart';
12

13
import 'i18n/stock_messages_all.dart';
14 15 16 17 18 19
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';
20

21 22 23
class StocksApp extends StatefulComponent {
  StocksAppState createState() => new StocksAppState();
}
24

25
class StocksAppState extends State<StocksApp> {
26

Hixie's avatar
Hixie committed
27 28
  final Map<String, Stock> _stocks = <String, Stock>{};
  final List<String> _symbols = <String>[];
29

30 31 32
  StockConfiguration _configuration = new StockConfiguration(
    stockMode: StockMode.optimistic,
    backupMode: BackupMode.enabled,
33 34
    debugShowGrid: false,
    debugShowSizes: false,
Hixie's avatar
Hixie committed
35 36
    showPerformanceOverlay: false,
    showSemanticsDebugger: false
37 38
  );

39 40
  void initState() {
    super.initState();
41
    new StockDataFetcher((StockData data) {
42
      setState(() {
Hixie's avatar
Hixie committed
43
        data.appendTo(_stocks, _symbols);
44
      });
45
    });
46 47
  }

48
  void configurationUpdater(StockConfiguration value) {
49
    setState(() {
50
      _configuration = value;
51 52 53
    });
  }

54
  ThemeData get theme {
55
    switch (_configuration.stockMode) {
56 57 58 59 60 61 62 63 64 65
      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]
        );
66
    }
67
  }
68

69
  Route _getRoute(RouteSettings settings) {
70
    List<String> path = settings.name.split('/');
Hixie's avatar
Hixie committed
71 72 73 74 75
    if (path[0] != '')
      return null;
    if (path[1] == 'stock') {
      if (path.length != 3)
        return null;
76 77 78 79 80 81
      if (_stocks.containsKey(path[2])) {
        return new MaterialPageRoute(
          settings: settings,
          builder: (BuildContext context) => new StockSymbolPage(stock: _stocks[path[2]])
        );
      }
Hixie's avatar
Hixie committed
82 83 84 85
    }
    return null;
  }

86
  Future<LocaleQueryData> _onLocaleChanged(Locale locale) async {
87
    String localeString = locale.toString();
Seth Ladd's avatar
Seth Ladd committed
88 89 90
    await initializeMessages(localeString);
    Intl.defaultLocale = localeString;
    return StockStrings.instance;
91 92
  }

93
  Widget build(BuildContext context) {
94 95 96 97
    assert(() {
      debugPaintSizeEnabled = _configuration.debugShowSizes;
      return true;
    });
Adam Barth's avatar
Adam Barth committed
98
    return new MaterialApp(
99 100
      title: 'Stocks',
      theme: theme,
101
      debugShowMaterialGrid: _configuration.debugShowGrid,
102
      showPerformanceOverlay: _configuration.showPerformanceOverlay,
Hixie's avatar
Hixie committed
103
      showSemanticsDebugger: _configuration.showSemanticsDebugger,
104
      routes: <String, RouteBuilder>{
105 106
         '/':         (RouteArguments args) => new StockHome(_stocks, _symbols, _configuration, configurationUpdater),
         '/settings': (RouteArguments args) => new StockSettings(_configuration, configurationUpdater)
Hixie's avatar
Hixie committed
107
      },
108 109
      onGenerateRoute: _getRoute,
      onLocaleChanged: _onLocaleChanged
110 111
    );
  }
112 113 114
}

void main() {
115
  runApp(new StocksApp());
116
}