其他
Flutter系列之Navigator使用详解
PS:想做一件事很容易,真正去做一件事很困难。
基本路由导航
路由参数传递
其他路由导航
基本路由导航
Navigator.push
static Future<T> push<T extends Object>(BuildContext context, Route<T> route) {
return Navigator.of(context).push(route);
}
_navigateToPage(context, NavigatorPushPopPage());
/// Navigator.push
/// 页面跳转
_navigateToPage(BuildContext context, Widget widget) {
Navigator.push(context, MaterialPageRoute(builder: (BuildContext context) {
return widget;
}));
}
Navigator.pop
static bool pop<T extends Object>(BuildContext context, [ T result ]) {
return Navigator.of(context).pop<T>(result);
}
icon: Icon(Icons.arrow_back),
onPressed: () => Navigator.pop(context)),
Navigator.pushnamed
static Future<T> pushNamed<T extends Object>(
BuildContext context,
String routeName, {
Object arguments,
}) {
return Navigator.of(context).pushNamed<T>(routeName, arguments: arguments);
}
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(),
routes: <String, WidgetBuilder>{
// 对应路由/NavigatorPushNamedPage
NavigatorPushNamedPage.routeName: (BuildContext context) =>
NavigatorPushNamedPage(),
},
);
}
}
/// page
/// Navigator.pushNamed
/// 使用已命名路由
class NavigatorPushNamedPage extends StatelessWidget {
static final routeName = '/NavigatorPushNamedPage';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Navigator.pushNamed"),
centerTitle: true,
),
body: Center(
child: Text(
"Navigator.pushNamed",
style: TextStyle(fontSize: 18),
),
),
);
}
}
_navigatePushNamedPage(context, NavigatorPushNamedPage.routeName);
/// Navigator.pushNamed
/// 页面跳转
_navigatePushNamedPage(BuildContext context, String routeName,
[Object arguments]) {
Navigator.pushNamed(context, routeName, arguments: arguments);
}
Future<T> push<T extends Object>(Route<T> route) {
assert(!_debugLocked);
assert(() { _debugLocked = true; return true; }());
assert(route != null);
assert(route._navigator == null);
final Route<dynamic> oldRoute = _history.isNotEmpty ? _history.last : null;
route._navigator = this;
route.install(_currentOverlayEntry);
_history.add(route);
route.didPush();
route.didChangeNext(null);
if (oldRoute != null) {
oldRoute.didChangeNext(route);
route.didChangePrevious(oldRoute);
}
for (NavigatorObserver observer in widget.observers)
observer.didPush(route, oldRoute);
RouteNotificationMessages.maybeNotifyRouteChange(_routePushedMethod, route, oldRoute);
assert(() { _debugLocked = false; return true; }());
_afterNavigation(route);
return route.popped;
}
路由参数传递
Navigator.push携带参数
_navigateToPage(context,
NavigatorPushWithParamPage(
param: "this info from last page!",
));
/// Navigator.push/pop
/// 页面跳转
_navigateToPage(BuildContext context, Widget widget) {
Navigator.push(context, MaterialPageRoute(builder: (BuildContext context) {
return widget;
}));
}
/// page
/// Navigator.push携带参数
class NavigatorPushWithParamPage extends StatelessWidget {
// 参数
final String param;
NavigatorPushWithParamPage({
this.param,
});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Navigator.push携带参数"),
centerTitle: true,
),
body: Center(
child: Text(
"arguments:${this.param}",
style: TextStyle(fontSize: 18),
),
),
);
}
}
Navigator.pushNamed携带参数
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(),
// 参数接收
onGenerateRoute: (RouteSettings settings) {
if (settings.name == NavigatorPushNamedWithParamPage.routeName) {
return MaterialPageRoute<String>(builder: (BuildContext context) {
return NavigatorPushNamedWithParamPage(settings.arguments);
});
}else{
return null;
}
},
);
}
}
_navigatePushNamedPage(
context,
NavigatorPushNamedWithParamPage.routeName,
"this info from last page!");
/// Navigator.pushNamed携带参数
_navigatePushNamedPage(BuildContext context, String routeName,
[Object arguments]) {
Navigator.pushNamed(context, routeName, arguments: arguments);
}
/// page
/// Navigator.pushNamed携带参数
/// 使用已命名路由
class NavigatorPushNamedWithParamPage extends StatelessWidget {
static final String routeName = '/NavigatorPushNamedWithParamPage';
final String info;
NavigatorPushNamedWithParamPage(this.info);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Navigator.pushNamed携带参数"),
centerTitle: true,
),
body: Center(
child: Text(
"arguments:${this.info}",
style: TextStyle(fontSize: 18),
),
),
),
);
}
}
页面返回时携带参数
_navigatePopWithParamPage(context, NavigatorPopWithParamPage());
/// Navigator.pop返回时携带参数
_navigatePopWithParamPage(BuildContext context, Widget widget) {
Navigator.push<String>(context,
MaterialPageRoute(builder: (BuildContext context) {
return widget;
})).then((result) {
// 返回时携带参数处理
Toast.show("Navigator.pop返回时携带参数:" + result, context);
});
}
/// page
/// Navigator.pop返回时携带参数
class NavigatorPopWithParamPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return WillPopScope(
child: Scaffold(
appBar: AppBar(
title: Text("Navigator.pop返回时携带参数"),
centerTitle: true,
),
body: Center(
child: Text(
"Navigator.pop返回时携带参数",
style: TextStyle(fontSize: 18),
),
),
),
onWillPop: () => _popBack(context));
}
/// 页面返回并设置返回参数,类似Android中的setResult方法
_setResult(BuildContext context) {
Navigator.pop(context, "this message from NavigatorPopWithParamPage!");
}
/// 统一处理返回键
Future<bool> _popBack(BuildContext context) {
_setResult(context);
return Future.value(false);
}
}
其他路由导航
Navigator.popAndPushNamed
// 根据指定的Route直接返回,在此之前的路由会被清除
Navigator.popUntil
// 跳转到新的Route,并将指定Route之前的的Route清空,pushNamedAndRemoveUntil与之类似
Navigator.pushAndRemoveUntil
// 页面替换,pushReplacementNamed与之类似
Navigator.pushReplacement