blob: cab5c17a11df229d1a340ba7ab2adfe881ad3b53 [file] [log] [blame]
Collin Jackson633b6502015-07-16 11:54:25 -07001// Copyright 2015 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Jim Simon30896862015-07-24 10:53:34 -07005library stocks;
6
Hixiea3ae46b2015-09-14 13:35:05 -07007import 'dart:async';
Jim Simon30896862015-07-24 10:53:34 -07008
Adam Barth65eba902015-10-09 20:44:52 -07009import 'package:flutter/material.dart';
Hixiea95c8662016-02-12 11:41:05 -080010import 'package:flutter/rendering.dart' show
11 debugPaintSizeEnabled,
12 debugPaintBaselinesEnabled,
13 debugPaintLayerBordersEnabled,
14 debugPaintPointersEnabled,
15 debugRepaintRainbowEnabled;
Hans Mullerc3d56b12017-10-11 16:01:13 -070016import 'package:flutter_localizations/flutter_localizations.dart';
Jim Simon30896862015-07-24 10:53:34 -070017
Ian Hicksona94999b2016-02-10 18:11:03 -080018import 'stock_data.dart';
19import 'stock_home.dart';
20import 'stock_settings.dart';
21import 'stock_strings.dart';
22import 'stock_symbol_viewer.dart';
23import 'stock_types.dart';
Collin Jackson633b6502015-07-16 11:54:25 -070024
Hans Muller8c2c5022017-08-23 17:07:59 -070025class _StocksLocalizationsDelegate extends LocalizationsDelegate<StockStrings> {
26 @override
27 Future<StockStrings> load(Locale locale) => StockStrings.load(locale);
28
29 @override
Hans Muller796c5432017-10-20 10:45:28 -070030 bool isSupported(Locale locale) => locale.languageCode == 'es' || locale.languageCode == 'en';
31
32 @override
Hans Muller8c2c5022017-08-23 17:07:59 -070033 bool shouldReload(_StocksLocalizationsDelegate old) => false;
34}
35
Adam Barth95fc5ae2016-03-12 12:13:16 -080036class StocksApp extends StatefulWidget {
Hixie797e27e2016-03-14 13:31:43 -070037 @override
Hixie0a0a92e2015-09-25 16:00:56 -070038 StocksAppState createState() => new StocksAppState();
39}
Collin Jackson633b6502015-07-16 11:54:25 -070040
Hixie0a0a92e2015-09-25 16:00:56 -070041class StocksAppState extends State<StocksApp> {
Ian Hickson9adb4a72017-06-23 14:58:29 -070042 StockData stocks;
Hixie0a0a92e2015-09-25 16:00:56 -070043
Ian Hickson34cfb602016-01-10 00:36:01 -080044 StockConfiguration _configuration = new StockConfiguration(
45 stockMode: StockMode.optimistic,
46 backupMode: BackupMode.enabled,
Ian Hickson48e13502016-01-10 15:43:23 -080047 debugShowGrid: false,
48 debugShowSizes: false,
Hixiea95c8662016-02-12 11:41:05 -080049 debugShowBaselines: false,
50 debugShowLayers: false,
51 debugShowPointers: false,
52 debugShowRainbow: false,
Hixie28a17882015-12-16 11:11:18 -080053 showPerformanceOverlay: false,
54 showSemanticsDebugger: false
Ian Hickson34cfb602016-01-10 00:36:01 -080055 );
56
Hixie797e27e2016-03-14 13:31:43 -070057 @override
Adam Barth347bd252015-09-30 13:15:46 -070058 void initState() {
59 super.initState();
Ian Hickson9adb4a72017-06-23 14:58:29 -070060 stocks = new StockData();
Collin Jackson633b6502015-07-16 11:54:25 -070061 }
62
Ian Hickson34cfb602016-01-10 00:36:01 -080063 void configurationUpdater(StockConfiguration value) {
Hixie0a0a92e2015-09-25 16:00:56 -070064 setState(() {
Ian Hickson34cfb602016-01-10 00:36:01 -080065 _configuration = value;
Hixie0a0a92e2015-09-25 16:00:56 -070066 });
67 }
Collin Jackson633b6502015-07-16 11:54:25 -070068
Hixie0a0a92e2015-09-25 16:00:56 -070069 ThemeData get theme {
Ian Hickson34cfb602016-01-10 00:36:01 -080070 switch (_configuration.stockMode) {
Hixie0a0a92e2015-09-25 16:00:56 -070071 case StockMode.optimistic:
72 return new ThemeData(
Todd Volkert7ac0ce72016-06-07 14:39:15 -070073 brightness: Brightness.light,
Hixie0a0a92e2015-09-25 16:00:56 -070074 primarySwatch: Colors.purple
75 );
76 case StockMode.pessimistic:
77 return new ThemeData(
Todd Volkert7ac0ce72016-06-07 14:39:15 -070078 brightness: Brightness.dark,
Alexandre Ardhuin578ca0a2017-03-21 23:14:55 +010079 accentColor: Colors.redAccent
Hixie0a0a92e2015-09-25 16:00:56 -070080 );
Collin Jackson633b6502015-07-16 11:54:25 -070081 }
pqf5a4e632016-06-14 15:19:14 -070082 assert(_configuration.stockMode != null);
pq7a955482016-06-14 12:13:56 -070083 return null;
Hixie0a0a92e2015-09-25 16:00:56 -070084 }
Collin Jackson633b6502015-07-16 11:54:25 -070085
Ian Hicksonaba03792018-03-22 13:21:07 -070086 Route<dynamic> _getRoute(RouteSettings settings) {
Ian Hickson9adb4a72017-06-23 14:58:29 -070087 // Routes, by convention, are split on slashes, like filesystem paths.
Chris Bracken7a52fb62017-03-03 18:05:27 -080088 final List<String> path = settings.name.split('/');
Ian Hickson9adb4a72017-06-23 14:58:29 -070089 // We only support paths that start with a slash, so bail if
90 // the first component is not empty:
Hixief2b7dd62015-09-29 09:37:53 -070091 if (path[0] != '')
92 return null;
Ian Hickson9adb4a72017-06-23 14:58:29 -070093 // If the path is "/stock:..." then show a stock page for the
94 // specified stock symbol.
95 if (path[1].startsWith('stock:')) {
96 // We don't yet support subpages of a stock, so bail if there's
97 // any more path components.
98 if (path.length != 2)
Hixief2b7dd62015-09-29 09:37:53 -070099 return null;
Ian Hickson9adb4a72017-06-23 14:58:29 -0700100 // Extract the symbol part of "stock:..." and return a route
101 // for that symbol.
102 final String symbol = path[1].substring(6);
Ian Hicksonaba03792018-03-22 13:21:07 -0700103 return new MaterialPageRoute<void>(
Ian Hickson9adb4a72017-06-23 14:58:29 -0700104 settings: settings,
105 builder: (BuildContext context) => new StockSymbolPage(symbol: symbol, stocks: stocks),
106 );
Hixief2b7dd62015-09-29 09:37:53 -0700107 }
Ian Hickson9adb4a72017-06-23 14:58:29 -0700108 // The other paths we support are in the routes table.
Hixief2b7dd62015-09-29 09:37:53 -0700109 return null;
110 }
111
Hixie797e27e2016-03-14 13:31:43 -0700112 @override
Hixie0a0a92e2015-09-25 16:00:56 -0700113 Widget build(BuildContext context) {
Ian Hickson48e13502016-01-10 15:43:23 -0800114 assert(() {
115 debugPaintSizeEnabled = _configuration.debugShowSizes;
Hixiea95c8662016-02-12 11:41:05 -0800116 debugPaintBaselinesEnabled = _configuration.debugShowBaselines;
117 debugPaintLayerBordersEnabled = _configuration.debugShowLayers;
118 debugPaintPointersEnabled = _configuration.debugShowPointers;
119 debugRepaintRainbowEnabled = _configuration.debugShowRainbow;
Ian Hickson48e13502016-01-10 15:43:23 -0800120 return true;
Alexandre Ardhuin2958d7d2017-09-21 09:33:01 +0200121 }());
Adam Barthdb3b9e82015-10-09 10:19:35 -0700122 return new MaterialApp(
Hixie0a0a92e2015-09-25 16:00:56 -0700123 title: 'Stocks',
124 theme: theme,
Hans Mullerc3d56b12017-10-11 16:01:13 -0700125 localizationsDelegates: <LocalizationsDelegate<dynamic>>[
Hans Muller8c2c5022017-08-23 17:07:59 -0700126 new _StocksLocalizationsDelegate(),
Hans Mullerc3d56b12017-10-11 16:01:13 -0700127 GlobalMaterialLocalizations.delegate,
128 GlobalWidgetsLocalizations.delegate,
Hans Muller8c2c5022017-08-23 17:07:59 -0700129 ],
Hans Muller4262c1e2017-09-08 16:47:40 -0700130 supportedLocales: const <Locale>[
Alexandre Ardhuineda03e22018-08-02 12:02:32 +0200131 Locale('en', 'US'),
132 Locale('es', 'ES'),
Hans Muller4262c1e2017-09-08 16:47:40 -0700133 ],
Ian Hickson48e13502016-01-10 15:43:23 -0800134 debugShowMaterialGrid: _configuration.debugShowGrid,
Ian Hickson88c43c32016-01-10 23:39:20 -0800135 showPerformanceOverlay: _configuration.showPerformanceOverlay,
Hixie28a17882015-12-16 11:11:18 -0800136 showSemanticsDebugger: _configuration.showSemanticsDebugger,
Adam Barth9b9ad3d2016-03-12 17:01:45 -0800137 routes: <String, WidgetBuilder>{
Ian Hickson9adb4a72017-06-23 14:58:29 -0700138 '/': (BuildContext context) => new StockHome(stocks, _configuration, configurationUpdater),
Adam Barth9b9ad3d2016-03-12 17:01:45 -0800139 '/settings': (BuildContext context) => new StockSettings(_configuration, configurationUpdater)
Hixief2b7dd62015-09-29 09:37:53 -0700140 },
Jason Simmons9693cd52015-12-07 11:19:15 -0800141 onGenerateRoute: _getRoute,
Hixie0a0a92e2015-09-25 16:00:56 -0700142 );
143 }
Collin Jackson633b6502015-07-16 11:54:25 -0700144}
145
146void main() {
Jason Simmons9693cd52015-12-07 11:19:15 -0800147 runApp(new StocksApp());
Collin Jackson633b6502015-07-16 11:54:25 -0700148}