gpt4 book ai didi

flutter - 如何在 Flutter Cupertino 底部导航栏中导航到同级项

转载 作者:行者123 更新时间:2023-12-03 21:04:20 24 4
gpt4 key购买 nike

我在 Flutter 中使用 Cupertino 设计并使用 CupertinoTabBar .当来自不同的屏幕时,我想访问不同的选项卡项目。

例如,我有 2 个项目:Home 和 Profile。 CupertinoTabBar在主屏幕中定义。来自 HomeScreen , 我想有个按钮访问ProfileScreen标签。在这种情况下如何导航?

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class MainScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _MainScreenState();
}
}

class _MainScreenState extends State<MainScreen> {

@override
Widget build(BuildContext context) {
return Material(
child: CupertinoTabScaffold(
tabBar: CupertinoTabBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text("Home")
),
BottomNavigationBarItem(
icon: Icon(Icons.user),
title: Text("My Appointments")
)
],
),
tabBuilder: (BuildContext context, int index) {
switch (index) {
case 0:
return CupertinoTabView(
builder: (BuildContext context) {
return HomeScreen();
},
defaultTitle: 'Home',
);
break;
case 1:
return CupertinoTabView(
builder: (BuildContext context) => ProfileScreen(),
defaultTitle: 'Profile',
);
break;
}

return null;
},
),
);
}
}

class HomeScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return Container(
child: CupertinoButton(
child: Text("Check Profile"),
onPressed: () {
// Pop this page and Navigate to Profile page
},
)
);
}
}

class ProfileScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return Container(
child: Text("Profile")
);
}
}

最佳答案

也许可以帮助你

int currentTabIndex = 0;

onTapped(int index) {
setState(() {
currentTabIndex = index;
});
}


List<Widget> tabs = [
InicioPage(),
OfertasPage(),
FavoritosPage(),
Login(),
];

tabBuilder: (context, index) {
switch (index) {
case 0:
return InicioPage();
break;
case 1:
return OfertasPage();
break;
case 2:
return FavoritosPage();
break;
case 3:
return Login();
default:
return InicioPage();
break;
}
}

关于flutter - 如何在 Flutter Cupertino 底部导航栏中导航到同级项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55315431/

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