Flutter滑动标签切换插件swipe_tab_controller的使用

Flutter滑动标签切换插件swipe_tab_controller的使用

Swipe Tab Controller 是一个简单的滑动控制器,用于 TabBarView,它可以根据滑动方向更新标签控制器的索引,并且具有较少的延迟。

示例代码

以下是一个完整的示例代码,展示了如何在 Flutter 应用程序中使用 swipe_tab_controller 插件。

import 'package:flutter/material.dart';
import 'package:swipe_tab_controller/swipe_tab_controller.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Swipe Tab Controller',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Swipe Tab Controller'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int selectedIndex = 0;

  bool isLoading = false;

  void changeLoading(bool value) {
    setState(() => isLoading = value);
  }

  void changeTab(int index) async {
    changeLoading(true);
    setState(() => selectedIndex = index);
    await Future.delayed(const Duration(milliseconds: 1300));
    changeLoading(false);
  }

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 3,
      child: SwipeTabController(
        onTabChanged: changeTab,
        child: Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
            bottom: TabBar(
              labelStyle: Theme.of(context).textTheme.titleLarge,
              indicatorColor: Theme.of(context).colorScheme.primary,
              indicatorWeight: 4,
              indicatorSize: TabBarIndicatorSize.tab,
              onTap: changeTab,
              tabs: const [
                Text('Tab1'),
                Text('Tab2'),
                Text('Tab3'),
              ],
            ),
          ),
          body: TabBarView(children: [
            Center(
              child: isLoading
                  ? const CircularProgressIndicator()
                  : Text(
                      'Tab 1',
                      style: Theme.of(context).textTheme.headlineSmall,
                    ),
            ),
            Center(
              child: isLoading
                  ? const CircularProgressIndicator()
                  : Text(
                      'Tab 2',
                      style: Theme.of(context).textTheme.headlineSmall,
                    ),
            ),
            Center(
              child: isLoading
                  ? const CircularProgressIndicator()
                  : Text(
                      'Tab 3',
                      style: Theme.of(context).textTheme.headlineSmall,
                    ),
            ),
          ]),
        ),
      ),
    );
  }
}

代码解释

  • 导入库:

    import 'package:flutter/material.dart';
    import 'package:swipe_tab_controller/swipe_tab_controller.dart';
    
  • 主应用:

    void main() {
      runApp(const MyApp());
    }
    

    这里定义了主函数并运行 MyApp

  • MyApp类:

    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Swipe Tab Controller',
          theme: ThemeData(
            colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
            useMaterial3: true,
          ),
          home: const MyHomePage(title: 'Swipe Tab Controller'),
        );
      }
    }
    

    定义了应用程序的主题和主页。

  • MyHomePage类:

    class MyHomePage extends StatefulWidget {
      const MyHomePage({super.key, required this.title});
    
      final String title;
    
      @override
      State<MyHomePage> createState() => _MyHomePageState();
    }
    

    定义了带有标题的主页。

  • _MyHomePageState类:

    class _MyHomePageState extends State<MyHomePage> {
      int selectedIndex = 0;
      bool isLoading = false;
    
      void changeLoading(bool value) {
        setState(() => isLoading = value);
      }
    
      void changeTab(int index) async {
        changeLoading(true);
        setState(() => selectedIndex = index);
        await Future.delayed(const Duration(milliseconds: 1300));
        changeLoading(false);
      }
    
      @override
      Widget build(BuildContext context) {
        return DefaultTabController(
          length: 3,
          child: SwipeTabController(
            onTabChanged: changeTab,
            child: Scaffold(
              appBar: AppBar(
                title: Text(widget.title),
                bottom: TabBar(
                  labelStyle: Theme.of(context).textTheme.titleLarge,
                  indicatorColor: Theme.of(context).colorScheme.primary,
                  indicatorWeight: 4,
                  indicatorSize: TabBarIndicatorSize.tab,
                  onTap: changeTab,
                  tabs: const [
                    Text('Tab1'),
                    Text('Tab2'),
                    Text('Tab3'),
                  ],
                ),
              ),
              body: TabBarView(children: [
                Center(
                  child: isLoading
                      ? const CircularProgressIndicator()
                      : Text(
                          'Tab 1',
                          style: Theme.of(context).textTheme.headlineSmall,
                        ),
                ),
                Center(
                  child: isLoading
                      ? const CircularProgressIndicator()
                      : Text(
                          'Tab 2',
                          style: Theme.of(context).textTheme.headlineSmall,
                        ),
                ),
                Center(
                  child: isLoading
                      ? const CircularProgressIndicator()
                      : Text(
                          'Tab 3',
                          style: Theme.of(context).textTheme.headlineSmall,
                        ),
                ),
              ]),
            )),
        );
      }
    }
    

