Flutter底部导航栏插件bottom_cupertino_tabbar的使用

Flutter底部导航栏插件bottom_cupertino_tabbar的使用

Overview

BottomCupertinoTabbar 是一个 Flutter 插件,用于创建具有 Cupertino(iOS 风格)外观的自定义和直观的底部导航栏。该插件为每个标签提供了独立的导航栈,确保用户在切换标签时保留每个标签的状态。

该插件提供了广泛的自定义选项,包括在标签栏的不同位置添加浮动操作按钮或其他小部件的能力。

BottomCupertinoTabbar 动图

首页预览 通知页预览

pub.dev platform-flutter


Table of Contents

Features

  • Cupertino Aesthetic: 提供具有 iOS 风格设计的底部导航栏,无缝融入旨在实现 iOS 外观和感觉的应用程序。
  • Separate Navigation Stacks: 支持每个标签的独立导航栈,允许在切换标签时保留导航历史记录和每个标签的状态。
  • Customizable Tab Items: 自定义每个标签的活动和非活动状态图标,添加标签文本,显示带有计数的通知徽章等。
  • Customization of the Tab Bar: 自定义活动和非活动状态的标签图标和文本颜色,设置通知徽章的颜色,确定初始活动的标签等。
  • Floating Action Button (FAB) or Custom Widgets: 将浮动操作按钮或其他小部件集成到标签栏中,灵活地将其放置在中心或任何其他 FloatingActionButtonLocation
  • Responsive and Adaptive: 设计为响应式并适应不同的屏幕尺寸和方向。
  • Easy Integration: 可轻松集成到现有的 Flutter 项目中,增强导航体验而无需对应用结构进行大量更改。
  • State Preservation Across Tabs: 确保在用户切换标签时保留每个标签内的状态(如用户输入、滚动位置等),提供无缝的用户体验。
  • Customizable Behavior on Tab Presses: onTabPressed 回调允许在选择标签时自定义行为,如弹出到堆栈的第一个路由、刷新数据等。

Installation

pubspec.yaml 文件中添加 bottom_cupertino_tabbar 作为依赖项。

dependencies:
  bottom_cupertino_tabbar: ^latest_version

Import

在需要使用该插件的地方导入包。

import 'package:bottom_cupertino_tabbar/bottom_cupertino_tabbar.dart';

Basic Usage

BottomCupertinoTabbar 可以在一个 StatefulWidget 中使用,以便有效地实现。

class SimpleTabBar extends StatefulWidget {
  const SimpleTabBar({super.key});

  @override
  State<SimpleTabBar> createState() => _SimpleTabBarState();
}

class _SimpleTabBarState extends State<SimpleTabBar> {
  @override
  Widget build(BuildContext context) {
    return BottomCupertinoTabbar(
      // 添加你的配置和自定义选项
    );
  }
}

以下是一个简单的示例,展示了如何使用 BottomCupertinoTabbar 创建一个包含 Home、Notifications、Contacts 和 Settings 标签的底部导航栏。每个标签都链接到相应的页面和图标,并且状态和导航栈得以保留。

首页预览 通知页预览

Customization

BottomCupertinoTabbar 可以广泛自定义:

  • Floating Action Button: 可以在标签栏的中心或任何 FloatingActionButtonLocation 位置添加浮动操作按钮(FAB)或其他小部件。这允许进行额外的操作和独特的设计。
  • Custom Icons and Labels: 每个标签可以有自定义图标和标签,允许个性化外观。你可以为活动和非活动状态设置图标。
  • Custom Colors and Themes: 自定义颜色方案和主题以匹配你的应用设计。你可以为活动和非活动状态设置颜色。
  • Adjustable Size and Positioning: 可以调整标签栏及其元素的大小和位置以适应布局需求。

BottomCupertinoTabbar 主要自定义选项

