Track scroll position

- Change RouteArguments to pass the route's BuildContext rather than
  the Navigator. This caused the bulk of the examples/ and .../test/
  changes (those are mostly mechanical changes). It also meant I could
  simplify Navigator.of().

- Make initState() actually get called when the State's Element is in
  the tree, so you can use Foo.of() functions there. Added a test for
  this also.

- Provide a RouteWidget so that routes have a position in the Widget
  tree. The bulk of the route logic is still in a longer-lived Route
  object for now.

- Make Route.setState() only rebuild the actual route, not the whole
  navigator.

- Provided a Route.of().

- Provided a Route.writeState / Route.readState API that tries to
  identify the clients by their runtimeType, their key, and their
  ancestors keys, up to the nearest ancestor with a GlobalKey.

- Made scrollables hook into this API to track state. Added a test to
  make sure this works.

- Fix the debug output of GestureDetector and the hashCode of
  MixedViewport.

- Fixed ScrollableWidgetListState<T> to handle infinite lists.
This commit is contained in:
Ian Hickson
2015-10-26 22:20:30 -07:00
committed by Hixie
parent e77cad81c1
commit 8a900f9042
25 changed files with 385 additions and 133 deletions

View File

@@ -44,9 +44,8 @@ class DialogMenuItem extends StatelessComponent {
}
class FeedFragment extends StatefulComponent {
FeedFragment({ this.navigator, this.userData, this.onItemCreated, this.onItemDeleted });
FeedFragment({ this.userData, this.onItemCreated, this.onItemDeleted });
final NavigatorState navigator;
final UserData userData;
final FitnessItemHandler onItemCreated;
final FitnessItemHandler onItemDeleted;
@@ -62,7 +61,7 @@ class FeedFragmentState extends State<FeedFragment> {
setState(() {
_fitnessMode = value;
});
config.navigator.pop();
Navigator.of(context).pop();
}
void _showDrawer() {
@@ -93,8 +92,8 @@ class FeedFragmentState extends State<FeedFragment> {
}
void _handleShowSettings() {
config.navigator.pop();
config.navigator.pushNamed('/settings');
Navigator.of(context)..pop()
..pushNamed('/settings');
}
// TODO(jackson): We should be localizing
@@ -122,7 +121,7 @@ class FeedFragmentState extends State<FeedFragment> {
content: new Text("Item deleted."),
actions: <SnackBarAction>[new SnackBarAction(label: "UNDO", onPressed: () {
config.onItemCreated(item);
config.navigator.pop();
Navigator.of(context).pop();
})]
);
}
@@ -193,7 +192,7 @@ class FeedFragmentState extends State<FeedFragment> {
void _handleActionButtonPressed() {
showDialog(context: context, child: new AddItemDialog()).then((routeName) {
if (routeName != null)
config.navigator.pushNamed(routeName);
Navigator.of(context).pushNamed(routeName);
});
}

View File

@@ -135,7 +135,6 @@ class FitnessAppState extends State<FitnessApp> {
routes: <String, RouteBuilder>{
'/': (RouteArguments args) {
return new FeedFragment(
navigator: args.navigator,
userData: _userData,
onItemCreated: _handleItemCreated,
onItemDeleted: _handleItemDeleted
@@ -143,19 +142,16 @@ class FitnessAppState extends State<FitnessApp> {
},
'/meals/new': (RouteArguments args) {
return new MealFragment(
navigator: args.navigator,
onCreated: _handleItemCreated
);
},
'/measurements/new': (RouteArguments args) {
return new MeasurementFragment(
navigator: args.navigator,
onCreated: _handleItemCreated
);
},
'/settings': (RouteArguments args) {
return new SettingsFragment(
navigator: args.navigator,
userData: _userData,
updater: settingsUpdater
);

View File

@@ -43,9 +43,8 @@ class MealRow extends FitnessItemRow {
}
class MealFragment extends StatefulComponent {
MealFragment({ this.navigator, this.onCreated });
MealFragment({ this.onCreated });
NavigatorState navigator;
FitnessItemHandler onCreated;
MealFragmentState createState() => new MealFragmentState();
@@ -56,14 +55,14 @@ class MealFragmentState extends State<MealFragment> {
void _handleSave() {
config.onCreated(new Meal(when: new DateTime.now(), description: _description));
config.navigator.pop();
Navigator.of(context).pop();
}
Widget buildToolBar() {
return new ToolBar(
left: new IconButton(
icon: "navigation/close",
onPressed: config.navigator.pop),
onPressed: Navigator.of(context).pop),
center: new Text('New Meal'),
right: <Widget>[
// TODO(abarth): Should this be a FlatButton?

View File

@@ -104,9 +104,8 @@ class MeasurementDateDialogState extends State<MeasurementDateDialog> {
}
class MeasurementFragment extends StatefulComponent {
MeasurementFragment({ this.navigator, this.onCreated });
MeasurementFragment({ this.onCreated });
final NavigatorState navigator;
final FitnessItemHandler onCreated;
MeasurementFragmentState createState() => new MeasurementFragmentState();
@@ -131,14 +130,14 @@ class MeasurementFragmentState extends State<MeasurementFragment> {
);
}
config.onCreated(new Measurement(when: _when, weight: parsedWeight));
config.navigator.pop();
Navigator.of(context).pop();
}
Widget buildToolBar() {
return new ToolBar(
left: new IconButton(
icon: "navigation/close",
onPressed: config.navigator.pop),
onPressed: Navigator.of(context).pop),
center: new Text('New Measurement'),
right: <Widget>[
// TODO(abarth): Should this be a FlatButton?

View File

@@ -10,9 +10,8 @@ typedef void SettingsUpdater({
});
class SettingsFragment extends StatefulComponent {
SettingsFragment({ this.navigator, this.userData, this.updater });
SettingsFragment({ this.userData, this.updater });
final NavigatorState navigator;
final UserData userData;
final SettingsUpdater updater;
@@ -29,7 +28,7 @@ class SettingsFragmentState extends State<SettingsFragment> {
return new ToolBar(
left: new IconButton(
icon: "navigation/arrow_back",
onPressed: config.navigator.pop
onPressed: () => Navigator.of(context).pop()
),
center: new Text('Settings')
);
@@ -48,7 +47,7 @@ class SettingsFragmentState extends State<SettingsFragment> {
void _handleGoalWeightChanged(String goalWeight) {
// TODO(jackson): Looking for null characters to detect enter key is a hack
if (goalWeight.endsWith("\u{0}")) {
config.navigator.pop(double.parse(goalWeight.replaceAll("\u{0}", "")));
Navigator.of(context).pop(double.parse(goalWeight.replaceAll("\u{0}", "")));
} else {
setState(() {
try {

View File

@@ -108,10 +108,10 @@ class GameDemoState extends State<GameDemo> {
_sounds,
(int lastScore) {
setState(() { _lastScore = lastScore; });
args.navigator.pop();
Navigator.of(args.context).pop();
}
);
args.navigator.pushNamed('/game');
Navigator.of(args.context).pushNamed('/game');
},
texture: _spriteSheetUI['btn_play_up.png'],
textureDown: _spriteSheetUI['btn_play_down.png'],

View File

@@ -92,8 +92,8 @@ class StocksAppState extends State<StocksApp> {
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)
'/': (RouteArguments args) => new StockHome(_stocks, _symbols, _optimismSetting, modeUpdater),
'/settings': (RouteArguments args) => new StockSettings(_optimismSetting, _backupSetting, settingsUpdater)
},
onGenerateRoute: _getRoute
);

View File

@@ -7,9 +7,8 @@ part of stocks;
typedef void ModeUpdater(StockMode mode);
class StockHome extends StatefulComponent {
StockHome(this.navigator, this.stocks, this.symbols, this.stockMode, this.modeUpdater);
StockHome(this.stocks, this.symbols, this.stockMode, this.modeUpdater);
final NavigatorState navigator;
final Map<String, Stock> stocks;
final List<String> symbols;
final StockMode stockMode;
@@ -25,7 +24,7 @@ class StockHomeState extends State<StockHome> {
String _searchQuery;
void _handleSearchBegin() {
config.navigator.pushState(this, (_) {
Navigator.of(context).pushState(this, (_) {
setState(() {
_isSearching = false;
_searchQuery = null;
@@ -38,10 +37,10 @@ class StockHomeState extends State<StockHome> {
void _handleSearchEnd() {
assert(() {
final StateRoute currentRoute = config.navigator.currentRoute;
final StateRoute currentRoute = Navigator.of(context).currentRoute;
return currentRoute.owner == this;
});
config.navigator.pop();
Navigator.of(context).pop();
}
void _handleSearchQueryChanged(String query) {
@@ -92,13 +91,13 @@ class StockHomeState extends State<StockHome> {
new FlatButton(
child: new Text('USE IT'),
onPressed: () {
config.navigator.pop(false);
Navigator.of(context).pop(false);
}
),
new FlatButton(
child: new Text('OH WELL'),
onPressed: () {
config.navigator.pop(false);
Navigator.of(context).pop(false);
}
),
]
@@ -142,8 +141,8 @@ class StockHomeState extends State<StockHome> {
}
void _handleShowSettings() {
config.navigator.pop();
config.navigator.pushNamed('/settings');
Navigator.of(context)..pop()
..pushNamed('/settings');
}
Widget buildToolBar() {
@@ -193,7 +192,7 @@ class StockHomeState extends State<StockHome> {
onOpen: (Stock stock, Key arrowKey) {
Set<Key> mostValuableKeys = new Set<Key>();
mostValuableKeys.add(arrowKey);
config.navigator.pushNamed('/stock/${stock.symbol}', mostValuableKeys: mostValuableKeys);
Navigator.of(context).pushNamed('/stock/${stock.symbol}', mostValuableKeys: mostValuableKeys);
}
);
}
@@ -239,7 +238,7 @@ class StockHomeState extends State<StockHome> {
}
void _handleUndo() {
config.navigator.pop();
Navigator.of(context).pop();
}
void _handleStockPurchased() {

View File

@@ -10,9 +10,8 @@ typedef void SettingsUpdater({
});
class StockSettings extends StatefulComponent {
const StockSettings(this.navigator, this.optimism, this.backup, this.updater);
const StockSettings(this.optimism, this.backup, this.updater);
final NavigatorState navigator;
final StockMode optimism;
final BackupMode backup;
final SettingsUpdater updater;
@@ -41,19 +40,19 @@ class StockSettingsState extends State<StockSettings> {
title: new Text("Change mode?"),
content: new Text("Optimistic mode means everything is awesome. Are you sure you can handle that?"),
onDismiss: () {
config.navigator.pop(false);
Navigator.of(context).pop(false);
},
actions: <Widget>[
new FlatButton(
child: new Text('NO THANKS'),
onPressed: () {
config.navigator.pop(false);
Navigator.of(context).pop(false);
}
),
new FlatButton(
child: new Text('AGREE'),
onPressed: () {
config.navigator.pop(true);
Navigator.of(context).pop(true);
}
),
]
@@ -75,7 +74,7 @@ class StockSettingsState extends State<StockSettings> {
return new ToolBar(
left: new IconButton(
icon: 'navigation/arrow_back',
onPressed: config.navigator.pop
onPressed: () => Navigator.of(context).pop()
),
center: new Text('Settings')
);

View File

@@ -65,8 +65,7 @@ class Dot extends StatelessComponent {
}
class ExampleDragSource extends StatelessComponent {
ExampleDragSource({ Key key, this.navigator, this.name, this.color }) : super(key: key);
final NavigatorState navigator;
ExampleDragSource({ Key key, this.name, this.color }) : super(key: key);
final String name;
final Color color;
@@ -75,7 +74,6 @@ class ExampleDragSource extends StatelessComponent {
Widget build(BuildContext context) {
return new Draggable(
navigator: navigator,
data: new DragData(name),
child: new Dot(color: color, size: kDotSize),
feedback: new Transform(
@@ -91,13 +89,7 @@ class ExampleDragSource extends StatelessComponent {
}
}
class DragAndDropApp extends StatefulComponent {
DragAndDropApp({ this.navigator });
final NavigatorState navigator;
DragAndDropAppState createState() => new DragAndDropAppState();
}
class DragAndDropAppState extends State<DragAndDropApp> {
class DragAndDropApp extends StatelessComponent {
Widget build(BuildContext context) {
return new Scaffold(
toolBar: new ToolBar(
@@ -107,9 +99,9 @@ class DragAndDropAppState extends State<DragAndDropApp> {
style: Theme.of(context).text.body1.copyWith(textAlign: TextAlign.center),
child: new Column(<Widget>[
new Flexible(child: new Row(<Widget>[
new ExampleDragSource(navigator: config.navigator, name: 'Orange', color: const Color(0xFFFF9000)),
new ExampleDragSource(navigator: config.navigator, name: 'Teal', color: const Color(0xFF00FFFF)),
new ExampleDragSource(navigator: config.navigator, name: 'Yellow', color: const Color(0xFFFFF000)),
new ExampleDragSource(name: 'Orange', color: const Color(0xFFFF9000)),
new ExampleDragSource(name: 'Teal', color: const Color(0xFF00FFFF)),
new ExampleDragSource(name: 'Yellow', color: const Color(0xFFFFF000)),
],
alignItems: FlexAlignItems.center,
justifyContent: FlexJustifyContent.spaceAround
@@ -130,7 +122,7 @@ void main() {
runApp(new MaterialApp(
title: 'Drag and Drop Flutter Demo',
routes: <String, RouteBuilder>{
'/': (RouteArguments args) => new DragAndDropApp(navigator: args.navigator)
'/': (RouteArguments args) => new DragAndDropApp()
}
));
}

View File

@@ -12,11 +12,11 @@ final Map<String, RouteBuilder> routes = <String, RouteBuilder>{
new Text("You are at home"),
new RaisedButton(
child: new Text('GO SHOPPING'),
onPressed: () => args.navigator.pushNamed('/shopping')
onPressed: () => Navigator.of(args.context).pushNamed('/shopping')
),
new RaisedButton(
child: new Text('START ADVENTURE'),
onPressed: () => args.navigator.pushNamed('/adventure')
onPressed: () => Navigator.of(args.context).pushNamed('/adventure')
)],
justifyContent: FlexJustifyContent.center
)
@@ -28,11 +28,11 @@ final Map<String, RouteBuilder> routes = <String, RouteBuilder>{
new Text("Village Shop"),
new RaisedButton(
child: new Text('RETURN HOME'),
onPressed: () => args.navigator.pop()
onPressed: () => Navigator.of(args.context).pop()
),
new RaisedButton(
child: new Text('GO TO DUNGEON'),
onPressed: () => args.navigator.pushNamed('/adventure')
onPressed: () => Navigator.of(args.context).pushNamed('/adventure')
)],
justifyContent: FlexJustifyContent.center
)
@@ -44,7 +44,7 @@ final Map<String, RouteBuilder> routes = <String, RouteBuilder>{
new Text("Monster's Lair"),
new RaisedButton(
child: new Text('RUN!!!'),
onPressed: () => args.navigator.pop()
onPressed: () => Navigator.of(args.context).pop()
)],
justifyContent: FlexJustifyContent.center
)