Hixie 90a0f6300f Simplify the usage of Navigator's routes argument
(These are changes cherry-picked from in-flight branches since they are
more independent and could be helpful even without those changes.)

- Change RouteBuilder's signature to take a single argument in which the
  other fields are placed, so that we can keep iterating on those
  arguments without having to break compatibility each time. Also, this
  makes defining route builders much simpler (only one argument to
  ignore rather than a variable number).

- Expose the next performance to RouteBuilders, since sometimes the
  route itself might not be where it's used.

- Allow BuildContext to be used to walk children, just like it can for
  ancestors

- Allow BuildContext to be used to get the Widget of the current
  BuildContext

- Allow StatefulComponentElement to be referenced with a type
  specialisation so that you don't have to cast when you know what the
  type you're dealing with actually is.
2015-10-05 13:59:30 -07:00

107 lines
2.7 KiB
Dart

// 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.
library stocks;
import 'dart:async';
import 'dart:math' as math;
import 'dart:sky' as sky;
import 'package:sky/animation.dart';
import 'package:sky/gestures.dart';
import 'package:sky/material.dart';
import 'package:sky/painting.dart';
import 'package:sky/widgets.dart';
import 'stock_data.dart';
part 'stock_arrow.dart';
part 'stock_home.dart';
part 'stock_list.dart';
part 'stock_menu.dart';
part 'stock_row.dart';
part 'stock_settings.dart';
part 'stock_symbol_viewer.dart';
part 'stock_types.dart';
class StocksApp extends StatefulComponent {
StocksAppState createState() => new StocksAppState();
}
class StocksAppState extends State<StocksApp> {
final Map<String, Stock> _stocks = <String, Stock>{};
final List<String> _symbols = <String>[];
void initState() {
super.initState();
new StockDataFetcher((StockData data) {
setState(() {
data.appendTo(_stocks, _symbols);
});
});
}
StockMode _optimismSetting = StockMode.optimistic;
BackupMode _backupSetting = BackupMode.disabled;
void modeUpdater(StockMode optimism) {
setState(() {
_optimismSetting = optimism;
});
}
void settingsUpdater({ StockMode optimism, BackupMode backup }) {
setState(() {
if (optimism != null)
_optimismSetting = optimism;
if (backup != null)
_backupSetting = backup;
});
}
ThemeData get theme {
switch (_optimismSetting) {
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]
);
}
}
RouteBuilder _getRoute(String name) {
List<String> path = name.split('/');
if (path[0] != '')
return null;
if (path[1] == 'stock') {
if (path.length != 3)
return null;
if (_stocks.containsKey(path[2]))
return (RouteArguments args) => new StockSymbolViewer(args.navigator, _stocks[path[2]]);
return null;
}
return null;
}
Widget build(BuildContext context) {
return new App(
title: 'Stocks',
theme: theme,
routes: <String, RouteBuilder>{
'/': (RouteArguments args) => new StockHome(args.navigator, _stocks, _symbols, _optimismSetting, modeUpdater),
'/settings': (RouteArguments args) => new StockSettings(args.navigator, _optimismSetting, _backupSetting, settingsUpdater)
},
onGenerateRoute: _getRoute
);
}
}
void main() {
runApp(new StocksApp());
}