Flutter tabbar上的badge

文章介绍了如何在Flutter应用中使用`pubspec.yaml`引入badges库,并展示了如何在`BottomNavigationBarItem`中动态显示badge,根据索引改变颜色和内容,如未读计数。

1、pubspec.yaml添加

badges: ^3.1.1

2、flutter put get

3、传入index为显示badge的位置

BottomNavigationBarItem _buildTabs(int index) {
    return BottomNavigationBarItem(

      label: _tabs[index],
      icon: badges.Badge(
        badgeStyle: badges.BadgeStyle(
          badgeColor: index == 1 ? Colors.red :Colors.transparent,
        ),
        position: badges.BadgePosition.topEnd(top: -14),
        badgeContent: index == 1 ? Text(

          '$unreadCount',
          style: const TextStyle(color: Colors.white,fontSize: 10),
        ):null,

        child: Image.asset(
          "images/${_tabIcons[index]}${index == _checkIndex ? "c" : "n"}.png",
          width: 24,
          height: 24,
        ),
      ),
    );
  }

### FlutterTabBar 的用法及常见问题解决 #### 1. 系统默认 TabBar 实现 在 Flutter 中,`TabBar` 是一个常见的 UI 组件,用于在不同内容区域之间快速切换。系统默认的 `TabBar` 实现可以通过 `DefaultTabController` 或手动创建 `TabController` 来完成[^2]。以下是一个简单的实现示例: ```dart import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: DefaultTabController( length: 3, child: Scaffold( appBar: AppBar( bottom: TabBar( tabs: [ Tab(icon: Icon(Icons.home)), Tab(icon: Icon(Icons.search)), Tab(icon: Icon(Icons.person)), ], ), title: Text('Flutter TabBar Example'), ), body: TabBarView( children: [ Center(child: Text('Home')), Center(child: Text('Search')), Center(child: Text('Profile')), ], ), ), ), ); } } ``` 上述代码展示了如何使用 `DefaultTabController` 创建一个包含三个标签的 `TabBar`,每个标签对应一个页面。 #### 2. 使用第三方库实现自定义 TabBar 如果需要更复杂的样式或功能,可以借助第三方库,例如 `flutter-cupertino-tabbar` 项目[^1]。该项目提供了一个类似 iOS 风格的底部导航栏,支持多种自定义选项。以下是其基本用法: ```dart import 'package:flutter/cupertino.dart'; import 'package:flutter_cupertino_tabbar/flutter_cupertino_tabbar.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return CupertinoApp( home: CupertinoTabScaffold( tabBar: CupertinoTabBar( items: [ BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'), BottomNavigationBarItem(icon: Icon(Icons.search), label: 'Search'), BottomNavigationBarItem(icon: Icon(Icons.person), label: 'Profile'), ], ), tabBuilder: (context, index) { switch (index) { case 0: return Center(child: Text('Home')); case 1: return Center(child: Text('Search')); case 2: return Center(child: Text('Profile')); default: return Container(); } }, ), ); } } ``` 通过 `CupertinoTabBar`,可以轻松实现类似于 iOS 的底部导航栏效果[^1]。 #### 3. 保持页面状态 在使用 `TabBar` 和 `TabBarView` 进行页面切换时,页面状态可能会被销毁。为了解决这一问题,可以结合 `AutomaticKeepAliveClientMixin` 或其他方法来保持页面状态[^4]。 以下是一个使用 `AutomaticKeepAliveClientMixin` 的示例: ```dart class KeepAlivePage extends StatefulWidget { @override _KeepAlivePageState createState() => _KeepAlivePageState(); } class _KeepAlivePageState extends State<KeepAlivePage> with AutomaticKeepAliveClientMixin { @override bool get wantKeepAlive => true; @override Widget build(BuildContext context) { super.build(context); return Center(child: Text('This page will not be rebuilt on tab switch')); } } ``` 将此页面作为 `TabBarView` 的子项时,页面状态将被保留[^4]。 #### 4. 监听 Tab 切换事件 如果需要监听 `TabBar` 的切换事件,可以通过 `TabController` 的 `addListener` 方法实现[^3]。以下是一个示例: ```dart class TabBarExample extends StatefulWidget { @override _TabBarExampleState createState() => _TabBarExampleState(); } class _TabBarExampleState extends State<TabBarExample> with SingleTickerProviderStateMixin { late TabController _tabController; @override void initState() { super.initState(); _tabController = TabController(length: 3, vsync: this); _tabController.addListener(() { print('Current index: ${_tabController.index}'); }); } @override void dispose() { _tabController.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( bottom: TabBar( controller: _tabController, tabs: [ Tab(text: 'Home'), Tab(text: 'Search'), Tab(text: 'Profile'), ], ), ), body: TabBarView( controller: _tabController, children: [ Center(child: Text('Home')), Center(child: Text('Search')), Center(child: Text('Profile')), ], ), ); } } ``` 上述代码展示了如何通过 `TabController` 监听 `TabBar` 的切换事件[^3]。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

允澄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值