属性 描述
activeColor 活动标签图标的颜色
inactiveColor 非活动标签图标的颜色
notificationsBadgeColor 通知徽章的颜色
firstActiveIndex 初始活动标签的索引
resizeToAvoidBottomInset 当键盘出现时调整底部栏的大小
showLabels 显示图标下方的标签
onTabPressed 选择标签时的回调函数,可用于自定义每个标签的行为和访问导航器
children 表示标签栏中每个标签的 BottomCupertinoTab 小部件列表
floatingActionButton 在标签栏上显示的可选浮动操作按钮(或其他小部件)
floatingActionButtonLocation 浮动操作按钮在标签栏上的位置
resizeToAvoidBottomInset 控制当屏幕键盘出现时是否调整底部栏的大小
backgroundColor 标签栏的背景颜色,默认为 Colors.white
showLabels 控制是否显示标签项上的标签
appBar BottomCupertinoTabbar 元素顶部显示的可选 AppBar
overrideIconsColor 控制是否覆盖图标的颜色

Tabs and State Preservation

标签使用 BottomCupertinoTab 小部件创建。每个标签都有自己的导航栈和状态管理,保留用户交互和导航历史记录。这是 BottomCupertinoTabbar 包的关键组件之一。它表示底部导航栏中的单个标签。

BottomCupertinoTab

class BottomCupertinoTab {
  final BottomCupertinoTabItem tab;
  final Widget page;

  const BottomCupertinoTab({
    required this.tab,
    required this.page
  });
}

参数

  • tab (BottomCupertinoTabItem): 标签的外观和行为配置。
  • page (Widget): 当标签处于活动状态时显示的小部件。通常是一个 Flutter 页面或屏幕。

每个标签由一个 BottomCupertinoTabItem 进行配置,并且包含一个表示选中标签时显示内容的 Flutter 小部件。

BottomCupertinoTabItem

BottomCupertinoTabItem 类用于配置 BottomCupertinoTabbar 中单个标签的外观和行为。

class BottomCupertinoTabItem {

  /// 要显示的图标
  final Widget? icon;

  /// 标签处于活动状态时的图标
  final Widget? activeIcon;

  /// 标签处于非活动状态时的图标
  final Widget? inactiveIcon;

  /// 标签的文本标签
  final String? label;

  /// 要显示的通知数量
  final int notificationsCounter;

  /// 控制是否显示通知计数器
  final bool showNotifications;

  /// 指示该标签是否被视为占位符
  final bool empty;

  /// 标签处于活动状态时使用的文本样式
  final TextStyle? activeLabelTextStyle;

  /// 标签处于非活动状态时使用的文本样式
  final TextStyle? inactiveLabelTextStyle;

  const BottomCupertinoTabItem({
    this.icon,
    this.activeIcon,
    this.inactiveIcon,
    this.label,
    this.notificationsCounter = 0,
    this.showNotifications = false,
    this.empty = false,
    this.activeLabelTextStyle,
    this.inactiveLabelTextStyle,
  });
}

BottomCupertinoTab 的示例:

BottomCupertinoTab(
  tab: BottomCupertinoTabItem(
    icon: Icon(Icons.home, size: 22,),
    label: "Home",
  ),
  page: HomePage(),
)

Complete Example

以下是完整示例的代码:

首页预览 通知页预览

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

import '../pages/pages.dart';
import 'example_manager/example_manager.dart';

class EasyTabbar extends StatefulWidget {
  const EasyTabbar({super.key});

  @override
  State<EasyTabbar> createState() => _EasyTabbarState();
}

class _EasyTabbarState extends State<EasyTabbar> {
  @override
  Widget build(BuildContext context) {
    return BottomCupertinoTabbar(
      activeColor: Colors.blue,
      inactiveColor: Colors.grey[300]!,
      notificationsBadgeColor: Colors.red,
      firstActiveIndex: 0,
      resizeToAvoidBottomInset: false,
      showLabels: true,
      overrideIconsColor: true,
      tabbarModel: (model, nestedNavigator) {
        ExampleManager().tabbarProviderModel = model;
        ExampleManager().nestedNavigator = nestedNavigator;
      },
      onTabPressed: (index, model, nestedNavigator) {
        if (index != model.currentTab) {
          model.changePage(index);
        } else {
          if (nestedNavigator[index]?.currentContext != null) {
            Navigator.of(nestedNavigator[index]!.currentContext!)
                .popUntil((route) => route.isFirst);
          }
        }
      },
      children: const [
        BottomCupertinoTab(
          tab: BottomCupertinoTabItem(
            icon: Icon(Icons.home, size: 22,),
            label: "Home",
          ),
          page: HomePage(),
        ),
        BottomCupertinoTab(
          tab: BottomCupertinoTabItem(
            icon: Icon(Icons.notifications, size: 22,),
            label: "Notifications",
          ),
          page: NotificationsPage(),
        ),
        BottomCupertinoTab(
          tab: BottomCupertinoTabItem(
            icon: Icon(Icons.contacts, size: 22,),
            notificationsCounter: 4,
            showNotifications: true,
            label: "Contacts",
          ),
          page: ContactsPage(),
        ),
        BottomCupertinoTab(
          tab: BottomCupertinoTabItem(
            icon: Icon(Icons.settings, size: 22,),
            label: "Settings",
          ),
          page: SettingsPage(),
        ),
      ],
    );
  }
}

