gpt4 book ai didi

android - 导航和路由 Flutter

转载 作者:IT王子 更新时间:2023-10-29 07:15:50 24 4
gpt4 key购买 nike

我刚开始 flutter 并尝试构建一个在 4 个页面之间导航的底部导航栏:

'.../pages/home.dart';
'.../pages/history.dart';
'.../pages/search.dart';
'.../pages/bookmarks.dart';

启动应用程序时,主页应始终作为主屏幕显示。 (显然!!)

我根据一些文档构建了导航栏。导航栏似乎可以正常工作,但问题是

我不知道在哪里以及如何实现其余的导航和标签切换逻辑

这是我的ma​​in_screen.dart

class _MainScreenState extends State<MainScreen> {
PageController _pageController;
int _page = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
body: PageView(
physics: NeverScrollableScrollPhysics(),
controller: _pageController,
onPageChanged: onPageChanged,
children: List.generate(4, (index) => Home()),
),
bottomNavigationBar: Theme(
data: Theme.of(context).copyWith(
canvasColor: Theme.of(context).primaryColor,
primaryColor: Theme.of(context).accentColor,
textTheme: Theme.of(context).textTheme.copyWith(caption: TextStyle(color: Colors.grey[400]),),
),
child: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(
Feather.getIconData("home"),
),
title: Container(height: 0.0),
),
BottomNavigationBarItem(
icon: Icon(
Feather.getIconData("file"),
),
title: Container(height: 0.0),
),
BottomNavigationBarItem(
icon: Icon(
Feather.getIconData("search"),
),
title: Container(height: 0.0),
),
BottomNavigationBarItem(
icon: Icon(
Feather.getIconData("bookmark"),
),
title: Container(height: 0.0),
),
],
onTap: navigationTapped,
currentIndex: _page,
),
),
);
}

void navigationTapped(int page){
_pageController.jumpToPage(page);
}
@override
void initState(){
super.initState();
_pageController = PageController(initialPage: 0);
}
@override
void dispose() {
super.dispose();
_pageController.dispose();
}
void onPageChanged(int page){
setState(() {
this._page = page;
});
}
}

最佳答案

因此,在您的 PageView 中,您列出了 children 这些页面应该与您的选项卡相对应。

目前您的 Home 小部件列出了 4 次,当您单击选项卡时,这显然不会显示任何差异。

如果你像这样替换 make your children 它应该可以正常工作

children: [Home(), History(), Search(), Bookmarks()]

关于android - 导航和路由 Flutter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57418412/

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