Flutter可扩展底部导航栏插件expandable_bottom_bar_new的使用
ExpandableBottomAppBar
具有可扩展面板的动画底部应用程序栏。
原始包可以在这里找到:https://github.com/rIIh/expandable-bottom-bar
我做了一个分支,因为原包已不再维护。
预览

开始使用
在 pubspec.yaml
文件中添加插件:
dependencies:
...
expandable_bottom_bar_new: 2.0.0-nullsafety.0
然后运行 flutter pub get
来安装依赖。
基本用法
在 Scaffold
中添加 BottomExpandableAppBar
小部件:
bottomNavigationBar: BottomExpandableAppBar(
// 提供控制器,在构建方法中或作为祖先节点的默认控制器
controller: bbc,
expandedHeight: expandedHeight.value,
horizontalMargin: 16,
expandedBackColor: Theme.of(context).backgroundColor,
// 你的底部面板代码
expandedBody: Center(
child: Text("Hello world!"),
),
// 你的底部应用程序栏代码
bottomAppBarBody: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisSize: MainAxisSize.max,
children: [
Expanded(
child: Text(
"Hello",
textAlign: TextAlign.center,
),
),
Spacer(
flex: 2,
),
Expanded(
child: Text(
"World",
textAlign: TextAlign.center,
),
),
],
),
),
)
自定义(可选)
BottomExpandableAppBar
-
horizontalMargin - 面板两侧距离边缘的距离
-
bottomOffset - 关闭状态下顶部面板边缘与应用程序栏边缘之间的距离
-
shape - FAB 的凹口形状
-
appBarHeight - 如果需要更改应用程序栏的高度
-
bottomAppBarColor - 应用程序栏容器的背景颜色 或
-
appBarDecoration - 应用程序栏容器的装饰
-
expandedBackColor - 面板容器的背景颜色 或
-
expandedDecoration - 面板容器的装饰
控制器
BottomAppBarController
设置
- snap - 如果为 true,则面板将在打开和关闭状态之间弹跳
- dragLength - 指针应移动的距离以完全打开/关闭面板
回调
- onDrag - 使用
GestureDetector
进行滑动控制时使用 - onDragEnd - 使用
GestureDetector
进行滑动控制时使用 - open - 切换到关闭状态
- close - 切换到打开状态
- swap - 如果面板打开则关闭它,反之亦然
完整示例代码
以下是一个完整的示例代码,展示了如何使用 expandable_bottom_bar_new
插件:
import 'package:expandable_bottom_bar_new/expandable_bottom_bar.dart';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: DefaultBottomBarController(
child: HomePage(),
),
);
}
}
class HomePage extends StatelessWidget {
final DefaultBottomBarController bbc = DefaultBottomBarController();
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[200],
// 设置为 true 以使底部应用程序栏覆盖主体内容
extendBody: true,
appBar: AppBar(
title: Text("可扩展底部导航栏示例"),
backgroundColor: Colors.blueGrey,
),
// 使用 docked FAB 来处理面板状态
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
floatingActionButton: GestureDetector(
// 设置 onVerticalDrag 事件以启用滑动效果
onVerticalDragUpdate: bbc.onDrag,
onVerticalDragEnd: bbc.onDragEnd,
child: FloatingActionButton.extended(
label: Text("拉动"),
icon: Icon(Icons.keyboard_arrow_up),
backgroundColor: Colors.deepOrange,
onPressed: () => bbc.swap(),
),
),
// 实际的可扩展底部应用程序栏
bottomNavigationBar: BottomExpandableAppBar(
controller: bbc,
expandedHeight: 300,
horizontalMargin: 16,
expandedBackColor: Colors.white,
expandedBody: Center(
child: Text(
"这是一个可扩展的底部导航栏!",
style: TextStyle(fontSize: 20),
),
),
bottomAppBarBody: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Icon(Icons.home, size: 40, color: Colors.blue),
Icon(Icons.search, size: 40, color: Colors.green),
Icon(Icons.settings, size: 40, color: Colors.red),
],
),
),
),
);
}
}
更多关于Flutter可扩展底部导航栏插件expandable_bottom_bar_new的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
expandable_bottom_bar_new
是一个 Flutter 插件,用于创建一个可扩展的底部导航栏。它允许用户在底部导航栏中展开或折叠更多的选项或内容。这个插件非常适合需要在底部导航栏中提供更多功能或选项的应用场景。
安装
首先,你需要在 pubspec.yaml
文件中添加依赖:
dependencies:
expandable_bottom_bar_new: ^1.0.0
然后运行 flutter pub get
来安装依赖。
基本使用
以下是一个简单的使用示例,展示如何在 Flutter 应用中使用 expandable_bottom_bar_new
插件。
import 'package:flutter/material.dart';
import 'package:expandable_bottom_bar_new/expandable_bottom_bar_new.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Expandable Bottom Bar Example'),
),
body: Center(
child: Text('Hello, World!'),
),
bottomNavigationBar: ExpandableBottomBar(
children: [
BottomBarItem(
icon: Icon(Icons.home),
label: 'Home',
onTap: () {
print('Home tapped');
},
),
BottomBarItem(
icon: Icon(Icons.search),
label: 'Search',
onTap: () {
print('Search tapped');
},
),
BottomBarItem(
icon: Icon(Icons.settings),
label: 'Settings',
onTap: () {
print('Settings tapped');
},
),
],
expandedChild: Container(
height: 200,
color: Colors.blue,
child: Center(
child: Text('Expanded Content'),
),
),
),
),
);
}
}
参数说明
-
children
: 这是一个List<BottomBarItem>
,用于定义底部导航栏的各个项目。每个BottomBarItem
包含一个图标、一个标签和一个点击事件处理函数。 -
expandedChild
: 这是一个Widget
,用于定义当底部导航栏展开时显示的内容。你可以在这里放置任何你想要的组件,比如列表、按钮等。
自定义
你可以通过设置 ExpandableBottomBar
的其他属性来自定义底部导航栏的外观和行为,例如:
backgroundColor
: 设置底部导航栏的背景颜色。iconColor
: 设置图标的颜色。textColor
: 设置文本的颜色。duration
: 设置展开和折叠的动画持续时间。curve
: 设置展开和折叠的动画曲线。
示例代码
ExpandableBottomBar(
backgroundColor: Colors.blueAccent,
iconColor: Colors.white,
textColor: Colors.white,
duration: Duration(milliseconds: 300),
curve: Curves.easeInOut,
children: [
BottomBarItem(
icon: Icon(Icons.home),
label: 'Home',
onTap: () {
print('Home tapped');
},
),
BottomBarItem(
icon: Icon(Icons.search),
label: 'Search',
onTap: () {
print('Search tapped');
},
),
BottomBarItem(
icon: Icon(Icons.settings),
label: 'Settings',
onTap: () {
print('Settings tapped');
},
),
],
expandedChild: Container(
height: 200,
color: Colors.blue,
child: Center(
child: Text('Expanded Content'),
),
),
)