更多关于Flutter滑动标签切换插件swipe_tab_controller的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter滑动标签切换插件swipe_tab_controller的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


swipe_tab_controller 是一个用于在 Flutter 中实现滑动标签切换的插件。它允许你在标签之间进行滑动切换,并且可以与 TabBarTabBarView 无缝集成。以下是如何使用 swipe_tab_controller 的基本步骤:

1. 添加依赖

首先,在你的 pubspec.yaml 文件中添加 swipe_tab_controller 依赖:

dependencies:
  flutter:
    sdk: flutter
  swipe_tab_controller: ^1.0.0

然后运行 flutter pub get 来获取依赖。

2. 导入包

在你的 Dart 文件中导入 swipe_tab_controller 包:

import 'package:swipe_tab_controller/swipe_tab_controller.dart';

3. 使用 SwipeTabController

你可以通过以下步骤来使用 SwipeTabController

3.1 创建 SwipeTabController

首先,创建一个 SwipeTabController 实例。通常你会将其放在 StatefulWidgetState 类中:

class MyHomePage extends StatefulWidget {
  [@override](/user/override)
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
  late SwipeTabController _tabController;

  [@override](/user/override)
  void initState() {
    super.initState();
    _tabController = SwipeTabController(length: 3, vsync: this);
  }

  [@override](/user/override)
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Swipe Tab Example'),
        bottom: TabBar(
          controller: _tabController,
          tabs: const [
            Tab(text: 'Tab 1'),
            Tab(text: 'Tab 2'),
            Tab(text: 'Tab 3'),
          ],
        ),
      ),
      body: TabBarView(
        controller: _tabController,
        children: const [
          Center(child: Text('Content of Tab 1')),
          Center(child: Text('Content of Tab 2')),
          Center(child: Text('Content of Tab 3')),
        ],
      ),
    );
  }
}

3.2 配置 TabBarTabBarView

在你的 Scaffold 中,使用 _tabController 来配置 TabBarTabBarView

  • TabBar 用于显示标签。
  • TabBarView 用于显示与每个标签对应的内容。

3.3 处理滑动事件

SwipeTabController 会自动处理滑动事件。当你滑动 TabBarView 时,TabBar 中的标签会自动切换到对应的位置。

4. 完整示例

以下是一个完整的示例,展示了如何使用 swipe_tab_controller

import 'package:flutter/material.dart';
import 'package:swipe_tab_controller/swipe_tab_controller.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Swipe Tab Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  [@override](/user/override)
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
  late SwipeTabController _tabController;

  [@override](/user/override)
  void initState() {
    super.initState();
    _tabController = SwipeTabController(length: 3, vsync: this);
  }

  [@override](/user/override)
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Swipe Tab Example'),
        bottom: TabBar(
          controller: _tabController,
          tabs: const [
            Tab(text: 'Tab 1'),
            Tab(text: 'Tab 2'),
            Tab(text: 'Tab 3'),
          ],
        ),
      ),
      body: TabBarView(
        controller: _tabController,
        children: const [
          Center(child: Text('Content of Tab 1')),
          Center(child: Text('Content of Tab 2')),
          Center(child: Text('Content of Tab 3')),
        ],
      ),
    );
  }
}
回到顶部