main.dart 3.45 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,
Hixie's avatar
Hixie committed
46 47
    showPerformanceOverlay: false,
    showSemanticsDebugger: false
48 49
  );

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 69 70 71 72 73 74 75 76
      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]
        );
77
    }
78
  }
79

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

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

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

void main() {
126
  runApp(new StocksApp());
127
}