123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- import 'dart:async';
- import 'package:flutter/material.dart';
- import 'package:lszlgl/base/base_state.dart';
- import 'package:lszlgl/page/home/home_page.dart';
- import 'package:lszlgl/page/user_center/user_center_page.dart';
- /// 主页tab页面
- class MainTabPage extends StatefulWidget {
- const MainTabPage({Key? key}) : super(key: key);
- @override
- State<MainTabPage> createState() => _MainTabPageState();
- }
- class _MainTabPageState extends State<MainTabPage> {
- late List<String> tabTextList;
- late List<String> tabIconList;
- late List<String> tabIconSelectList;
- late List<Widget> pageList;
- late PageController ctrl;
- int selectedIndex = 0;
- DateTime? lastTime;
- ValueNotifier<bool> popState = ValueNotifier(false);
- void refreshTab(int index, {bool refreshPage = false}) {
- if (index == selectedIndex) return;
- setState(() {
- selectedIndex = index;
- });
- if (refreshPage) ctrl.jumpToPage(index);
- }
- @override
- void initState() {
- super.initState();
- tabTextList = ['首页', '用户中心'];
- tabIconList = [imgNavHome, imgNavUserCenter];
- tabIconSelectList = [imgNavHomeSelect, imgNavUserCenterSelect];
- pageList = [const HomePage(), const UserCenterPage()];
- ctrl = PageController(initialPage: selectedIndex);
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- backgroundColor: const Color(0xFFF3F3F3),
- body: buildPop(buildBody()),
- bottomNavigationBar: buildNavigation(),
- );
- }
- Widget buildPop(Widget child) {
- return ValueListenableBuilder<bool>(
- valueListenable: popState,
- builder: (_, value, __) {
- return PopScope(
- canPop: value,
- child: child,
- onPopInvoked: (didPop) {
- if (didPop) return;
- MyNavigator.showToast('再试一次退出');
- popState.value = true;
- // 2秒后不可退出
- Timer(
- const Duration(seconds: 2),
- () => popState.value = false,
- );
- },
- );
- },
- );
- }
- Widget buildBody() {
- return PageView.builder(
- controller: ctrl,
- // physics: const BouncingScrollPhysics(),
- onPageChanged: refreshTab,
- itemCount: pageList.length,
- itemBuilder: (_, index) => pageList[index],
- );
- }
- Widget buildNavigation() {
- return NavigationBar(
- height: 56,
- selectedIndex: selectedIndex,
- surfaceTintColor: Colors.white,
- onDestinationSelected: (index) => refreshTab(index, refreshPage: true),
- backgroundColor: Colors.white,
- destinations: List.generate(
- tabTextList.length,
- (index) => GestureDetector(
- behavior: HitTestBehavior.opaque,
- onTap: () => refreshTab(index, refreshPage: true),
- child: Column(
- mainAxisAlignment: MainAxisAlignment.spaceEvenly,
- children: [
- Image.asset(
- index == selectedIndex ? tabIconSelectList[index] : tabIconList[index],
- height: 18,
- ),
- Text(
- tabTextList[index],
- style: TextStyle(
- fontSize: 12,
- fontWeight: FontWeight.w500,
- color: index == selectedIndex ? const Color(0xFF333333) : const Color(0xFF888888),
- ),
- ),
- ],
- ),
- ),
- ).toList(),
- );
- }
- }
|