Flutter 在页面视图之间导航时如何保持底部导航
我有4个页面,一个页面视图和它们各自的底部导航器,在第一个索引处,我正在执行一个导航到第四个页面的函数。然而,导航后,底部导航消失,这是不一致的。我如何使其一致性Flutter 在页面视图之间导航时如何保持底部导航,flutter,dart,Flutter,Dart,我有4个页面,一个页面视图和它们各自的底部导航器,在第一个索引处,我正在执行一个导航到第四个页面的函数。然而,导航后,底部导航消失,这是不一致的。我如何使其一致性 class HomePage extends StatefulWidget { HomePage({ Key key, this.category, this.shopname, }) : super(key: key); //update this to include the uid in th
class HomePage extends StatefulWidget {
HomePage({
Key key,
this.category,
this.shopname,
}) : super(key: key); //update this to include the uid in the constructor
final String shopname;
final DocumentSnapshot category;
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
导航器位于登录页
LandingPage(), //<<================
SavedTab(
shopname: widget.shopname,
),
MyDrawer(),
LandingPage(),//实际上,您只需要底部导航栏就可以导航到所需的页面。它基本上呈现BottomNavBar小部件内的所有屏幕,因此底部导航栏始终显示。以下是一个例子:
class BottomNavBar extends StatefulWidget {
@override
_BottomNavBarState createState() => _BottomNavBarState();
}
class _BottomNavBarState extends State<BottomNavBar> {
int _selectedIndex = 0;
static List<Widget> _widgetOptions = <Widget>[
Screen1(),
Screen2(),
Screen3(),
];
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: _widgetOptions.elementAt(_selectedIndex),
),
bottomNavigationBar: BottomNavigationBar(
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.dashboard),
label: 'Screen 1',
),
BottomNavigationBarItem(
icon: Icon(Icons.work),
label: 'Screen 2',
),
BottomNavigationBarItem(
icon: Icon(Icons.directions_car),
label: 'Screen 3',
),
],
currentIndex: _selectedIndex,
onTap: _onItemTapped,
),
);
}
}
类底部导航栏扩展StatefulWidget{
@凌驾
_BottomNavBarState createState()=>\u BottomNavBarState();
}
类_BottomNavBarState扩展状态{
int _selectedIndex=0;
静态列表\u widgetOptions=[
屏幕1(),
屏幕2(),
屏幕3(),
];
void\u未映射(整数索引){
设置状态(){
_selectedIndex=索引;
});
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:中(
子项:_widgetOptions.elementAt(_selectedIndex),
),
底部导航栏:底部导航栏(
项目:[
底部导航气压计(
图标:图标(图标.仪表板),
标签:“屏幕1”,
),
底部导航气压计(
图标:图标(Icons.work),
标签:“屏幕2”,
),
底部导航气压计(
图标:图标(图标、方向和汽车),
标签:“屏幕3”,
),
],
currentIndex:_selectedIndex,
onTap:\u未映射,
),
);
}
}
请在评论中告诉我它是否适合您。检查我的代码
-
或
定制脚手架
类AppScaffold扩展StatefulWidget{
最后清单项目;
最终名单建设者;
施工脚手架({
钥匙?,钥匙,
需要此项,
这是建筑商的要求,
}):super(key:key);
@凌驾
_AppScaffoldState createState();
静态导航器状态导航器FCurrentix(
构建上下文上下文{
应用脚手架索引?开关按钮索引,
}) {
最终appScaffoldState=
context是StatefulElement&&context.state是AppScaffoldState
?context.state为_AppScaffoldState
:context.findAncestorStateOfType()!;
最终currentIndex=appScaffoldState.currentIndex;
最终Navigatoryys=appScaffoldState.Navigatoryys;
最终新索引=开关按钮索引?.rawValue;
if(newIndex!=null&&
newIndex!=当前索引&&
appScaffoldState.mounted){
appScaffoldState.onTap(newIndex);
返回navigatorKeys[newIndex].currentState!;
}
返回navigatorKeys[currentIndex].currentState!;
}
静态NotReplayValueStream currentIndexStream(
BuildContext)=>
context.findAncestorStateOfType()!.indexS;
静态导航器状态导航器索引(
构建上下文上下文,
AppScaffoldIndex指数,
) {
最终appScaffoldState=
context.findAncestorStateOfType()!;
返回appScaffoldState.navigatorKeys[index.rawValue].currentState!;
}
}
类_AppScaffoldState使用DisposeBagMixin扩展状态{
var navigatorKeys=[];
最终索引=ValueSubject(AppScaffoldIndex.home,sync:true);
@pragma('vm:首选内联')
@pragma('dart2js:tryInline')
int get currentIndex=>indexS.requireValue.rawValue;
@凌驾
void initState(){
super.initState();
navigatorKeys=List.generate(
widget.builders.length,
()=>GlobalKey(),
);
处理指标(袋);
}
@凌驾
小部件构建(构建上下文){
返回式示波器(
onWillPop:(){
final navigatorState=navigatorKeys[currentIndex].currentState!;
final canPop=navigatorState.canPop();
如果(canPop){
navigatorState.maybePop();
}
如果(!canPop&¤tIndex>0){
onTap(0);
返回Future.value(false);
}
返回Future.value(!canPop);
},
子项:RXStreambulder(
流:指数,
生成器:(上下文,快照){
最终索引=快照值;
返回脚手架(
主体:构建主体(索引),
底部导航栏:底部导航栏(
items:widget.items,
类型:BottomNavigationBarType.fixed,
当前索引:索引,
onTap:onTap,
),
);
},
),
);
}
void onTap(最终整数新索引){
if(currentIndex==newIndex){
navigatorKeys[当前索引]
.当前状态
?.popintil((route)=>route.isFirst);
}否则{
indexS.add(_fromRawValue(newIndex));
}
}
小部件构建体(int索引){
返回索引堆栈(
索引:索引,,
儿童:[
for(int i=0;iMaterialPage路由(
设置:设置,
生成器:(上下文)=>widget.builders[i](上下文,设置),
),
观察员:[
HeroController(),
],
)
],
);
}
}
主页
返回AppScaffold(
密钥:appScaffoldKey,
建筑商:[
(上下文,设置)=>homeRoutes[settings.name]!(上下文,设置),
(上下文、设置)=>
favoritesRoutes[设置.名称]!(上下文,设置),
(上下文、设置)=>
通知路由[settings.name]!(上下文、设置),
(上下文,设置)=>profileRoutes[settings.name]!(上下文,设置),
],
项目:[
底部导航气压计(
图标:常量图标(图标。原点四舍五入),
标签:S.of(上下文).home,
),
底部导航气压计(
图标:常量图标(Icons.favorite\u round
HomeTab() //<<================
],
),
),
BottomTabs(
selectedTab: _selectedTab,
tabPressed: (num) {
_tabsPageController.animateToPage(num,
duration: Duration(milliseconds: 300),
curve: Curves.easeOutCubic);
},
),
],
),
);
}
}
class BottomNavBar extends StatefulWidget {
@override
_BottomNavBarState createState() => _BottomNavBarState();
}
class _BottomNavBarState extends State<BottomNavBar> {
int _selectedIndex = 0;
static List<Widget> _widgetOptions = <Widget>[
Screen1(),
Screen2(),
Screen3(),
];
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: _widgetOptions.elementAt(_selectedIndex),
),
bottomNavigationBar: BottomNavigationBar(
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.dashboard),
label: 'Screen 1',
),
BottomNavigationBarItem(
icon: Icon(Icons.work),
label: 'Screen 2',
),
BottomNavigationBarItem(
icon: Icon(Icons.directions_car),
label: 'Screen 3',
),
],
currentIndex: _selectedIndex,
onTap: _onItemTapped,
),
);
}
}