Flutter弹窗提示插件alert_craft的使用
Flutter弹窗提示插件alert_craft的使用
alert_craft
是一个用于在你的 Flutter 应用程序中管理各种覆盖层和警告的插件。它提供了一个简单的 API 来显示警告对话框、加载对话框、选择对话框、吐司消息和自定义小部件。
特性
- 显示警告对话框:显示可自定义的警告对话框。
- 显示加载对话框:显示加载指示器。
- 显示选择对话框:显示包含可选选项的对话框。
- 显示吐司消息:显示非干扰性的吐司消息。
- 显示自定义对话框:在对话框中显示自定义小部件。
- 显示自定义警告:显示包含自定义小部件的自定义警告。
- 关闭警告:关闭当前显示的覆盖层或警告。
显示警告对话框
显示一个可自定义的警告对话框,包括标题、描述和可选按钮样式。
void showAlert() {
ShowAlert().showAlertDialog(
type: 1,
title: '警告标题',
description: '这是一个警告描述。',
buttonColor: Colors.blue,
buttonText: '确定',
buttonTextColor: Colors.white,
backgroundColor: Colors.black,
);
}
显示加载对话框
在当前屏幕上显示加载指示器。
void showLoading(BuildContext context) {
ShowAlert().showLoadingDialog(context);
}
显示选择对话框
显示一个带有选项和自定义按钮动作的对话框。
void showSelection() {
ShowAlert().showSelectionDialog(
type: 1,
title: '选择一个选项',
description: '请选择以下选项:',
buttonTextLeft: '取消',
buttonTextRight: '确认',
leftFunction: () {
// 处理左按钮动作
},
rightFunction: () {
// 处理右按钮动作
},
);
}
显示吐司消息
显示一个非干扰性的吐司消息,包括标题和描述。
void showToast() {
ShowAlert().showToastMessage(
type: 1,
title: '吐司标题',
description: '这是一条吐司消息。',
backgroundColor: Colors.green,
);
}
显示自定义对话框
在对话框中显示自定义小部件。
void showCustomDialog() {
ShowAlert().showCustomDialog(
MyCustomWidget(), // 替换为你的自定义小部件
);
}
显示自定义警告
显示包含自定义小部件的自定义警告。
void showCustomAlert() {
ShowAlert().showCustomAlert(
MyCustomAlertWidget(), // 替换为你的自定义警告小部件
);
}
关闭警告
关闭当前显示的覆盖层或警告。
void closeAlert() {
ShowAlert().closeAlert();
}
安装
将 alert_craft
添加到你的 pubspec.yaml
文件中:
dependencies:
alert_craft: ^1.0.0
示例代码
以下是一个完整的示例代码,展示了如何使用 alert_craft
插件来实现不同的弹窗功能。
import 'package:alert_craft/service/overlay_service.dart';
import 'package:flutter/material.dart';
import 'package:alert_craft/alert_craft.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
final ShowAlert _showAlert = ShowAlert();
HomeScreen({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Alert Craft 示例'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
_showAlert.showAlertDialog(
type: 1,
title: '警告对话框',
description: '这是一个警告对话框示例。',
buttonColor: Colors.blue,
buttonText: '确定',
buttonTextColor: Colors.white,
backgroundColor: Colors.grey[200],
);
},
child: const Text('显示警告对话框'),
),
ElevatedButton(
onPressed: () {
_showAlert.showLoadingDialog();
},
child: const Text('显示加载对话框'),
),
ElevatedButton(
onPressed: () {
_showAlert.showSelectionDialog(
type: 2,
title: '选择对话框',
description: '请选择一个选项:',
buttonTextLeft: '取消',
buttonTextRight: '确认',
buttonColor: Colors.green,
buttonTextColor: Colors.white,
backgroundColor: Colors.white,
leftFunction: () {},
rightFunction: () {} ,
);
},
child: const Text('显示选择对话框'),
),
ElevatedButton(
onPressed: () {
_showAlert.showToastMessage(
type: 3,
title: '吐司消息',
description: '这是一条吐司消息示例。',
backgroundColor: Colors.black,
);
},
child: const Text('显示吐司消息'),
),
ElevatedButton(
onPressed: () {
_showAlert.showCustomDialog(
const CustomDialogWidget(),
);
},
child: const Text('显示自定义对话框'),
),
],
),
),
);
}
}
class CustomDialogWidget extends StatelessWidget {
const CustomDialogWidget({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return AlertDialog(
title: const Text('自定义对话框'),
content: const Text('这是一个自定义对话框示例。'),
actions: [
TextButton(
onPressed: () => OverlayService().closeOverlay(),
child: const Text('关闭'),
),
],
);
}
}
更多关于Flutter弹窗提示插件alert_craft的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter弹窗提示插件alert_craft的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
alert_craft
是一个用于在 Flutter 应用中显示自定义弹窗的插件。它提供了简单易用的 API,允许你快速创建和显示各种类型的弹窗,包括警告、确认、输入框等。以下是如何在 Flutter 项目中使用 alert_craft
的基本步骤和示例。
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 alert_craft
依赖:
dependencies:
flutter:
sdk: flutter
alert_craft: ^1.0.0 # 请检查最新版本
然后运行 flutter pub get
来安装依赖。
2. 基本使用
显示简单提示框
你可以使用 AlertCraft.showAlert
方法来显示一个简单的提示框。
import 'package:flutter/material.dart';
import 'package:alert_craft/alert_craft.dart';
class MyHomePage extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('AlertCraft Example'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
AlertCraft.showAlert(
context,
title: '提示',
message: '这是一个简单的提示框!',
buttonText: '确定',
);
},
child: Text('显示提示框'),
),
),
);
}
}
显示确认框
如果你需要显示一个确认框,可以使用 AlertCraft.showConfirm
方法。
import 'package:flutter/material.dart';
import 'package:alert_craft/alert_craft.dart';
class MyHomePage extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('AlertCraft Example'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
AlertCraft.showConfirm(
context,
title: '确认',
message: '你确定要执行此操作吗?',
confirmText: '确定',
cancelText: '取消',
onConfirm: () {
print('用户点击了确定');
},
onCancel: () {
print('用户点击了取消');
},
);
},
child: Text('显示确认框'),
),
),
);
}
}
显示输入框
alert_craft
还支持显示带有输入框的弹窗。
import 'package:flutter/material.dart';
import 'package:alert_craft/alert_craft.dart';
class MyHomePage extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('AlertCraft Example'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
AlertCraft.showInput(
context,
title: '输入',
message: '请输入一些内容:',
confirmText: '提交',
onConfirm: (String value) {
print('用户输入的内容:$value');
},
);
},
child: Text('显示输入框'),
),
),
);
}
}
3. 自定义弹窗
alert_craft
允许你自定义弹窗的样式和行为。你可以通过传递自定义的 Widget
来替换默认的弹窗布局。
import 'package:flutter/material.dart';
import 'package:alert_craft/alert_craft.dart';
class MyHomePage extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('AlertCraft Example'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
AlertCraft.showCustom(
context,
child: Container(
padding: EdgeInsets.all(20),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(10),
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('自定义弹窗内容'),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('关闭'),
),
],
),
),
);
},
child: Text('显示自定义弹窗'),
),
),
);
}
}