dart - 所有屏幕都需要一个持久/相同的底部导航栏 - Flutter
问题描述
我是颤振和飞镖的初学者。我一直在尝试navigationBar
在我的应用程序的三个不同页面上实现 a。切换适用于单个页面,但我在所有页面上保持活动和非活动选项卡状态时遇到问题。似乎当它导航到另一个页面时,我也失去了标签的活动状态。这是我的代码。
AppFooter.dart
import 'package:flutter/material.dart';
class AppFooter extends StatefulWidget {
@override
_AppFooterState createState() => _AppFooterState();
}
class _AppFooterState extends State<AppFooter> {
int index = 0;
@override
Widget build(BuildContext context) {
return new Theme(
data: Theme.of(context).copyWith(
// sets the background color of the `BottomNavigationBar`
canvasColor: Colors.white,
// sets the active color of the `BottomNavigationBar` if `Brightness` is light
primaryColor: Colors.green,
textTheme: Theme.of(context)
.textTheme
.copyWith(caption: new TextStyle(color: Colors.grey))),
child: new BottomNavigationBar(
type: BottomNavigationBarType.fixed,
currentIndex: index,
onTap: (int index) {
setState(() {
this.index = index;
});
switch (index){
case 0: Navigator.of(context).pushNamed('/dashboard');
break;
case 1: Navigator.of(context).pushNamed('/medical centre');
break;
case 2: Navigator.of(context).pushNamed('/history');
break;
}
},
items: [
new BottomNavigationBarItem(
backgroundColor: Colors.white,
icon: index==0?new Image.asset('assets/images/dashboard_active.png'):new Image.asset('assets/images/dashboard_inactive.png'),
title: new Text('Dashboard', style: new TextStyle(fontSize: 12.0))),
new BottomNavigationBarItem(
backgroundColor: Colors.white,
icon: index==1?new Image.asset('assets/images/medical_sevice_active.png'):new Image.asset('assets/images/medical_sevice_inactive.png'),
title: new Text('Health Services', style: new TextStyle(fontSize: 12.0))),
new BottomNavigationBarItem(
icon: InkWell(
child: Icon(
Icons.format_align_left,
// color: green,
size: 20.0,
),
),
title: new Text('History', style: new TextStyle(fontSize: 12.0))),
]),
);
}
}
解决方案
如果我正确理解您的问题,您需要在所有三个页面上都保留底部导航栏。关于如何实现它,有一篇写得很好的文章。您可以在此处找到详细信息。
https://github.com/bizz84/nested-navigation-demo-flutter
所有学分归原作者所有。
推荐阅读
- python - 这意味着什么 - “将 Anaconda 注册为我的默认 Python”?
- python - NumPy TypeError:只有整数标量数组可以转换为标量索引
- node.js - 并行运行 node.js 函数
- r - 情节:一帧中两个情节的不同设置
- selenium - 有没有办法使用 pytest --dist loadfile 和 loadscope 来控制测试的分发方式
- javascript - 为 React Nivo 条形图转换对象数组
- c++ - VS Code 的问题说 endl、cout 和 sin 没有在这个范围内声明
- javascript - 将时间格式化为 MM:SS,即使它不是一个完整的分钟
- python - Django ArrayField - 从一类常量中选择 - ERROR fields.E005
- scala - 具有编码特殊字符的Scala API不适用于客户端