gpt4 book ai didi

dart - 所有屏幕都需要一个持久/相同的底部导航栏 - Flutter

转载 作者:IT老高 更新时间:2023-10-28 12:38:02 27 4
gpt4 key购买 nike

我是 flutter 和 Dart 的初学者。我一直在尝试在我的应用程序的三个不同页面上实现 navigationBar。切换适用于单个页面,但我在所有页面上保持事件和非事件选项卡状态时遇到问题。似乎当它导航到另一个页面时,我也失去了标签的事件状态。这是我的代码。

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://medium.com/coding-with-flutter/flutter-case-study-multiple-navigators-with-bottomnavigationbar-90eb6caa6dbf

https://github.com/bizz84/nested-navigation-demo-flutter

所有学分归原作者所有。

关于dart - 所有屏幕都需要一个持久/相同的底部导航栏 - Flutter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54767298/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com