无法使底部导航栏和路由在抖动中协同工作 [英] Cannot make bottom navigation bar and routing work together in flutter
问题描述
我是新手,无法从底部导航栏导航到新页面
I am new to flutter and i cannot navigate to new page from bottom navigation bar
我有主应用
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
statusBarColor: Colors.transparent,
));
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(primarySwatch: Colors.blue),
builder: (BuildContext buildContext, Widget widtget) => Scaffold(
body: RootNavigator(),
bottomNavigationBar: BottomNavigation(),
),
);
}
}
和Rootnavigator
and Rootnavigator
class RootNavigator extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Navigator(
initialRoute: '/',
onGenerateRoute: (RouteSettings settings) {
// final args = settings.arguments;
return MaterialPageRoute(
settings: settings,
builder: (BuildContext context) {
switch (settings.name) {
case '/':
return Page1();
case '/page2':
return Page2();
case '/page3':
return Page3();
default:
return RouteErrorPage();
}
});
},
);
}
}
和底部导航器
class BottomNavigation extends StatefulWidget {
@override
BottomNavigationState createState() {
return new BottomNavigationState();
}
}
class BottomNavigationState extends State<BottomNavigation> {
int currIndex = 0;
onTap(int index) {
setState(() => currIndex = index);
switch (index) {
case 0:
Navigator.pushNamed(context, '/');
break;
case 1:
Navigator.pushNamed(context, '/page2');
break;
case 2:
Navigator.pushNamed(context, 'page3');
break;
default:
Navigator.push(
context, MaterialPageRoute(builder: (_) => RouteErrorPage()));
}
}
....
// added app bar items
}
标签正在切换,但路由未切换.它停留在主页上. 我觉得有一些与上下文有关的东西,但不知道如何解决. 有人可以帮忙吗? 谢谢
Tabs are switching but routes are not. It stays on home page. I feel like there is something with context but do not know how to solve it. Can anybody help? Thanks
p.s.如果我将底部导航栏分别移动到每个页面,除了选中的选项卡(由于状态)之外,其他所有功能都可以正常工作,而且我还想保留一个共享的应用程序栏
p.s. if i move bottom navigation bar to each page separatly everything work sexcept selected tab (because of state) and also i want to keep one, shared app bar
推荐答案
答案为-@LoVe注释正确. 那就是颤动的原理. 如果您具有底部导航,则必须在页面之间滑动. 重定向意味着要移动到全新的页面,您必须在sratch中定义Scaffold. 如果您想共享AppBar-使其成为可重用的小部件
Answer is - @LoVe comment was correct. Thats how flutter works. if you have bottom navigation you have to swipe through pages. Redirection means moving to completely new page and there you have to define Scaffold from sratch. If you want to have shared AppBar - make it reusable widget
这篇关于无法使底部导航栏和路由在抖动中协同工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!