Flutter自定义TabBar插件banai_tabbar的使用

Flutter自定义TabBar插件banai_tabbar的使用

该插件旨在解决Flutter中标签栏字体放大和缩小时的抖动问题。

  1. labelStylefontSize 属性不能设置。
  2. unselectedLabelStyle 必须设置 fontSize 属性。
  3. unselectedLabelStylefontSize 属性必须大于0。
  4. labelFontSize 替换 labelStylefontSize 属性。

预览

视频链接

https://user-images.githubusercontent.com/21274377/128660549-64f90736-beb6-4ab4-b45e-fd4245765c79.mp4

开始使用

pubspec.yaml 文件中添加依赖:

dependencies:
  banai_tabbar: ^最新版本

使用非常简单

以下是一个完整的示例代码:

import 'dart:ui';

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

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

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

class _MyAppState extends State<MyApp> with AutomaticKeepAliveClientMixin, SingleTickerProviderStateMixin {
  [@override](/user/override)
  bool get wantKeepAlive => true;

  late TabController _tabConroller;
  List tablist = [
    '推荐',
    '娱乐',
    '体育',
    '军事',
    '国际',
    '科技',
    '汽车',
    '我关注的',
  ];

  [@override](/user/override)
  void initState() {
    super.initState();
    _tabConroller = TabController(vsync: this, length: tablist.length, initialIndex: 3);
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    super.build(context);
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('插件Banai_Appbar示例'),
        ),
        body: Column(
          children: [
            Container(
              height: 50,
              width: MediaQuery.of(context).size.width, // 获取当前屏幕宽度
              child: BanaiTabbar(
                controller: _tabConroller,
                isScrollable: true, // 是否可滚动
                indicator: const BoxDecoration(), // 指示器样式
                unselectedLabelColor: Colors.black38, // 未选中标签颜色
                labelColor: Colors.pinkAccent, // 选中标签颜色
                unselectedLabelStyle: TextStyle(
                  fontSize: 16, // 未选中标签字体大小
                ),
                labelFontSize: 30, // 选中标签字体大小
                onChange: (int currentIndex) { // 切换标签时的回调
                  print(currentIndex);
                },
                onAnimatedChange: (double animatedValue, double diff, int currentIndex, int nextIndex) { // 动画变化时的回调
                  print("动画值: $animatedValue  差值: $diff  当前索引: $currentIndex  下一个索引: $nextIndex");
                },
                tabs: [
                  for (var i = 0; i < tablist.length; i++)
                    Text(tablist[i]) // 动态生成标签
                ],
              ),
            ),
            Expanded(
              child: TabBarView(
                controller: _tabConroller,
                children: [
                  for (var i = 0; i < tablist.length; i++)
                    Text(tablist[i]) // 动态生成标签页面
                ]
              )
            )
          ],
        ),
      ),
    );
  }
}

更多关于Flutter自定义TabBar插件banai_tabbar的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter自定义TabBar插件banai_tabbar的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,下面是一个关于如何在Flutter项目中使用自定义TabBar插件banai_tabbar的代码案例。假设你已经将banai_tabbar插件添加到了你的pubspec.yaml文件中。

首先,确保你的pubspec.yaml文件中包含以下依赖:

dependencies:
  flutter:
    sdk: flutter
  banai_tabbar: ^最新版本号 # 请替换为实际版本号

然后,运行flutter pub get来安装依赖。

接下来,让我们看一个使用banai_tabbar的完整示例。

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter BanaiTabBar Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

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

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

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('BanaiTabBar Demo'),
        bottom: PreferredSize(
          preferredSize: Size.fromHeight(kToolbarHeight),
          child: BanaiTabBar(
            controller: _tabController,
            tabs: [
              BanaiTab(
                icon: Icon(Icons.home),
                title: Text('Home'),
              ),
              BanaiTab(
                icon: Icon(Icons.search),
                title: Text('Search'),
              ),
              BanaiTab(
                icon: Icon(Icons.account_circle),
                title: Text('Profile'),
              ),
            ],
            indicatorColor: Colors.blue,
            indicatorSize: BanaiTabBarIndicatorSize.label,
          ),
        ),
      ),
      body: TabBarView(
        controller: _tabController,
        children: [
          Center(child: Text('Home Tab')),
          Center(child: Text('Search Tab')),
          Center(child: Text('Profile Tab')),
        ],
      ),
    );
  }
}

在这个示例中,我们做了以下几件事:

  1. 创建主应用MyApp是一个无状态小部件,它设置了应用的主题并指定了主页。
  2. 创建主页MyHomePage是一个有状态小部件,它包含了TabController来管理标签页的切换。
  3. 初始化TabController:在initState方法中初始化TabController
  4. 释放资源:在dispose方法中释放TabController资源。
  5. 构建UI:在build方法中,我们使用了Scaffold小部件来构建应用的主体结构。在AppBar的底部,我们使用了BanaiTabBar来创建自定义的标签栏。每个标签通过BanaiTab小部件定义,包括图标和标题。TabBarView用于显示与标签对应的内容。

请确保你已经正确安装并导入了banai_tabbar插件,并根据实际版本更新依赖项。这个示例展示了如何使用banai_tabbar来创建一个自定义的标签栏,并处理标签页之间的切换。

回到顶部