我使用自定义导航器为持久的底部栏做了建议的here。
class _MainScreenState extends State<MainScreen> {
final _navigatorKey = GlobalKey<NavigatorState>();
// ...
@override
Widget build(BuildContext context) {
return Scaffold(
body: Navigator(
key: _navigatorKey,
initialRoute: '/',
onGenerateRoute: (RouteSettings settings) {
WidgetBuilder builder;
// Manage your route names here
switch (settings.name) {
case '/':
builder = (BuildContext context) => HomePage();
break;
case '/page1':
builder = (BuildContext context) => Page1();
break;
case '/page2':
builder = (BuildContext context) => Page2();
break;
default:
throw Exception('Invalid route: ${settings.name}');
}
// You can also return a PageRouteBuilder and
// define custom transitions between pages
return MaterialPageRoute(
builder: builder,
settings: settings,
);
},
),
bottomNavigationBar: _yourBottomNavigationBar,
);
}
}
现在,如果我在HomePage上有子小部件,并且需要将其导航到Page2的按钮,那么我将如何在HomePage内部实现它-因为?
答案 0 :(得分:0)
一些按钮或GestureDetector
onTap : ()=>Navigator.of(context).pushNamed('/page2')
要么
onPress : ()=>Navigator.of(context).pushNamed('/page2')