Clean up the existing Navigator API. (#15718)
This is not a grand refactor yet, it's just cleaning up what we have already, so that people who keep using this API (e.g. dialogs) have something coherent to deal with. The major changes are that Navigator and NavigatorState have the same API now, that most of the examples use `<void>` instead of `<Null>`, that the navigator observer can see replaces, and that the `settings` is moved from ModalRoute to Route. I also cleaned up some of the API documentation.
This commit is contained in:
@@ -451,7 +451,7 @@ class _AnimationDemoHomeState extends State<AnimationDemoHome> {
|
||||
if (_scrollController.offset >= midScrollOffset)
|
||||
_scrollController.animateTo(0.0, curve: _kScrollCurve, duration: _kScrollDuration);
|
||||
else
|
||||
Navigator.of(context).maybePop();
|
||||
Navigator.maybePop(context);
|
||||
}
|
||||
|
||||
// Only enable paging for the heading when the user has scrolled to midScrollOffset.
|
||||
|
||||
@@ -21,7 +21,7 @@ class _CupertinoDialogDemoState extends State<CupertinoDialogDemo> {
|
||||
barrierDismissible: false,
|
||||
builder: (BuildContext context) => child,
|
||||
)
|
||||
.then<Null>((T value) { // The value passed to Navigator.pop() or null.
|
||||
.then<void>((T value) { // The value passed to Navigator.pop() or null.
|
||||
if (value != null) {
|
||||
_scaffoldKey.currentState.showSnackBar(new SnackBar(
|
||||
content: new Text('You selected: $value')
|
||||
|
||||
@@ -173,7 +173,7 @@ class Tab1RowItem extends StatelessWidget {
|
||||
final Widget row = new GestureDetector(
|
||||
behavior: HitTestBehavior.opaque,
|
||||
onTap: () {
|
||||
Navigator.of(context).push(new CupertinoPageRoute<Null>(
|
||||
Navigator.of(context).push(new CupertinoPageRoute<void>(
|
||||
builder: (BuildContext context) => new Tab1ItemPage(
|
||||
color: color,
|
||||
colorName: colorName,
|
||||
@@ -778,7 +778,7 @@ class Tab3Dialog extends StatelessWidget {
|
||||
color: CupertinoColors.activeBlue,
|
||||
child: const Text('Sign in'),
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop();
|
||||
Navigator.pop(context);
|
||||
},
|
||||
),
|
||||
],
|
||||
|
||||
@@ -112,7 +112,7 @@ class _CupertinoPickerDemoState extends State<CupertinoPickerDemo> {
|
||||
const Padding(padding: const EdgeInsets.only(top: 32.0)),
|
||||
new GestureDetector(
|
||||
onTap: () async {
|
||||
await showModalBottomSheet<Null>(
|
||||
await showModalBottomSheet<void>(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
return _buildBottomPicker();
|
||||
|
||||
@@ -70,7 +70,7 @@ class DialogDemoState extends State<DialogDemo> {
|
||||
context: context,
|
||||
builder: (BuildContext context) => child,
|
||||
)
|
||||
.then<Null>((T value) { // The value passed to Navigator.pop() or null.
|
||||
.then<void>((T value) { // The value passed to Navigator.pop() or null.
|
||||
if (value != null) {
|
||||
_scaffoldKey.currentState.showSnackBar(new SnackBar(
|
||||
content: new Text('You selected: $value')
|
||||
|
||||
@@ -67,7 +67,7 @@ class _DrawerDemoState extends State<DrawerDemo> with TickerProviderStateMixin {
|
||||
}
|
||||
|
||||
void _showNotImplementedMessage() {
|
||||
Navigator.of(context).pop(); // Dismiss the drawer.
|
||||
Navigator.pop(context); // Dismiss the drawer.
|
||||
_scaffoldKey.currentState.showSnackBar(const SnackBar(
|
||||
content: const Text("The drawer's items don't do anything")
|
||||
));
|
||||
@@ -231,7 +231,7 @@ class _DrawerDemoState extends State<DrawerDemo> with TickerProviderStateMixin {
|
||||
}
|
||||
|
||||
void _onOtherAccountsTap(BuildContext context) {
|
||||
showDialog<Null>(
|
||||
showDialog<void>(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
return new AlertDialog(
|
||||
@@ -240,7 +240,7 @@ class _DrawerDemoState extends State<DrawerDemo> with TickerProviderStateMixin {
|
||||
new FlatButton(
|
||||
child: const Text('OK'),
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop();
|
||||
Navigator.pop(context);
|
||||
},
|
||||
),
|
||||
],
|
||||
|
||||
@@ -165,7 +165,7 @@ class GridDemoPhotoItem extends StatelessWidget {
|
||||
final BannerTapCallback onBannerTap; // User taps on the photo's header or footer.
|
||||
|
||||
void showPhoto(BuildContext context) {
|
||||
Navigator.push(context, new MaterialPageRoute<Null>(
|
||||
Navigator.push(context, new MaterialPageRoute<void>(
|
||||
builder: (BuildContext context) {
|
||||
return new Scaffold(
|
||||
appBar: new AppBar(
|
||||
|
||||
@@ -15,7 +15,7 @@ class ModalBottomSheetDemo extends StatelessWidget {
|
||||
child: new RaisedButton(
|
||||
child: const Text('SHOW BOTTOM SHEET'),
|
||||
onPressed: () {
|
||||
showModalBottomSheet<Null>(context: context, builder: (BuildContext context) {
|
||||
showModalBottomSheet<void>(context: context, builder: (BuildContext context) {
|
||||
return new Container(
|
||||
child: new Padding(
|
||||
padding: const EdgeInsets.all(32.0),
|
||||
|
||||
@@ -54,7 +54,7 @@ class _PersistentBottomSheetDemoState extends State<PersistentBottomSheetDemo> {
|
||||
}
|
||||
|
||||
void _showMessage() {
|
||||
showDialog<Null>(
|
||||
showDialog<void>(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
return new AlertDialog(
|
||||
|
||||
@@ -167,14 +167,14 @@ class _RecipeGridPageState extends State<RecipeGridPage> {
|
||||
}
|
||||
|
||||
void showFavoritesPage(BuildContext context) {
|
||||
Navigator.push(context, new MaterialPageRoute<Null>(
|
||||
Navigator.push(context, new MaterialPageRoute<void>(
|
||||
settings: const RouteSettings(name: '/pesto/favorites'),
|
||||
builder: (BuildContext context) => new PestoFavorites(),
|
||||
));
|
||||
}
|
||||
|
||||
void showRecipePage(BuildContext context, Recipe recipe) {
|
||||
Navigator.push(context, new MaterialPageRoute<Null>(
|
||||
Navigator.push(context, new MaterialPageRoute<void>(
|
||||
settings: const RouteSettings(name: '/pesto/recipe'),
|
||||
builder: (BuildContext context) {
|
||||
return new Theme(
|
||||
|
||||
@@ -327,7 +327,7 @@ class ShrineOrderRoute extends ShrinePageRoute<Order> {
|
||||
ShrineOrderRoute({
|
||||
@required this.order,
|
||||
WidgetBuilder builder,
|
||||
RouteSettings settings: const RouteSettings(),
|
||||
RouteSettings settings,
|
||||
}) : assert(order != null),
|
||||
super(builder: builder, settings: settings);
|
||||
|
||||
@@ -336,5 +336,5 @@ class ShrineOrderRoute extends ShrinePageRoute<Order> {
|
||||
@override
|
||||
Order get currentResult => order;
|
||||
|
||||
static ShrineOrderRoute of(BuildContext context) => ModalRoute.of(context);
|
||||
static ShrineOrderRoute of(BuildContext context) => ModalRoute.of<Order>(context);
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ class ShrinePageState extends State<ShrinePage> {
|
||||
}
|
||||
|
||||
void _showShoppingCart() {
|
||||
showModalBottomSheet<Null>(context: context, builder: (BuildContext context) {
|
||||
showModalBottomSheet<void>(context: context, builder: (BuildContext context) {
|
||||
if (widget.shoppingCart.isEmpty) {
|
||||
return const Padding(
|
||||
padding: const EdgeInsets.all(24.0),
|
||||
|
||||
@@ -25,7 +25,7 @@ Widget buildShrine(BuildContext context, Widget child) {
|
||||
class ShrinePageRoute<T> extends MaterialPageRoute<T> {
|
||||
ShrinePageRoute({
|
||||
WidgetBuilder builder,
|
||||
RouteSettings settings: const RouteSettings()
|
||||
RouteSettings settings,
|
||||
}) : super(builder: builder, settings: settings);
|
||||
|
||||
@override
|
||||
|
||||
@@ -64,12 +64,12 @@ class VideoCard extends StatelessWidget {
|
||||
}
|
||||
|
||||
void pushFullScreenWidget() {
|
||||
final TransitionRoute<Null> route = new PageRouteBuilder<Null>(
|
||||
final TransitionRoute<void> route = new PageRouteBuilder<void>(
|
||||
settings: new RouteSettings(name: title, isInitialRoute: false),
|
||||
pageBuilder: fullScreenRoutePageBuilder,
|
||||
);
|
||||
|
||||
route.completed.then((Null _) {
|
||||
route.completed.then((void result) {
|
||||
controller.setVolume(0.0);
|
||||
});
|
||||
|
||||
|
||||
@@ -165,7 +165,7 @@ class GalleryAppState extends State<GalleryApp> {
|
||||
checkerboardRasterCacheImages: _checkerboardRasterCacheImages,
|
||||
checkerboardOffscreenLayers: _checkerboardOffscreenLayers,
|
||||
routes: _kRoutes,
|
||||
home: _applyScaleFactor(home),
|
||||
home: home,
|
||||
builder: (BuildContext context, Widget child) {
|
||||
return new Directionality(
|
||||
textDirection: _overrideDirection,
|
||||
|
||||
@@ -31,23 +31,23 @@ class UpdaterState extends State<Updater> {
|
||||
}
|
||||
|
||||
static DateTime _lastUpdateCheck;
|
||||
Future<Null> _checkForUpdates() async {
|
||||
Future<void> _checkForUpdates() async {
|
||||
// Only prompt once a day
|
||||
if (_lastUpdateCheck != null &&
|
||||
new DateTime.now().difference(_lastUpdateCheck) < const Duration(days: 1)) {
|
||||
return; // We already checked for updates recently
|
||||
return null; // We already checked for updates recently
|
||||
}
|
||||
_lastUpdateCheck = new DateTime.now();
|
||||
|
||||
final String updateUrl = await widget.updateUrlFetcher();
|
||||
if (updateUrl != null) {
|
||||
final bool wantsUpdate = await showDialog(context: context, builder: _buildDialog);
|
||||
final bool wantsUpdate = await showDialog<bool>(context: context, builder: _buildDialog);
|
||||
if (wantsUpdate != null && wantsUpdate)
|
||||
launch(updateUrl);
|
||||
}
|
||||
}
|
||||
|
||||
Widget _buildDialog(BuildContext _) {
|
||||
Widget _buildDialog(BuildContext context) {
|
||||
final ThemeData theme = Theme.of(context);
|
||||
final TextStyle dialogTextStyle =
|
||||
theme.textTheme.subhead.copyWith(color: theme.textTheme.caption.color);
|
||||
@@ -56,16 +56,19 @@ class UpdaterState extends State<Updater> {
|
||||
content: new Text('A newer version is available.', style: dialogTextStyle),
|
||||
actions: <Widget>[
|
||||
new FlatButton(
|
||||
child: const Text('NO THANKS'),
|
||||
onPressed: () {
|
||||
Navigator.pop(context, false);
|
||||
}),
|
||||
child: const Text('NO THANKS'),
|
||||
onPressed: () {
|
||||
Navigator.pop(context, false);
|
||||
},
|
||||
),
|
||||
new FlatButton(
|
||||
child: const Text('UPDATE'),
|
||||
onPressed: () {
|
||||
Navigator.pop(context, true);
|
||||
}),
|
||||
]);
|
||||
child: const Text('UPDATE'),
|
||||
onPressed: () {
|
||||
Navigator.pop(context, true);
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -83,7 +83,7 @@ class StocksAppState extends State<StocksApp> {
|
||||
return null;
|
||||
}
|
||||
|
||||
Route<Null> _getRoute(RouteSettings settings) {
|
||||
Route<dynamic> _getRoute(RouteSettings settings) {
|
||||
// Routes, by convention, are split on slashes, like filesystem paths.
|
||||
final List<String> path = settings.name.split('/');
|
||||
// We only support paths that start with a slash, so bail if
|
||||
@@ -100,7 +100,7 @@ class StocksAppState extends State<StocksApp> {
|
||||
// Extract the symbol part of "stock:..." and return a route
|
||||
// for that symbol.
|
||||
final String symbol = path[1].substring(6);
|
||||
return new MaterialPageRoute<Null>(
|
||||
return new MaterialPageRoute<void>(
|
||||
settings: settings,
|
||||
builder: (BuildContext context) => new StockSymbolPage(symbol: symbol, stocks: stocks),
|
||||
);
|
||||
|
||||
@@ -93,7 +93,7 @@ class StockHomeState extends State<StockHome> {
|
||||
});
|
||||
break;
|
||||
case _StockMenuItem.refresh:
|
||||
showDialog<Null>(
|
||||
showDialog<void>(
|
||||
context: context,
|
||||
builder: (BuildContext context) => new _NotImplementedDialog(),
|
||||
);
|
||||
@@ -297,7 +297,7 @@ class StockHomeState extends State<StockHome> {
|
||||
}
|
||||
|
||||
void _handleCreateCompany() {
|
||||
showModalBottomSheet<Null>(
|
||||
showModalBottomSheet<void>(
|
||||
context: context,
|
||||
builder: (BuildContext context) => new _CreateCompanySheet(),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user