main.dart 3.35 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
import 'dart:collection';
9
import 'dart:math' as math;
10
import 'dart:ui' as ui;
11

12 13 14
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
Hixie's avatar
Hixie committed
15
import 'package:flutter/rendering.dart';
16
import 'package:flutter/scheduler.dart';
17
import 'package:intl/intl.dart';
18

19
import 'stock_data.dart';
20
import 'i18n/stock_messages_all.dart';
21

22 23 24 25 26 27
part 'stock_arrow.dart';
part 'stock_home.dart';
part 'stock_list.dart';
part 'stock_menu.dart';
part 'stock_row.dart';
part 'stock_settings.dart';
28
part 'stock_strings.dart';
Hixie's avatar
Hixie committed
29
part 'stock_symbol_viewer.dart';
30
part 'stock_types.dart';
31

32 33 34
class StocksApp extends StatefulComponent {
  StocksAppState createState() => new StocksAppState();
}
35

36
class StocksAppState extends State<StocksApp> {
37

Hixie's avatar
Hixie committed
38 39
  final Map<String, Stock> _stocks = <String, Stock>{};
  final List<String> _symbols = <String>[];
40

41 42 43
  StockConfiguration _configuration = new StockConfiguration(
    stockMode: StockMode.optimistic,
    backupMode: BackupMode.enabled,
44 45
    debugShowGrid: false,
    debugShowSizes: false,
46
    showPerformanceOverlay: false
47 48
  );

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

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

64
  ThemeData get theme {
65
    switch (_configuration.stockMode) {
66 67 68 69 70 71 72 73 74 75
      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]
        );
76
    }
77
  }
78

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

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

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

void main() {
124
  runApp(new StocksApp());
125
}