gpt4 book ai didi

带有 PageView 的 Flutter 底部导航栏

转载 作者:行者123 更新时间:2023-12-03 13:29:37 32 4
gpt4 key购买 nike

我想用页面浏览量构建底部导航栏。它将是 3 页,您可以向左或向右过渡。我可以滑动,但我的导航栏选择的项目颜色不会改变。你能帮助我吗?

class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int _selectedIndex = 0;
final PageController _pageController = PageController();

@override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: BottomNavigationBar(
items: [
BottomNavigationBarItem(
icon: Icon(Icons.portrait), title: Text('Profile')),
BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('Home')),
BottomNavigationBarItem(
icon: Icon(Icons.shopping_cart), title: Text('Shop'))
],
onTap: _onTappedBar,
selectedItemColor: Colors.orange,
currentIndex: _selectedIndex,
),
body: PageView(
controller: _pageController,
children: <Widget>[
ProfilePage(),
HomeTables(),
ShoppingPage(),
],
),
);
}

void _onTappedBar(int value) {
setState(() {
_selectedIndex = value;
});
_pageController.jumpToPage(value);
}
}

最佳答案

您只需要使用 onPageChanged PageView 的属性来捕获当前页码。

PageView(
controller: _pageController,
children: <Widget>[
ProfilePage(),
HomeTables(),
ShoppingPage(),
],
onPageChanged: (page) {
setState(() {
_selectedIndex = page;
});
},
);

关于带有 PageView 的 Flutter 底部导航栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61269906/

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