Flutter GetX调用snackbar、Dialog、BottomSheet
Flutter GetX调用snackbar、Dialog、BottomSheet
我们可以通过GetX很轻松的调用上面组件,而且无需传入上下文context
Snackbar提示信息:
如果想在应用程序中触发某些特定的事件后,需要弹出一则快捷消息,那么使用Snackbar则是最佳的选择,Snackbar 和我们前面讲的toast有点相似,接下来我们看一下GetX如何来联调Snackbar来使用。
Snackbar基本使用:
第一步:应用程序入口设置
当我们导入依赖后,在应用程序顶层把GetMaterialApp 作为顶层,如下所示
import 'package:flutter/material.dart';
import 'package:get/get.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const GetMaterialApp(
debugShowCheckedModeBanner: false, //去掉debug图标
title: 'Flutter GetX',
home: MyHomePage(),
);
}
}
第二步:调用snackbar
我们可以通过Get.snackbar() 来显示 snackbar ,如下所示
Get.snackbar(
"提示!", "这是一条通知",
icon: Icon(Icons.alarm),
snackPosition: SnackPosition.BOTTOM
);
Snackbar属性和说明
字段 | 属性 | 描述 |
---|
title | String | 弹出的标题文字 |
message | String | 弹出的消息文字 |
colorText | Color | title和message的文字颜色 |
duration | Duration | Snackbar弹出的持续时间(默认3秒) |
instantInit | bool | 当false可以把snackbar 放在initState,默认true |
snackPosition | SnackPosition | 弹出时的位置,有两个选项【TOP,BOTTOM】默认TOP |
titleText | Widget | 弹出标题的组件,设置该属性会导致title属性失效 |
messageText | Widget | 弹出消息的组件,设置该属性会导致messageText属性失效 |
icon | Widget | 弹出时图标,显示在title和message的左侧 |
shouldIconPulse | bool | 弹出时图标是否闪烁,默认false |
maxWidth | double | Snackbar最大的宽度 |
margin | EdgeInsets | Snackbar外边距,默认zero |
padding | EdgeInsets | Snackbar内边距,默认EdgeInsets.all(16) |
borderRadius | double | 边框圆角大小,默认15 |
borderColor | Color | 边框的颜色,必须设置borderWidth,否则无效果 |
borderWidth | double | 边框的线条宽度 |
backgroundColor | Color | Snackbar背景颜色,默认Colors.grey.withOpacity(0.2) |
leftBarIndicatorColor | Color | 左侧指示器的颜色 |
boxShadows | List | Snackbar阴影颜色 |
backgroundGradient | Gradient | 背景的线性颜色 |
mainButton | TextButton | 主要按钮,一般显示发送、确认按钮 |
onTap | OnTap | 点击Snackbar事件回调 |
isDismissible | bool | 是否开启Snackbar手势关闭,可配合dismissDirection使用 |
showProgressIndicator | bool | 是否显示进度条指示器,默认false |
dismissDirection | SnackDismissDirection | Snackbar关闭的方向 |
progressIndicatorController | AnimationController | 进度条指示器的动画控制器 |
progressIndicatorBackgroundColor | Color | 进度条指示器的背景颜色 |
progressIndicatorValueColor | Animation | 进度条指示器的背景颜色,Animation |
snackStyle | SnackStyle | Snackbar是否会附加到屏幕边缘 |
forwardAnimationCurve | Curve | Snackbar弹出的动画,默认Curves.easeOutCirc |
reverseAnimationCurve | Curve | Snackbar消失的动画,默认Curves.easeOutCirc |
animationDuration | Duration | Snackbar弹出和小时的动画时长,默认1秒 |
barBlur | double | Snackbar背景的模糊度 |
overlayBlur | double | 弹出时的毛玻璃效果值,默认0 |
snackbarStatus | SnackbarStatusCallback | Snackbar弹出或消失时的事件回调(即将打开、已打开、即将关闭、已关闭) |
overlayColor | Color | 弹出时的毛玻璃的背景颜色 |
userInputForm | Form | 用户输入表单 |
Dialog对话框:
我们可以通过Get.defaultDialog() 调用Dialog来显示 dialog ,如下所示
// 默认对话框
Get.defaultDialog(
title: "提示",
content: const Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.error),
Text("您确定退出登录?", style: TextStyle(color: Colors.blue))
],
),
);
Get.defaultDialog(
title: "提示",
middleText: "您确定要删除吗?",
//确定按钮
confirm: ElevatedButton(
onPressed: () {
print("确定");
Get.back();
},
child: const Text("确定")),
//取消按钮
cancel: ElevatedButton(
onPressed: () {
print("取消");
Get.back();
},
child: const Text("取消"),
),
);
Dialog属性和说明
字段 | 属性 | 描述 |
---|
title | String | 弹出的标题,默认(Alert) |
titlePadding | EdgeInsetsGeometry | 标题的内边距,默认(EdgeInsets.all(8)) |
titleStyle | TextStyle | 标题的样式 |
middleText | String | 中间内容区域显示的文字 |
middleTextStyle | TextStyle | 中间内容区域显示的文字样式 |
content | Widget | 弹出的内容,该值设置后middleText将无效 |
contentPadding | EdgeInsetsGeometry | 内容的内边距,默认(EdgeInsets.all(8)) |
onConfirm | VoidCallback | 确认按钮回调 |
onCancel | VoidCallback | 取消按钮回调 |
onCustom | VoidCallback | 自定义按钮回调 |
cancelTextColor | Color | 取消按钮文字的颜色 |
confirmTextColor | Color | 确认按钮文字的颜色 |
textConfirm | String | 确认按钮的文字 |
textCancel | String | 取消按钮的文字 |
textCustom | String | 自定义按钮的文字 |
confirm | Widget | 确认按钮的组件 |
cancel | Widget | 取消按钮的组件 |
custom | Widget | 自定义按钮的组件 |
backgroundColor | Color | 弹出框的背景颜色 |
barrierDismissible | bool | 是否可以通过点击背景关闭弹窗 |
buttonColor | Color | 按钮的文字颜色,根据按钮类型来设定不同的位置 |
radius | double | 弹出框的圆角大小,默认20 |
actions | List | 增加额外的子组件 |
onWillPop | WillPopCallback | 拦截关闭之前做一些操作 |
navigatorKey | GlobalKey | 用于打开对话框的key |
BottomSheet底部弹窗:
BottomSheet 是底部弹出的一个组件。
我们可以通过Get.bottomSheet() 来显示 BottomSheet ,如下所示
//BottomSheet底部弹窗
Get.bottomSheet(
Container(
height: 260,
color: Colors.white,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
ListTile(
title: Text("分享到 QQ"),
onTap: () {
Get.back();
}),
ListTile(
title: Text("分享到 微信 "),
onTap: () {
Get.back();
}),
ListTile(
title: Text("分享到 微博"),
onTap: () {
Get.back();
}),
ListTile(
title: Text("分享到 知乎"),
onTap: () {
Get.back();
}),
],
),
),
);
BottomSheet属性和说明
字段 | 属性 | 描述 |
---|
bottomsheet | Widget | 弹出的Widget组件 |
backgroundColor | Color | bottomsheet的背景颜色 |
elevation | double | bottomsheet的阴影 |
persistent | bool | 是否添加到路由中 |
shape | ShapeBorder | 边框形状,一般用于圆角效果 |
clipBehavior | Clip | 裁剪的方式 |
barrierColor | Color | 弹出层的背景颜色 |
ignoreSafeArea | bool | 是否忽略安全适配 |
isScrollControlled | bool | 是否支持全屏弹出,默认false |
useRootNavigator | bool | 是否使用根导航 |
isDismissible | bool | 点击背景是否可关闭,默认ture |
enableDrag | bool | 是否可以拖动关闭,默认true |
settings | RouteSettings | 路由设置 |
enterBottomSheetDuration | Duration | bottomsheet进入时的动画时间 |
exitBottomSheetDuration | Duration | bottomsheet退出时的动画时间 |