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

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


本插件旨在更新Flutter版本。我们仍然尊重原始插件convex_bottom_bar


官方的BottomAppBar只能展示一个带缺口的FAB(浮动操作按钮)和一个AppBar,有时我们需要一个凸起的FAB。BottomAppBar和NotchShape的实现启发了这个ConvexAppBar。

以下是一些支持的样式:

固定 反应式 徽章芯片
固定圆形 反应圆形 翻转
文字内嵌 带标题 选项卡图像
按钮 固定角 -
-

如何使用

通常情况下,ConvexAppBar可以与Scaffold一起工作,通过设置其bottomNavigationBar

ConvexAppBar有两个构造函数。ConvexAppBar()将使用默认样式来简化标签创建。

在你的包的pubspec.yaml文件中添加此内容,并使用最新版本:

dependencies:
  convex_bottom_bar_renew: ^latest_version

示例代码:

import 'package:convex_bottom_bar_renew/convex_bottom_bar_renew.dart';

Scaffold(
  bottomNavigationBar: ConvexAppBar(
    items: [
      TabItem(icon: Icons.home, title: 'Home'),
      TabItem(icon: Icons.map, title: 'Discovery'),
      TabItem(icon: Icons.add, title: 'Add'),
      TabItem(icon: Icons.message, title: 'Message'),
      TabItem(icon: Icons.people, title: 'Profile'),
    ],
    onTap: (int i) => print('click index=$i'),
  )
);

Flutter版本支持

由于Flutter发展迅速,可能会有破坏性变更。我们将尝试通过不同的包版本支持稳定版和测试版。

稳定版Flutter版本 包版本 备注
>=3.7.0 >=3.2.0 自v3.7.0起,稳定版更改了DefaultTabController api
>=1.20 >=2.4.0 自v1.20起,稳定版更改了Stack api
<1.20 <=2.3.0 支持如v1.17、v1.12等稳定版不再更新

特性

  • 提供多种内部样式
  • 可以改变AppBar的主题
  • 提供builder API来自定义新样式
  • 在标签菜单上添加徽章
  • 提供优雅的过渡动画
  • 提供hook API覆盖一些内部样式
  • 支持RTL

目录

主题化

条形会使用默认样式,你可能想要对其进行主题化。以下是支持的一些属性:

属性 描述
backgroundColor AppBar背景
gradient 渐变会覆盖backgroundColor
height AppBar高度
color 标签图标/文本颜色
activeColor 标签图标/文本颜色(选中时)
curveSize 凸形的大小
top 凸形相对于AppBar的顶部边缘
cornerRadius 绘制带有左上角和右上角的背景;仅适用于固定标签样式
style 描述凸形样式的样式:fixed, fixedCircle, react, reactCircle
chipBuilder 自定义徽章构建器,使用ConvexAppBar.badge进行默认徽章

徽章

如果你需要在标签上添加徽章,使用ConvexAppBar.badge即可完成。

示例代码:

ConvexAppBar.badge({
  0: '99+',
  1: Icons.assistant_photo,
  2: Colors.redAccent
},
items: [
  TabItem(icon: Icons.home, title: 'Home'),
  TabItem(icon: Icons.map, title: 'Discovery'),
  TabItem(icon: Icons.add, title: 'Add'),
],
onTap: (int i) => print('click index=$i'),
);

badge()方法接受一个徽章数组;badges是一个键值对映射,每个条目值可以是StringIconDataColorWidget

单按钮

如果你只需要一个单按钮,可以查看ConvexButton

示例代码:

Scaffold(
  appBar: AppBar(title: const Text('ConvexButton Example')),
  body: Center(child: Text('count $count')),
  bottomNavigationBar: ConvexButton.fab(
    onTap: () => setState(() => count++),
  ),
);

样式钩子

用于内部标签样式。与ConvexAppBar.builder不同,你可能希望在不定义新标签样式的情况下更新标签样式。

警告:

此钩子功能有限,如果提供的大小与内部样式不匹配,则可能导致溢出错误

示例代码:

