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

18 19 20 21 22 23
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';
24

25 26 27 28
class _StocksLocalizationsDelegate extends LocalizationsDelegate<StockStrings> {
  @override
  Future<StockStrings> load(Locale locale) => StockStrings.load(locale);

29 30 31
  @override
  bool isSupported(Locale locale) => locale.languageCode == 'es' || locale.languageCode == 'en';

32 33 34 35
  @override
  bool shouldReload(_StocksLocalizationsDelegate old) => false;
}

36
class StocksApp extends StatefulWidget {
37
  @override
38 39
  StocksAppState createState() => new StocksAppState();
}
40

41
class StocksAppState extends State<StocksApp> {
42
  StockData stocks;
43

44 45 46
  StockConfiguration _configuration = new StockConfiguration(
    stockMode: StockMode.optimistic,
    backupMode: BackupMode.enabled,
47 48
    debugShowGrid: false,
    debugShowSizes: false,
49 50 51 52
    debugShowBaselines: false,
    debugShowLayers: false,
    debugShowPointers: false,
    debugShowRainbow: false,
Hixie's avatar
Hixie committed
53 54
    showPerformanceOverlay: false,
    showSemanticsDebugger: false
55 56
  );

57
  @override
58 59
  void initState() {
    super.initState();
60
    stocks = new StockData();
61 62
  }

63
  void configurationUpdater(StockConfiguration value) {
64
    setState(() {
65
      _configuration = value;
66 67 68
    });
  }

69
  ThemeData get theme {
70
    switch (_configuration.stockMode) {
71 72
      case StockMode.optimistic:
        return new ThemeData(
73
          brightness: Brightness.light,
74 75 76 77
          primarySwatch: Colors.purple
        );
      case StockMode.pessimistic:
        return new ThemeData(
78
          brightness: Brightness.dark,
79
          accentColor: Colors.redAccent
80
        );
81
    }
pq's avatar
pq committed
82
    assert(_configuration.stockMode != null);
pq's avatar
pq committed
83
    return null;
84
  }
85

86
  Route<Null> _getRoute(RouteSettings settings) {
87
    // Routes, by convention, are split on slashes, like filesystem paths.
88
    final List<String> path = settings.name.split('/');
89 90
    // We only support paths that start with a slash, so bail if
    // the first component is not empty:
Hixie's avatar
Hixie committed
91 92
    if (path[0] != '')
      return null;
93 94 95 96 97 98
    // If the path is "/stock:..." then show a stock page for the
    // specified stock symbol.
    if (path[1].startsWith('stock:')) {
      // We don't yet support subpages of a stock, so bail if there's
      // any more path components.
      if (path.length != 2)
Hixie's avatar
Hixie committed
99
        return null;
100 101 102 103 104 105 106
      // Extract the symbol part of "stock:..." and return a route
      // for that symbol.
      final String symbol = path[1].substring(6);
      return new MaterialPageRoute<Null>(
        settings: settings,
        builder: (BuildContext context) => new StockSymbolPage(symbol: symbol, stocks: stocks),
      );
Hixie's avatar
Hixie committed
107
    }
108
    // The other paths we support are in the routes table.
Hixie's avatar
Hixie committed
109 110 111
    return null;
  }

112
  @override
113
  Widget build(BuildContext context) {
114 115
    assert(() {
      debugPaintSizeEnabled = _configuration.debugShowSizes;
116 117 118 119
      debugPaintBaselinesEnabled = _configuration.debugShowBaselines;
      debugPaintLayerBordersEnabled = _configuration.debugShowLayers;
      debugPaintPointersEnabled = _configuration.debugShowPointers;
      debugRepaintRainbowEnabled = _configuration.debugShowRainbow;
120
      return true;
121
    }());
Adam Barth's avatar
Adam Barth committed
122
    return new MaterialApp(
123 124
      title: 'Stocks',
      theme: theme,
125
      localizationsDelegates: <LocalizationsDelegate<dynamic>>[
126
        new _StocksLocalizationsDelegate(),
127 128
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
129
      ],
130 131 132 133
      supportedLocales: const <Locale>[
        const Locale('en', 'US'),
        const Locale('es', 'ES'),
      ],
134
      debugShowMaterialGrid: _configuration.debugShowGrid,
135
      showPerformanceOverlay: _configuration.showPerformanceOverlay,
Hixie's avatar
Hixie committed
136
      showSemanticsDebugger: _configuration.showSemanticsDebugger,
137
      routes: <String, WidgetBuilder>{
138
         '/':         (BuildContext context) => new StockHome(stocks, _configuration, configurationUpdater),
139
         '/settings': (BuildContext context) => new StockSettings(_configuration, configurationUpdater)
Hixie's avatar
Hixie committed
140
      },
141
      onGenerateRoute: _getRoute,
142 143
    );
  }
144 145 146
}

void main() {
147
  runApp(new StocksApp());
148
}