flutter tab页面切换练手,手势滑动、禁止滑动、page切换动画,禁止切换动画。

1:AppBar、TabBar、TabBarView实现页面切换,点击tab后tabBarView有左右切换动画,滑动page联动tabBar

Dart 复制代码
class DevicePage extends StatefulWidget {
  const DevicePage({super.key});

  @override
  State<DevicePage> createState() => _DeviceState();
}

class _DeviceState extends State<DevicePage>
    with SingleTickerProviderStateMixin {
  TabController? _tabController;

  List<Tab> tabs = [
    const Tab(
      text: '设备类型',
    ),
    const Tab(
      text: '设备规格',
    ),
    const Tab(
      text: '设备台账',
    ),
  ];
  int _cuttentIndex = 0;
  List<Widget> tabViews = [
    const TabDeviceType(),
    const TabDeviceSpecs(),
    const TabDeviceLedger()
  ];

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: tabs.length, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: false,
        backgroundColor: AppColors.baseColor,
        centerTitle: true,
        title: TabBar(
          controller: _tabController,
          tabs: tabs,
          isScrollable: false,
          labelStyle: TextStyle(
              color: Colors.white,
              fontSize: 16.sp,
              fontWeight: FontWeight.bold),
          unselectedLabelStyle: TextStyle(
              color: const Color(0x99FFFFFF),
              fontSize: 15.sp,
              fontWeight: FontWeight.normal),
          indicatorColor: Colors.white,
          dividerHeight: 0,
          indicatorPadding: EdgeInsets.only(top: 3.h),
          indicator:
              MyCustomIndicator(indWidth: 13.w, indHeight: 3.w, radius: 2.w),
          // onTap:(int index){   针对点击tab page无切换动画
          //   _cuttentIndex = index;
          //   print('index = $index');
          //   setState(() {}); //刷新状态
          // },
        ),
      ),
      body: TabBarView(
        controller: _tabController,
        children: tabViews,
        // physics: new NeverScrollableScrollPhysics(),  //禁止页面左右手势滑动
      ),
      // body: tabViews[_cuttentIndex],
    );
  }
}

2:只tab点击切换,带page切换动画,关闭page手势切换。

3:关闭Tab点击切换动画

相关推荐
kirk_wang5 小时前
Flutter 导航锁踩坑实录:从断言失败到类型转换异常
前端·javascript·flutter
往来凡尘6 小时前
Flutter运行iOS26真机的两个问题
flutter·ios
yfmingo9 小时前
flutter项目大量使用.obs会导致项目性能极度下降吗
flutter
山璞9 小时前
Flutter3.32 中使用 webview4.13 与 vue3 项目的 h5 页面通信,以及如何调试
前端·flutter
ezeroyoung9 小时前
环信em_chat_uikit(Flutter)适配鸿蒙
flutter·华为·harmonyos
恋猫de小郭9 小时前
再次紧急修复,Flutter 针对 WebView 无法点击问题增加新的快速修复
android·前端·flutter
庄雨山11 小时前
Flutter+Riverpod+开源鸿蒙:新一代跨端状态管理实战
flutter·开源·openharmonyos
遝靑12 小时前
深入 Flutter 底层:自定义 RenderObject 实现高性能异形列表项
flutter