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点击切换动画

相关推荐
肥肥呀呀呀8 小时前
flutter getx 中.obs 的方法refresh方法
flutter
只可远观8 小时前
Flutter 泛型 泛型方法 泛型类 泛型接口
服务器·windows·flutter
肥肥呀呀呀8 小时前
ipa包安装到apple手机上
flutter
桃花仙丶10 小时前
iOS/Flutter混合开发之PlatformView配置与使用
flutter·ios·xcode·swift·dart
一名普通的程序员14 小时前
Flutter GetX 核心技巧:GetWidget 的深度解析
flutter
一名普通的程序员14 小时前
Flutter GetX 核心技巧:GetView / GetWidget 的魔法解密
flutter
只可远观14 小时前
Mac搭建Flutter IOS环境详细指南
前端·flutter·macos·ios
一名普通的程序员15 小时前
Flutter GetX 状态管理新姿势:Rx扩展方法实践指南
flutter
一名普通的程序员15 小时前
GetX框架里容易被忽略的那些小知识(六)
flutter
孤鸿玉15 小时前
[Flutter小试牛刀] 低配版signals,添加局部刷新
前端·flutter