flutter - 页面之间的底部导航栏
问题描述
我正在尝试创建一个底部导航栏,它将在我的主屏幕和我的 Settigns 屏幕之间切换。我试图这样做,但在我的 Stful 小部件顶部添加一个列表似乎不起作用,我试图添加一个列表以便我可以切换页面。但是在我身体的开头添加一个列表似乎没有帮助。
这是我尝试的:
class _HomeState extends State<Home> {
var temperature;
var humidity;
//Bottom navigation bar stuff
int _currentIndex = 0;
final tab = [
Home(),
Settings(),
],
@override
void initState () {
this.getWeather();
super.initState();
}
@override
Widget build(BuildContext context) {
final AuthService _auth = AuthService();
final user = Provider.of<User>(context);
return StreamBuilder<UserData>(
stream: DatabaseService(uid: user.uid).userData,
builder : ((context, snapshot){
if (!snapshot.hasData) {
return Loading();
}
UserData userData = snapshot.data;
List<bool> cardsValue = [userData.device1, userData.device2, userData.device3, false];
return Scaffold(
//Nav part
bottomNavigationBar: BottomNavigationBar(
backgroundColor: Colors.grey[900],
currentIndex: _currentIndex,
type: BottomNavigationBarType.fixed,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home, color: Colors.white,),
title: Text("Home",style: TextStyle(color: Colors.white),)
),
BottomNavigationBarItem(
icon: Icon(
Icons.settings,
color: Colors.white,
),
title: Text(
"Settings",
style: TextStyle(color: Colors.white),)
)
],
onTap: (int index){
setState(() {
this._currentIndex = index;
});
},
),
backgroundColor: Colors.grey[900],
appBar: AppBar(
elevation: 0,
backgroundColor: Colors.grey[900],
title: Row(
...
body: SingleChildScrollView(
child: ConstrainedBox(
constraints: BoxConstraints(),
child: Padding(
padding: EdgeInsets.fromLTRB(12, 20, 12, 0),
child: Column(
children: <Widget>
这是按下设置图标时我想去的地方:
import 'package:flutter/material.dart';
class Settings extends StatefulWidget {
@override
_SettingsState createState() => _SettingsState();
}
class _SettingsState extends State<Settings> {
@override
Widget build(BuildContext context) {
return Scaffold(
);
}
}
解决方案
您需要使用 aTabBarView
和 a TabController
。
请参阅这本食谱以了解如何操作。
编辑 :
脚手架的身体应该是TabBarView(children: tab)
。
切换BottomNavigationBar
为TabBar
.
创建一个 TabController 作为状态成员:TabController _tabController;
像这样初始化它initState()
:
_tabController = TabController(vsync: this, length: 2);
TabBar
最后,将和的控制器参数设置TabBarView
为_tabController
。
那应该这样做。
推荐阅读
- python - 在 python 中出现错误“字符串索引超出范围”
- html - 如何在html中将元素设置为右侧?
- c# - C# - 将数字转换为最小 BitArray 的最快方法是什么
- django - 在 Axios、Vue JS 中使用 Django 模式传递 ID Mitch mech
- json - Pyspark Struct 列:爆炸后的奇怪行为
- java - 期间发生内部错误:“分析覆盖会话 java
".java.lang.IllegalArgumentException-代码覆盖 EclEmma in sts - informatica-cloud - iics 中的平面文件连接失败。连接的行为就像在 Windows 的运行时运行的 Unix 服务器
- javascript - 如何获取面包屑链接点击的价值
- reactjs - 打字稿:如何将 PropTypes.oneOf(Object.value(FOOBAR)) 转换为类型?
- node.js - Angular + Nodejs Express:错误跨域请求被阻止:同源策略不允许读取远程资源