更多示例可以在包的 example 文件夹中找到。

Conclusion

BottomCupertinoTabbar 是一个理想的 Flutter 应用程序插件,适用于需要具有高级自定义功能的底部导航栏,包括在不同位置添加浮动操作按钮或其他小部件。该插件符合原生 iOS 导航模式,并通过其状态保留和广泛的自定义功能增强了用户体验。

Donate

喜欢这个插件吗?考虑捐赠一杯咖啡或啤酒 :)

Buy Me A Coffee


更多关于Flutter底部导航栏插件bottom_cupertino_tabbar的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter底部导航栏插件bottom_cupertino_tabbar的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是一个关于如何使用 bottom_cupertino_tabbar 插件在 Flutter 中实现底部导航栏的示例代码。这个插件提供了一个类似 iOS Cupertino 风格的底部导航栏。

首先,你需要在 pubspec.yaml 文件中添加依赖项:

dependencies:
  flutter:
    sdk: flutter
  bottom_cupertino_tabbar: ^2.0.0  # 请检查最新版本号并替换

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

接下来是完整的示例代码,展示如何在 Flutter 应用中使用 bottom_cupertino_tabbar

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

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

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

class BottomCupertinoTabBarDemo extends StatefulWidget {
  @override
  _BottomCupertinoTabBarDemoState createState() => _BottomCupertinoTabBarDemoState();
}

class _BottomCupertinoTabBarDemoState extends State<BottomCupertinoTabBarDemo> with SingleTickerProviderStateMixin {
  int _currentIndex = 0;
  final List<Widget> _children = [
    Center(child: Text('Home Page')),
    Center(child: Text('Search Page')),
    Center(child: Text('Profile Page')),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _children[_currentIndex],
      bottomNavigationBar: BottomCupertinoTabBar(
        currentIndex: _currentIndex,
        onTabSelected: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
        items: [
          BottomCupertinoTabItem(icon: Icon(Icons.home), title: 'Home'),
          BottomCupertinoTabItem(icon: Icon(Icons.search), title: 'Search'),
          BottomCupertinoTabItem(icon: Icon(Icons.person), title: 'Profile'),
        ],
        activeColor: Colors.blue,
        inactiveColor: Colors.grey,
        backgroundColor: Colors.white,
      ),
    );
  }
}

解释

  1. 依赖项添加

    • pubspec.yaml 中添加 bottom_cupertino_tabbar 依赖项。
  2. 主应用入口

    • MyApp 是主应用类,它使用 MaterialApp 包装整个应用,并设置主题和主页。
  3. 主页

    • BottomCupertinoTabBarDemo 是一个有状态的组件,它管理底部导航栏的当前索引和子页面。
    • 使用 StatefulWidgetSingleTickerProviderStateMixin 来处理状态。
  4. 底部导航栏

    • BottomCupertinoTabBar 用于创建类似 iOS Cupertino 风格的底部导航栏。
    • currentIndex 表示当前选中的索引。
    • onTabSelected 是一个回调,当选中一个标签时调用,更新当前索引。
    • items 是一个 BottomCupertinoTabItem 列表,每个项包含图标和标题。
    • activeColorinactiveColor 分别表示激活和未激活的标签颜色。
    • backgroundColor 设置导航栏的背景颜色。

这样,你就可以在 Flutter 应用中使用 bottom_cupertino_tabbar 插件实现一个类似 iOS 风格的底部导航栏了。

回到顶部