main.dart 3.88 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
  StocksAppState createState() => StocksAppState();
39
}
40

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

44
  StockConfiguration _configuration = StockConfiguration(
45 46
    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
    showPerformanceOverlay: false,
54
    showSemanticsDebugger: false,
55 56
  );

57
  @override
58 59
  void initState() {
    super.initState();
60
    stocks = 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
      case StockMode.optimistic:
72
        return ThemeData(
73
          brightness: Brightness.light,
74
          primarySwatch: Colors.purple,
75 76
        );
      case StockMode.pessimistic:
77
        return 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<dynamic> _getRoute(RouteSettings settings) {
87
    if (settings.name == '/stock') {
88
      final String symbol = settings.arguments;
89
      return MaterialPageRoute<void>(
90
        settings: settings,
91
        builder: (BuildContext context) => StockSymbolPage(symbol: symbol, stocks: stocks),
92
      );
Hixie's avatar
Hixie committed
93
    }
94
    // The other paths we support are in the routes table.
Hixie's avatar
Hixie committed
95 96 97
    return null;
  }

98
  @override
99
  Widget build(BuildContext context) {
100 101
    assert(() {
      debugPaintSizeEnabled = _configuration.debugShowSizes;
102 103 104 105
      debugPaintBaselinesEnabled = _configuration.debugShowBaselines;
      debugPaintLayerBordersEnabled = _configuration.debugShowLayers;
      debugPaintPointersEnabled = _configuration.debugShowPointers;
      debugRepaintRainbowEnabled = _configuration.debugShowRainbow;
106
      return true;
107
    }());
108
    return MaterialApp(
109 110
      title: 'Stocks',
      theme: theme,
111
      localizationsDelegates: <LocalizationsDelegate<dynamic>>[
112
        _StocksLocalizationsDelegate(),
113 114
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
115
      ],
116
      supportedLocales: const <Locale>[
117 118
        Locale('en', 'US'),
        Locale('es', 'ES'),
119
      ],
120
      debugShowMaterialGrid: _configuration.debugShowGrid,
121
      showPerformanceOverlay: _configuration.showPerformanceOverlay,
Hixie's avatar
Hixie committed
122
      showSemanticsDebugger: _configuration.showSemanticsDebugger,
123
      routes: <String, WidgetBuilder>{
124
         '/':         (BuildContext context) => StockHome(stocks, _configuration, configurationUpdater),
125
         '/settings': (BuildContext context) => StockSettings(_configuration, configurationUpdater),
Hixie's avatar
Hixie committed
126
      },
127
      onGenerateRoute: _getRoute,
128 129
    );
  }
130 131 132
}

void main() {
133
  runApp(StocksApp());
134
}