Skip to content

Nav refactor stock #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions dev/benchmarks/test_apps/stocks/lib/i18n/stock_strings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@ class _StockStringsDelegate extends LocalizationsDelegate<StockStrings> {
}

StockStrings _lookupStockStrings(Locale locale) {


// Lookup logic when language+country codes are specified.
switch (locale.languageCode) {
case 'en': {
Expand Down
108 changes: 3 additions & 105 deletions dev/benchmarks/test_apps/stocks/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,110 +4,8 @@

library stocks;

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart' show
debugPaintSizeEnabled,
debugPaintBaselinesEnabled,
debugPaintLayerBordersEnabled,
debugPaintPointersEnabled,
debugRepaintRainbowEnabled;
import 'package:flutter/widgets.dart';

import 'i18n/stock_strings.dart';
import 'stock_data.dart';
import 'stock_home.dart';
import 'stock_settings.dart';
import 'stock_symbol_viewer.dart';
import 'stock_types.dart';
import 'stocks_app.dart';

class StocksApp extends StatefulWidget {
@override
StocksAppState createState() => StocksAppState();
}

class StocksAppState extends State<StocksApp> {
StockData stocks;

StockConfiguration _configuration = StockConfiguration(
stockMode: StockMode.optimistic,
backupMode: BackupMode.enabled,
debugShowGrid: false,
debugShowSizes: false,
debugShowBaselines: false,
debugShowLayers: false,
debugShowPointers: false,
debugShowRainbow: false,
showPerformanceOverlay: false,
showSemanticsDebugger: false,
);

@override
void initState() {
super.initState();
stocks = StockData();
}

void configurationUpdater(StockConfiguration value) {
setState(() {
_configuration = value;
});
}

ThemeData get theme {
switch (_configuration.stockMode) {
case StockMode.optimistic:
return ThemeData(
brightness: Brightness.light,
primarySwatch: Colors.purple,
);
case StockMode.pessimistic:
return ThemeData(
brightness: Brightness.dark,
accentColor: Colors.redAccent,
);
}
assert(_configuration.stockMode != null);
return null;
}

Route<dynamic> _getRoute(RouteSettings settings) {
if (settings.name == '/stock') {
final String symbol = settings.arguments as String;
return MaterialPageRoute<void>(
settings: settings,
builder: (BuildContext context) => StockSymbolPage(symbol: symbol, stocks: stocks),
);
}
// The other paths we support are in the routes table.
return null;
}

@override
Widget build(BuildContext context) {
assert(() {
debugPaintSizeEnabled = _configuration.debugShowSizes;
debugPaintBaselinesEnabled = _configuration.debugShowBaselines;
debugPaintLayerBordersEnabled = _configuration.debugShowLayers;
debugPaintPointersEnabled = _configuration.debugShowPointers;
debugRepaintRainbowEnabled = _configuration.debugShowRainbow;
return true;
}());
return MaterialApp(
title: 'Stocks',
theme: theme,
localizationsDelegates: StockStrings.localizationsDelegates,
supportedLocales: StockStrings.supportedLocales,
debugShowMaterialGrid: _configuration.debugShowGrid,
showPerformanceOverlay: _configuration.showPerformanceOverlay,
showSemanticsDebugger: _configuration.showSemanticsDebugger,
routes: <String, WidgetBuilder>{
'/': (BuildContext context) => StockHome(stocks, _configuration, configurationUpdater),
'/settings': (BuildContext context) => StockSettings(_configuration, configurationUpdater),
},
onGenerateRoute: _getRoute,
);
}
}

void main() {
runApp(StocksApp());
}
void main() => runApp(const StocksApp());
39 changes: 26 additions & 13 deletions dev/benchmarks/test_apps/stocks/lib/stock_home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import 'package:flutter/gestures.dart' show DragStartBehavior;
import 'i18n/stock_strings.dart';
import 'stock_data.dart';
import 'stock_list.dart';
import 'stock_state.dart';
import 'stock_symbol_viewer.dart';
import 'stock_types.dart';
import 'stocks_app.dart';

typedef ModeUpdater = void Function(StockMode mode);

Expand Down Expand Up @@ -52,11 +54,9 @@ class _NotImplementedDialog extends StatelessWidget {
}

class StockHome extends StatefulWidget {
const StockHome(this.stocks, this.configuration, this.updater);
const StockHome(this.configuration);

final StockData stocks;
final StockConfiguration configuration;
final ValueChanged<StockConfiguration> updater;

@override
StockHomeState createState() => StockHomeState();
Expand All @@ -83,8 +83,7 @@ class StockHomeState extends State<StockHome> {
}

void _handleStockModeChange(StockMode value) {
if (widget.updater != null)
widget.updater(widget.configuration.copyWith(stockMode: value));
StocksApp.updateConfigurationOf(context, StocksApp.configurationOf(context).copyWith(stockMode: value));
}

void _handleStockMenu(BuildContext context, _StockMenuItem value) {
Expand Down Expand Up @@ -181,7 +180,10 @@ class StockHomeState extends State<StockHome> {
}

void _handleShowSettings() {
Navigator.popAndPushNamed(context, '/settings');
// Removes the opened drawer.
Navigator.of(context).pop();
final StockState state = StockStateScope.of(context);
state.routePath = const StockSettingsPath();
}

void _handleShowAbout() {
Expand Down Expand Up @@ -232,7 +234,7 @@ class StockHomeState extends State<StockHome> {

static Iterable<Stock> _getStockList(StockData stocks, Iterable<String> symbols) {
return symbols.map<Stock>((String symbol) => stocks[symbol])
.where((Stock stock) => stock != null);
.where((Stock stock) => stock != null);
}

Iterable<Stock> _filterBySearchQuery(Iterable<Stock> stocks) {
Expand Down Expand Up @@ -263,20 +265,21 @@ class StockHomeState extends State<StockHome> {
stocks: stocks.toList(),
onAction: _buyStock,
onOpen: (Stock stock) {
Navigator.pushNamed(context, '/stock', arguments: stock.symbol);
final StockState state = StockStateScope.of(context);
state.routePath = StockSymbolPath(stock.symbol);
},
onShow: (Stock stock) {
_scaffoldKey.currentState.showBottomSheet<void>((BuildContext context) => StockSymbolBottomSheet(stock: stock));
},
);
}

Widget _buildStockTab(BuildContext context, StockHomeTab tab, List<String> stockSymbols) {
Widget _buildStockTab(BuildContext context, StockData stocks, StockHomeTab tab, List<String> stockSymbols) {
return AnimatedBuilder(
key: ValueKey<StockHomeTab>(tab),
animation: Listenable.merge(<Listenable>[_searchQuery, widget.stocks]),
animation: Listenable.merge(<Listenable>[_searchQuery, stocks]),
builder: (BuildContext context, Widget child) {
return _buildStockList(context, _filterBySearchQuery(_getStockList(widget.stocks, stockSymbols)).toList(), tab);
return _buildStockList(context, _filterBySearchQuery(_getStockList(stocks, stockSymbols)).toList(), tab);
},
);
}
Expand Down Expand Up @@ -317,6 +320,7 @@ class StockHomeState extends State<StockHome> {

@override
Widget build(BuildContext context) {
final StockData stocks = StockStateScope.of(context).stocks;
return DefaultTabController(
length: 2,
child: Scaffold(
Expand All @@ -328,8 +332,8 @@ class StockHomeState extends State<StockHome> {
body: TabBarView(
dragStartBehavior: DragStartBehavior.down,
children: <Widget>[
_buildStockTab(context, StockHomeTab.market, widget.stocks.allSymbols),
_buildStockTab(context, StockHomeTab.portfolio, portfolioSymbols),
_buildStockTab(context, stocks, StockHomeTab.market, stocks.allSymbols),
_buildStockTab(context, stocks, StockHomeTab.portfolio, portfolioSymbols),
],
),
),
Expand All @@ -355,3 +359,12 @@ class _CreateCompanySheet extends StatelessWidget {
);
}
}

class StockHomePage extends MaterialPage<void> {
StockHomePage(
StockConfiguration configuration,
) : super(
key: const ValueKey<String>('home'),
builder: (BuildContext context) => StockHome(configuration),
);
}
38 changes: 21 additions & 17 deletions dev/benchmarks/test_apps/stocks/lib/stock_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import 'package:flutter/material.dart';

import 'stock_types.dart';
import 'stocks_app.dart';

class StockSettings extends StatefulWidget {
const StockSettings(this.configuration, this.updater);
const StockSettings(this.configuration);

final StockConfiguration configuration;
final ValueChanged<StockConfiguration> updater;

@override
StockSettingsState createState() => StockSettingsState();
Expand All @@ -19,44 +19,44 @@ class StockSettings extends StatefulWidget {
class StockSettingsState extends State<StockSettings> {
void _handleOptimismChanged(bool value) {
value ??= false;
sendUpdates(widget.configuration.copyWith(stockMode: value ? StockMode.optimistic : StockMode.pessimistic));
StocksApp.updateConfigurationOf(context, StocksApp.configurationOf(context).copyWith(stockMode: value ? StockMode.optimistic : StockMode.pessimistic));
}

void _handleBackupChanged(bool value) {
sendUpdates(widget.configuration.copyWith(backupMode: value ? BackupMode.enabled : BackupMode.disabled));
StocksApp.updateConfigurationOf(context, StocksApp.configurationOf(context).copyWith(backupMode: value ? BackupMode.enabled : BackupMode.disabled));
}

void _handleShowGridChanged(bool value) {
sendUpdates(widget.configuration.copyWith(debugShowGrid: value));
StocksApp.updateConfigurationOf(context, StocksApp.configurationOf(context).copyWith(debugShowGrid: value));
}

void _handleShowSizesChanged(bool value) {
sendUpdates(widget.configuration.copyWith(debugShowSizes: value));
StocksApp.updateConfigurationOf(context, StocksApp.configurationOf(context).copyWith(debugShowSizes: value));
}

void _handleShowBaselinesChanged(bool value) {
sendUpdates(widget.configuration.copyWith(debugShowBaselines: value));
StocksApp.updateConfigurationOf(context, StocksApp.configurationOf(context).copyWith(debugShowBaselines: value));
}

void _handleShowLayersChanged(bool value) {
sendUpdates(widget.configuration.copyWith(debugShowLayers: value));
StocksApp.updateConfigurationOf(context, StocksApp.configurationOf(context).copyWith(debugShowLayers: value));
}

void _handleShowPointersChanged(bool value) {
sendUpdates(widget.configuration.copyWith(debugShowPointers: value));
StocksApp.updateConfigurationOf(context, StocksApp.configurationOf(context).copyWith(debugShowPointers: value));
}

void _handleShowRainbowChanged(bool value) {
sendUpdates(widget.configuration.copyWith(debugShowRainbow: value));
StocksApp.updateConfigurationOf(context, StocksApp.configurationOf(context).copyWith(debugShowRainbow: value));
}


void _handleShowPerformanceOverlayChanged(bool value) {
sendUpdates(widget.configuration.copyWith(showPerformanceOverlay: value));
StocksApp.updateConfigurationOf(context, StocksApp.configurationOf(context).copyWith(showPerformanceOverlay: value));
}

void _handleShowSemanticsDebuggerChanged(bool value) {
sendUpdates(widget.configuration.copyWith(showSemanticsDebugger: value));
StocksApp.updateConfigurationOf(context, StocksApp.configurationOf(context).copyWith(showSemanticsDebugger: value));
}

void _confirmOptimismChange() {
Expand Down Expand Up @@ -92,11 +92,6 @@ class StockSettingsState extends State<StockSettings> {
}
}

void sendUpdates(StockConfiguration value) {
if (widget.updater != null)
widget.updater(value);
}

AppBar buildAppBar(BuildContext context) {
return AppBar(
title: const Text('Settings'),
Expand Down Expand Up @@ -216,3 +211,12 @@ class StockSettingsState extends State<StockSettings> {
);
}
}

class StockSettingsPage extends MaterialPage<void> {
StockSettingsPage(
StockConfiguration configuration,
) : super(
key: const ValueKey<String>('settings'),
builder: (BuildContext context) => StockSettings(configuration),
);
}
52 changes: 52 additions & 0 deletions dev/benchmarks/test_apps/stocks/lib/stock_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/widgets.dart';

import 'stock_data.dart';

@immutable
abstract class StockRoutePath { const StockRoutePath(); }
class StockHomePath extends StockRoutePath { const StockHomePath(); }
class StockSettingsPath extends StockRoutePath { const StockSettingsPath(); }
class StockSymbolPath extends StockRoutePath {
const StockSymbolPath(this.symbol);
final String symbol;
}

class StockState extends ChangeNotifier {
StockState(this.stocks);

bool get debugInitialized {
bool answer;
assert((){
answer = routePath != null;
return true;
}());
return answer;
}

StockRoutePath get routePath => _routePath;
StockRoutePath _routePath;
set routePath(StockRoutePath value) {
if (value != _routePath) {
_routePath = value;
notifyListeners();
}
}

final StockData stocks;
}

class StockStateScope extends InheritedNotifier<StockState> {
const StockStateScope({
Key key,
StockState state,
Widget child,
}) : super(key: key, notifier: state, child: child);

static StockState of(BuildContext context) {
return context.dependOnInheritedWidgetOfExactType<StockStateScope>().notifier;
}
}
Loading