StyleProvider(
  style: Style(),
  child: ConvexAppBar(
    initialActiveIndex: 1,
    height: 50,
    top: -30,
    curveSize: 100,
    style: TabStyle.fixedCircle,
    items: [
      TabItem(icon: Icons.link),
      TabItem(icon: Icons.import_contacts),
      TabItem(title: "2020", icon: Icons.work),
    ],
    backgroundColor: _tabBackgroundColor,
  ),
)
class Style extends StyleHook {
  [@override](/user/override)
  double get activeIconSize => 40;

  [@override](/user/override)
  double get activeIconMargin => 10;

  [@override](/user/override)
  double get iconSize => 20;

  [@override](/user/override)
  TextStyle textStyle(Color color) {
    return TextStyle(fontSize: 20, color: color);
  }
}

RTL支持

RTL内部支持,并且如果你在应用中定义了TextDirection,AppBar应该可以正常工作。 RTL和LTR都可以通过Directionality进行配置:

Directionality(
  textDirection: TextDirection.rtl,
  child: Scaffold(body:ConvexAppBar(/*TODO ...*/)),
)

自定义示例

如果默认样式不符合您的情况,可以尝试使用ConvexAppBar.builder(),允许您自定义几乎所有的标签功能。

示例代码:

Scaffold(
  bottomNavigationBar: ConvexAppBar.builder(
    count: 5,
    backgroundColor: Colors.blue,
    itemBuilder: Builder(),
  )
);

// 用户自定义类
class Builder extends DelegateBuilder {
  [@override](/user/override)
  Widget build(BuildContext context, int index, bool active) {
    return Text('TAB $index');
  }
}

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

1 回复

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


convex_bottom_bar_renew 是一个用于 Flutter 的底部导航栏插件,它是 convex_bottom_bar 的分支版本,提供了更加灵活和可定制的底部导航栏。以下是如何在 Flutter 项目中使用 convex_bottom_bar_renew 的步骤:

1. 添加依赖

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

dependencies:
  flutter:
    sdk: flutter
  convex_bottom_bar_renew: ^3.0.0

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

2. 导入包

在你的 Dart 文件中导入 convex_bottom_bar_renew

import 'package:convex_bottom_bar_renew/convex_bottom_bar_renew.dart';

3. 创建底部导航栏

接下来,你可以创建一个 ConvexAppBar 并将其添加到 ScaffoldbottomNavigationBar 属性中。

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomeScreen(),
    );
  }
}

class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  int _selectedIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Convex Bottom Bar Example'),
      ),
      body: Center(
        child: Text('Selected Index: $_selectedIndex'),
      ),
      bottomNavigationBar: ConvexAppBar(
        items: [
          TabItem(icon: Icons.home, title: 'Home'),
          TabItem(icon: Icons.map, title: 'Map'),
          TabItem(icon: Icons.add, title: 'Add'),
          TabItem(icon: Icons.message, title: 'Message'),
          TabItem(icon: Icons.people, title: 'Profile'),
        ],
        initialActiveIndex: _selectedIndex,
        onTap: (int index) {
          setState(() {
            _selectedIndex = index;
          });
        },
      ),
    );
  }
}

4. 自定义导航栏

ConvexAppBar 提供了多种自定义选项,例如:

  • 样式:你可以选择不同的样式,比如 ConvexAppBarStyle.fixed, ConvexAppBarStyle.react, ConvexAppBarStyle.flip, 等。
  • 颜色:可以自定义图标的颜色、背景颜色等。
  • 动画:可以启用或禁用动画效果。

例如,你可以这样自定义 ConvexAppBar

ConvexAppBar(
  style: ConvexAppBarStyle.react,
  items: [
    TabItem(icon: Icons.home, title: 'Home'),
    TabItem(icon: Icons.map, title: 'Map'),
    TabItem(icon: Icons.add, title: 'Add'),
    TabItem(icon: Icons.message, title: 'Message'),
    TabItem(icon: Icons.people, title: 'Profile'),
  ],
  initialActiveIndex: _selectedIndex,
  onTap: (int index) {
    setState(() {
      _selectedIndex = index;
    });
  },
  backgroundColor: Colors.deepPurple,
  color: Colors.white,
  activeColor: Colors.amber,
);
回到顶部