main_tab_page.dart 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:lszlgl/base/base_state.dart';
  4. import 'package:lszlgl/page/home/home_page.dart';
  5. import 'package:lszlgl/page/user_center/user_center_page.dart';
  6. /// 主页tab页面
  7. class MainTabPage extends StatefulWidget {
  8. const MainTabPage({Key? key}) : super(key: key);
  9. @override
  10. State<MainTabPage> createState() => _MainTabPageState();
  11. }
  12. class _MainTabPageState extends State<MainTabPage> {
  13. late List<String> tabTextList;
  14. late List<String> tabIconList;
  15. late List<String> tabIconSelectList;
  16. late List<Widget> pageList;
  17. late PageController ctrl;
  18. int selectedIndex = 0;
  19. DateTime? lastTime;
  20. ValueNotifier<bool> popState = ValueNotifier(false);
  21. void refreshTab(int index, {bool refreshPage = false}) {
  22. if (index == selectedIndex) return;
  23. setState(() {
  24. selectedIndex = index;
  25. });
  26. if (refreshPage) ctrl.jumpToPage(index);
  27. }
  28. @override
  29. void initState() {
  30. super.initState();
  31. tabTextList = ['首页', '用户中心'];
  32. tabIconList = [imgNavHome, imgNavUserCenter];
  33. tabIconSelectList = [imgNavHomeSelect, imgNavUserCenterSelect];
  34. pageList = [const HomePage(), const UserCenterPage()];
  35. ctrl = PageController(initialPage: selectedIndex);
  36. }
  37. @override
  38. Widget build(BuildContext context) {
  39. return Scaffold(
  40. backgroundColor: const Color(0xFFF3F3F3),
  41. body: buildPop(buildBody()),
  42. bottomNavigationBar: buildNavigation(),
  43. );
  44. }
  45. Widget buildPop(Widget child) {
  46. return ValueListenableBuilder<bool>(
  47. valueListenable: popState,
  48. builder: (_, value, __) {
  49. return PopScope(
  50. canPop: value,
  51. child: child,
  52. onPopInvoked: (didPop) {
  53. if (didPop) return;
  54. MyNavigator.showToast('再试一次退出');
  55. popState.value = true;
  56. // 2秒后不可退出
  57. Timer(
  58. const Duration(seconds: 2),
  59. () => popState.value = false,
  60. );
  61. },
  62. );
  63. },
  64. );
  65. }
  66. Widget buildBody() {
  67. return PageView.builder(
  68. controller: ctrl,
  69. // physics: const BouncingScrollPhysics(),
  70. onPageChanged: refreshTab,
  71. itemCount: pageList.length,
  72. itemBuilder: (_, index) => pageList[index],
  73. );
  74. }
  75. Widget buildNavigation() {
  76. return NavigationBar(
  77. height: 56,
  78. selectedIndex: selectedIndex,
  79. surfaceTintColor: Colors.white,
  80. onDestinationSelected: (index) => refreshTab(index, refreshPage: true),
  81. backgroundColor: Colors.white,
  82. destinations: List.generate(
  83. tabTextList.length,
  84. (index) => GestureDetector(
  85. behavior: HitTestBehavior.opaque,
  86. onTap: () => refreshTab(index, refreshPage: true),
  87. child: Column(
  88. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  89. children: [
  90. Image.asset(
  91. index == selectedIndex ? tabIconSelectList[index] : tabIconList[index],
  92. height: 18,
  93. ),
  94. Text(
  95. tabTextList[index],
  96. style: TextStyle(
  97. fontSize: 12,
  98. fontWeight: FontWeight.w500,
  99. color: index == selectedIndex ? const Color(0xFF333333) : const Color(0xFF888888),
  100. ),
  101. ),
  102. ],
  103. ),
  104. ),
  105. ).toList(),
  106. );
  107. }
  108. }