Flutter弹窗提示插件flutter_webkul_alert_box的使用
Flutter弹窗提示插件flutter_webkul_alert_box的使用
Webkul Alert Box Flutter Package
WebkulAlertBox
是一个可定制的Flutter小部件,允许你以各种动画样式或无动画的方式显示对话框。该包提供了一种灵活且易于使用的方法来显示带有自定义标题、消息、图标和操作的警报。
更多详情,请访问:https://mobikul.com/
功能特性
- 显示带或不带动画的对话框。
- 可定制的对话框,包含标题、消息、图标和动作。
- 多种动画风格,包括从不同方向滑动。
- 可选的持续时间和可关闭行为的警报框。
安装
在你的 pubspec.yaml
文件中添加以下依赖项:
dependencies:
flutter:
sdk: flutter
flutter_webkul_alert_box: ^0.0.6 # 替换为最新版本
使用方法
导入包
import 'package:webkul_alert_box/flutter_webkul_alert_box.dart';
属性说明
Property | Type | Default | Description |
---|---|---|---|
context |
BuildContext |
Required | 当前的 BuildContext ,用于显示警报框。 |
animation |
AnimationType |
none |
用于显示警报框的动画风格。选项包括 none , dropFromTop , dropFromBottom , slideFromRight , slideFromLeft 等。 |
title |
String? |
"" |
警报框中显示的标题文本。 |
icon |
Widget? |
"" |
在警报框顶部显示的可选图标。 |
content |
String? |
"" |
警报框中显示的消息或内容。 |
actions |
List<Widget> |
const [] |
显示在警报框底部的动作小部件列表(例如按钮)。 |
animationDuration |
double |
300 |
动画持续时间,单位为毫秒。 |
barrierDismissible |
bool |
true |
是否可以通过点击警报框边界外的区域来关闭警报框。 |
示例代码
下面是一个完整的示例demo,展示了如何使用 flutter_webkul_alert_box
插件创建具有不同动画效果的警报框。
import 'package:flutter/material.dart';
import 'package:flutter_webkul_alert_box/flutter_webkul_alert_box.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Webkul Alert Box Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const AlertBoxDemo(),
);
}
}
class AlertBoxDemo extends StatelessWidget {
const AlertBoxDemo({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Webkul Alert Box Demo'),
),
body: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
ElevatedButton(
onPressed: () {
// Directly calling the function with all parameters
mobikulAlertBox(
context,
animation: AnimationType.dropFromTop,
title: 'Custom Alert',
content: 'This is a customizable alert with drop from top animation.',
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('OK'),
),
],
animationDuration: 300,
barrierDismissible: true,
);
},
child: const Text('Drop from Top'),
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: () {
mobikulAlertBox(
context,
animation: AnimationType.dropFromBottom,
title: 'Custom Alert',
content: 'This is a customizable alert with drop from bottom animation.',
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('OK'),
),
],
animationDuration: 300,
barrierDismissible: true,
);
},
child: const Text('Drop from Bottom'),
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: () {
mobikulAlertBox(
context,
animation: AnimationType.slideFromRight,
title: 'Custom Alert',
content: 'This is a customizable alert with slide from right animation.',
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('OK'),
),
],
animationDuration: 300,
barrierDismissible: true,
);
},
child: const Text('Slide from Right'),
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: () {
mobikulAlertBox(
context,
animation: AnimationType.slideFromLeft,
title: 'Custom Alert',
content: 'This is a customizable alert with slide from left animation.',
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('OK'),
),
],
animationDuration: 300,
barrierDismissible: true,
);
},
child: const Text('Slide from Left'),
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: () {
mobikulAlertBox(
context,
animation: AnimationType.slideFromBottomRight,
title: 'Custom Alert',
content: 'This is a customizable alert with slide from bottom right animation.',
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('OK'),
),
],
animationDuration: 300,
barrierDismissible: true,
);
},
child: const Text('Slide from Bottom Right'),
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: () {
mobikulAlertBox(
context,
animation: AnimationType.slideFromBottomLeft,
title: 'Custom Alert',
content: 'This is a customizable alert with slide from bottom left animation.',
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('OK'),
),
],
animationDuration: 300,
barrierDismissible: true,
);
},
child: const Text('Slide from Bottom Left'),
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: () {
mobikulAlertBox(
context,
animation: AnimationType.slideFromTopRight,
title: 'Custom Alert',
content: 'This is a customizable alert with slide from top right animation.',
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('OK'),
),
],
animationDuration: 300,
barrierDismissible: true,
);
},
child: const Text('Slide from Top Right'),
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: () {
mobikulAlertBox(
context,
animation: AnimationType.slideFromTopLeft,
title: 'Custom Alert',
content: 'This is a customizable alert with slide from top left animation.',
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('OK'),
),
],
animationDuration: 300,
barrierDismissible: true,
);
},
child: const Text('Slide from Top Left'),
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: () {
mobikulAlertBox(
context,
animation: AnimationType.none,
title: 'Custom Alert',
content: 'This is a customizable alert with no animation.',
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('OK'),
),
],
animationDuration: 300,
barrierDismissible: true,
);
},
child: const Text('No Animation'),
),
],
),
),
);
}
}
通过上述代码,你可以看到如何使用 flutter_webkul_alert_box
插件创建不同类型的警报框,并应用不同的动画效果。希望这个示例对你有所帮助!如果有任何问题,欢迎随时提问。
更多关于Flutter弹窗提示插件flutter_webkul_alert_box的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复