您可以使用自定义的Navigator来满足这3个要求。
Flutter团队对此做了一个视频,下面是他们的文章:https :
//medium.com/flutter/getting-to-the-bottom-of-navigation-in-
flutter-b3e440b9386
基本上,您需要将脚手架的主体包装到一个自定义中
Navigator:
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, ); }}
在底部导航栏中,只需使用以下命令即可导航到新custom中的新屏幕
Navigator:
_navigatorKey.currentState.pushNamed('/yourRouteName');
为了实现第三个要求,这是
Navigator.pop带你到一个视图,你将需要包装定制
Navigator了
WillPopScope:
@overrideWidget build(BuildContext context) { return Scaffold( body: WillPopScope( onWillPop: () async { if (_navigatorKey.currentState.canPop()) { _navigatorKey.currentState.pop(); return false; } return true; }, child: Navigator( // ... ), ), bottomNavigationBar: _yourBottomNavigationBar, );}
就是这样!无需手动处理d出或管理自定义历史记录列表。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)