当前位置:   article > 正文

Flutter 之Tab切换效果_flutter tab

flutter tab

 

 1、点击tab实现自由切换页面

一、首先在Flutter中一切皆组件,万物皆组件,都可以用组件来表达

1、创建一个Flutter界面

  1. class MainPage extends StatelessWidget {
  2. const MainPage({Key key}) : super(key: key);
  3. @override
  4. Widget build(BuildContext context) {
  5. return TabNavigator();
  6. }
  7. }

2、在创建TabNavigator组件,继承StatefulWidget,然后使用PageViewBottomNavigationBar 实现滑动和切换

首先重写InitState方法,在了里边初始化需要用到的资源和变量

其次因为PageView需要被控制,所以我们需要创建一个PageController里边传入当前选中的下标

  _controller = PageController(initialPage: _currentIndex);

在实现PageView.Builder事件然后再实现OnPageChanged事件中重新设置setState重新赋值给下标变量

  1. setState(() {
  2. _currentIndex = index;
  3. });

在 bottomNavigationBar的参数中实现BottomNavigationBar,currentIndex就是当前选中的下标,

type这是为Fixed一直显示,onTap事件在跳转Page和赋值Index,最后items传入对应得图标和文字

以下是源代码:

  1. /*@ClassName: TabNavigator
  2. *@Author: wanXiaoFan
  3. *@Date: 2021/8/30 15:46
  4. *@Email: wanyunfei_it@163.com
  5. *@Description: 导航栏
  6. *@changeRecord [修改记录]<br />
  7. */
  8. class TabNavigator extends StatefulWidget {
  9. const TabNavigator({Key key}) : super(key: key);
  10. @override
  11. _TabNavigatorState createState() => _TabNavigatorState();
  12. }
  13. class _TabNavigatorState extends State<TabNavigator> {
  14. var _currentIndex = 0;
  15. PageController _controller;
  16. final _pages = [HomePage(), CarPage(), LivePage(), StarPage(), UserPage()];
  17. @override
  18. void initState() {
  19. super.initState();
  20. _controller = PageController(initialPage: _currentIndex);
  21. }
  22. @override
  23. Widget build(BuildContext context) {
  24. return Scaffold(
  25. body: PageView.builder(
  26. itemBuilder: (BuildContext context, int index) {
  27. return _pages[index];
  28. },
  29. //禁止滑动
  30. physics: NeverScrollableScrollPhysics(),
  31. controller: _controller,
  32. onPageChanged: (index) {
  33. setState(() {
  34. _currentIndex = index;
  35. });
  36. },
  37. ),
  38. bottomNavigationBar: BottomNavigationBar(
  39. currentIndex: _currentIndex,
  40. type: BottomNavigationBarType.fixed,
  41. onTap: (index) {
  42. _controller.jumpToPage(index);
  43. setState(() {
  44. _currentIndex = index;
  45. });
  46. },
  47. items: [
  48. BottomNavigationBarItem(
  49. icon: Image.asset(
  50. "image/main/ic_home_normal.png",
  51. width: AppColors.navBarIconSize,
  52. height: AppColors.navBarIconSize,
  53. ),
  54. activeIcon: Image.asset(
  55. "image/main/ic_home_pressed.png",
  56. width: AppColors.navBarIconSize,
  57. height: AppColors.navBarIconSize,
  58. ),
  59. title: Text(
  60. "首页",
  61. style: TextStyle(
  62. color: _currentIndex != 0
  63. ? Color(AppColors.navBarNormal)
  64. : Color(AppColors.navBarPressed)),
  65. ),
  66. ),
  67. BottomNavigationBarItem(
  68. icon: Image.asset(
  69. "image/main/ic_car_normal.png",
  70. width: AppColors.navBarIconSize,
  71. height: AppColors.navBarIconSize,
  72. ),
  73. activeIcon: Image.asset(
  74. "image/main/ic_car_pressed.png",
  75. width: AppColors.navBarIconSize,
  76. height: AppColors.navBarIconSize,
  77. ),
  78. title: Text(
  79. "选车",
  80. style: TextStyle(
  81. color: _currentIndex != 1
  82. ? Color(AppColors.navBarNormal)
  83. : Color(AppColors.navBarPressed)),
  84. ),
  85. ),
  86. BottomNavigationBarItem(
  87. icon: Image.asset(
  88. "image/main/ic_live_normal.png",
  89. width: AppColors.navBarIconSize,
  90. height: AppColors.navBarIconSize,
  91. ),
  92. activeIcon: Image.asset(
  93. "image/main/ic_live_pressed.png",
  94. width: AppColors.navBarIconSize,
  95. height: AppColors.navBarIconSize,
  96. ),
  97. title: Text(
  98. "直播",
  99. style: TextStyle(
  100. color: _currentIndex != 2
  101. ? Color(AppColors.navBarNormal)
  102. : Color(AppColors.navBarPressed)),
  103. ),
  104. ),
  105. BottomNavigationBarItem(
  106. icon: Image.asset(
  107. "image/main/ic_star_normal.png",
  108. width: AppColors.navBarIconSize,
  109. height: AppColors.navBarIconSize,
  110. ),
  111. activeIcon: Image.asset(
  112. "image/main/ic_star_pressed.png",
  113. width: AppColors.navBarIconSize,
  114. height: AppColors.navBarIconSize,
  115. ),
  116. title: Text(
  117. "星球",
  118. style: TextStyle(
  119. color: _currentIndex != 3
  120. ? Color(AppColors.navBarNormal)
  121. : Color(AppColors.navBarPressed)),
  122. ),
  123. ),
  124. BottomNavigationBarItem(
  125. icon: Image.asset(
  126. "image/main/ic_user_normal.png",
  127. width: AppColors.navBarIconSize,
  128. height: AppColors.navBarIconSize,
  129. ),
  130. activeIcon: Image.asset(
  131. "image/main/ic_user_pressed.png",
  132. width: AppColors.navBarIconSize,
  133. height: AppColors.navBarIconSize,
  134. ),
  135. title: Text(
  136. "我的",
  137. style: TextStyle(
  138. color: _currentIndex != 4
  139. ? Color(AppColors.navBarNormal)
  140. : Color(AppColors.navBarPressed)),
  141. ),
  142. )
  143. ],
  144. ),
  145. );
  146. }
  147. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/431920
推荐阅读
相关标签
  

闽ICP备14008679号