Flutter底部弹出菜单插件bottom_sheet_fx的使用
Flutter底部弹出菜单插件bottom_sheet_fx的使用
Custom Bottom Drawer
bottom_sheet_fx
是一个高度可定制的 Flutter 底部抽屉小部件,具有平滑的动画效果和手势控制。
特性
- 🎨 完全可定制的外观
- 🔄 带有增强缓动曲线的平滑动画
- ↕️ 支持拖动手势
- 🎮 通过控制器进行程序化控制
- 📱 响应式设计
- 🎯 主内容缩放和滑动效果
- 🌗 背景遮罩带有淡入淡出动画
- 🛠️ 可自定义的拖动手柄
安装
在项目终端运行以下命令:
flutter pub add bottom_sheet_fx
使用方法
首先,导入 bottom_sheet_fx
包:
import 'package:bottom_sheet_fx/bottom_sheet_fx.dart';
创建一个控制器:
final drawerController = BottomSheetController();
使用小部件:
BottomSheetFx(
controller: drawerController,
mainContent: YourMainContent(), // 主内容
sheetContent: YourSheetContent(), // 抽屉内容
maxHeight: 0.9, // 最大高度为屏幕高度的90%
backgroundColor: Colors.white, // 抽屉背景颜色
barrierColor: Colors.black54, // 背景遮罩颜色
animationDuration: Duration(milliseconds: 300), // 动画持续时间
)
程序化控制
// 打开抽屉
drawerController.open();
// 关闭抽屉
drawerController.close();
// 切换抽屉状态
drawerController.toggle();
// 检查抽屉是否打开
final isOpen = drawerController.isOpen;
自定义选项
该小部件提供了多种自定义选项:
属性名称 | 类型 | 默认值 | 描述 |
---|---|---|---|
mainContent |
Widget |
required | 应用的主要内容 |
sheetContent |
Widget |
required | 显示在抽屉中的内容 |
controller |
BottomSheetController? |
null | 控制器用于程序化控制 |
minHeight |
double |
0.0 | 关闭时的最小高度 |
maxHeight |
double |
0.90 | 最大高度占屏幕的百分比 |
animationDuration |
Duration |
300ms | 打开/关闭动画的持续时间 |
backgroundColor |
Color |
Colors.white | 抽屉的背景颜色 |
barrierColor |
Color |
Colors.black54 | 背景遮罩的颜色 |
手势控制
- 向上/向下拖动以打开/关闭抽屉
- 基于速度的动画
- 松手时会自动吸附到最近的位置
视觉效果
- 主内容的缩放和滑动动画
- 平滑的缓动曲线
- 动画圆角
- 带有淡入淡出的背景遮罩
- 增强的阴影效果
示例代码
import 'package:flutter/material.dart';
import 'package:bottom_sheet_fx/bottom_sheet_fx.dart';
class Example extends StatelessWidget {
final drawerController = BottomSheetController();
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
body: BottomSheetFx(
controller: drawerController,
mainContent: Container(
color: Colors.blue,
child: Center(
child: Text('Main Content'),
),
),
sheetContent: Container(
padding: EdgeInsets.all(16),
child: Column(
children: [
Text('Drawer Content'),
// 在这里添加抽屉内容
],
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: () => drawerController.toggle(),
child: Icon(Icons.add),
),
);
}
}
更多关于Flutter底部弹出菜单插件bottom_sheet_fx的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter底部弹出菜单插件bottom_sheet_fx的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
bottom_sheet_fx
是一个用于在 Flutter 中创建底部弹出菜单的插件。它提供了多种样式和自定义选项,使得创建底部弹出菜单变得非常简单和灵活。以下是如何使用 bottom_sheet_fx
插件的基本步骤:
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 bottom_sheet_fx
插件的依赖:
dependencies:
flutter:
sdk: flutter
bottom_sheet_fx: ^1.0.0 # 请检查最新版本
然后运行 flutter pub get
来安装依赖。
2. 导入包
在你的 Dart 文件中导入 bottom_sheet_fx
包:
import 'package:bottom_sheet_fx/bottom_sheet_fx.dart';
3. 使用 BottomSheetFx
你可以使用 BottomSheetFx
来显示底部弹出菜单。以下是一个简单的示例:
import 'package:flutter/material.dart';
import 'package:bottom_sheet_fx/bottom_sheet_fx.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('BottomSheetFx Example'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
BottomSheetFx.show(
context,
child: Container(
padding: EdgeInsets.all(20),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('This is a bottom sheet'),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
Navigator.pop(context); // 关闭底部弹出菜单
},
child: Text('Close'),
),
],
),
),
options: BottomSheetFxOptions(
backgroundColor: Colors.white,
elevation: 10,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(top: Radius.circular(20)),
),
),
);
},
child: Text('Show Bottom Sheet'),
),
),
);
}
}
4. 自定义选项
BottomSheetFxOptions
提供了多种自定义选项,例如:
backgroundColor
: 设置底部弹出菜单的背景颜色。elevation
: 设置底部弹出菜单的阴影高度。shape
: 设置底部弹出菜单的形状。isDismissible
: 设置是否可以通过点击外部关闭底部弹出菜单。enableDrag
: 设置是否可以通过拖动关闭底部弹出菜单。isScrollControlled
: 设置底部弹出菜单是否可以根据内容滚动。
5. 关闭底部弹出菜单
你可以通过 Navigator.pop(context)
来关闭底部弹出菜单。
6. 处理回调
你还可以在关闭底部弹出菜单时处理回调:
BottomSheetFx.show(
context,
child: Container(
padding: EdgeInsets.all(20),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('This is a bottom sheet'),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
Navigator.pop(context); // 关闭底部弹出菜单
},
child: Text('Close'),
),
],
),
),
options: BottomSheetFxOptions(
backgroundColor: Colors.white,
elevation: 10,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(top: Radius.circular(20)),
),
),
).then((value) {
print('Bottom sheet closed');
});