Introduce a showPopupMenu() function

Instead of having to manage the popup menu from your app's build
function, you now just call showPopupMenu() with the menu's position and
it takes care of everything for you.

This solves the problem that the popup menu was trying to mutate the
state of the navigator from within its own initState() function.

Also, remove the "route" argument to RouteBase.build() since it equals
"this" by definition...

Also, remove ModalOverlay, and instead put that logic in the navigator.
This commit is contained in:
Hixie
2015-09-14 13:35:05 -07:00
parent f15b948060
commit a3ae46b97e
9 changed files with 176 additions and 165 deletions

View File

@@ -4,6 +4,7 @@
library stocks;
import 'dart:async';
import 'dart:math' as math;
import 'dart:sky' as sky;

View File

@@ -74,28 +74,6 @@ class StockHome extends StatefulComponent {
});
}
bool _menuShowing = false;
AnimationStatus _menuStatus = AnimationStatus.dismissed;
void _handleMenuShow() {
setState(() {
_menuShowing = true;
_menuStatus = AnimationStatus.forward;
});
}
void _handleMenuHide() {
setState(() {
_menuShowing = false;
});
}
void _handleMenuDismissed() {
setState(() {
_menuStatus = AnimationStatus.dismissed;
});
}
bool _autorefresh = false;
void _handleAutorefreshChanged(bool value) {
setState(() {
@@ -112,6 +90,13 @@ class StockHome extends StatefulComponent {
return EventDisposition.processed;
}
void _handleMenuShow() {
showStockMenu(navigator,
autorefresh: _autorefresh,
onAutorefreshChanged: _handleAutorefreshChanged
);
}
Drawer buildDrawer() {
if (_drawerStatus == AnimationStatus.dismissed)
return null;
@@ -282,31 +267,13 @@ class StockHome extends StatefulComponent {
);
}
void addMenuToOverlays(List<Widget> overlays) {
if (_menuStatus == AnimationStatus.dismissed)
return;
overlays.add(new ModalOverlay(
children: [new StockMenu(
showing: _menuShowing,
onDismissed: _handleMenuDismissed,
navigator: navigator,
autorefresh: _autorefresh,
onAutorefreshChanged: _handleAutorefreshChanged
)],
onDismiss: _handleMenuHide));
}
Widget build() {
List<Widget> overlays = [
new Scaffold(
toolbar: _isSearching ? buildSearchBar() : buildToolBar(),
body: buildTabNavigator(),
snackBar: buildSnackBar(),
floatingActionButton: buildFloatingActionButton(),
drawer: buildDrawer()
),
];
addMenuToOverlays(overlays);
return new Stack(overlays);
return new Scaffold(
toolbar: _isSearching ? buildSearchBar() : buildToolBar(),
body: buildTabNavigator(),
snackBar: buildSnackBar(),
floatingActionButton: buildFloatingActionButton(),
drawer: buildDrawer()
);
}
}

View File

@@ -4,45 +4,29 @@
part of stocks;
class StockMenu extends Component {
StockMenu({
Key key,
this.showing,
this.onDismissed,
this.navigator,
this.autorefresh: false,
this.onAutorefreshChanged
}) : super(key: key);
final bool showing;
final PopupMenuDismissedCallback onDismissed;
final Navigator navigator;
final bool autorefresh;
final ValueChanged onAutorefreshChanged;
Widget build() {
var checkbox = new Checkbox(
value: this.autorefresh,
onChanged: this.onAutorefreshChanged
);
return new Positioned(
child: new PopupMenu(
items: [
new PopupMenuItem(child: new Text('Add stock')),
new PopupMenuItem(child: new Text('Remove stock')),
new PopupMenuItem(
onPressed: () => onAutorefreshChanged(!autorefresh),
child: new Row([new Flexible(child: new Text('Autorefresh')), checkbox])
),
],
level: 4,
showing: showing,
onDismissed: onDismissed,
navigator: navigator
),
Future showStockMenu(Navigator navigator, { bool autorefresh, ValueChanged onAutorefreshChanged }) {
return showMenu(
navigator: navigator,
position: new MenuPosition(
right: sky.view.paddingRight,
top: sky.view.paddingTop
);
}
}
),
builder: (Navigator navigator) {
return <PopupMenuItem>[
new PopupMenuItem(child: new Text('Add stock')),
new PopupMenuItem(child: new Text('Remove stock')),
new PopupMenuItem(
onPressed: () => onAutorefreshChanged(!autorefresh),
child: new Row([
new Flexible(child: new Text('Autorefresh')),
new Checkbox(
value: autorefresh,
onChanged: onAutorefreshChanged
)
]
)
),
];
}
);
}