Flutter对话框管理插件flutter_app_dialog的使用
Flutter对话框管理插件flutter_app_dialog的使用
flutter_app_dialog
是一个用于在iOS和Android上简单且易于实现对话框的Flutter插件。
安装
在你的 pubspec.yaml
文件中添加 flutter_app_dialog
依赖:
dependencies:
flutter_app_dialog: ^版本号
然后运行 flutter pub get
来获取该依赖。
示例
以下是一些基本的示例代码,展示了如何使用 flutter_app_dialog
插件来创建不同类型的对话框。
基本对话框
showDialog(
context: context,
builder: (BuildContext context) {
return BaseDialogWidget(
child: BasicDialog(
title: '标题',
content: '您的消息',
titleStyle: TextStyle(color: Colors.blue),
iconColor: Colors.red,
contentStyle: TextStyle(color: Colors.blue),
),
);
});
信息对话框
showDialog(
context: context,
builder: (BuildContext context) {
return BaseDialogWidget(
child: InfoDialog(
title: '信息对话框',
content: '您的消息',
titleStyle: TextStyle(color: Colors.blue),
contentStyle: TextStyle(color: Colors.blue),
),
);
});
无网络连接对话框
showDialog(
context: context,
builder: (BuildContext context) {
return BaseDialogWidget(
child: NoInternetConnectionDialog(
title: '无网络连接',
content: '请检查您的连接状态并重试',
),
);
});
支付对话框
showDialog(
context: context,
builder: (BuildContext context) {
return BaseDialogWidget(
child: PaymentDialog(
icon: Icons.check_circle_outline,
iconColor: Color.fromRGBO(126, 211, 33, 1),
iconSize: 50,
paymentStatus: '成功',
message: '感谢您的支付',
buttonName: '确定',
onPressButton: () {
print('按下按钮!');
},
),
);
});
评分对话框
showDialog(
context: context,
builder: (BuildContext context) {
return BaseDialogWidget(
child: RatingDialog(
title: '享受我们的应用',
titleStyle: TextStyle(color: Color.fromRGBO(42, 42, 48, 1), fontWeight: FontWeight.bold),
description: '点击星星在App Store上评价。',
positiveRatingColor: Colors.blue,
negativeRatingColor: Colors.blue,
positiveButtonName: '提交',
negativeButtonName: '取消',
onRatingSubmit: (value) {
debugPrint('评分值: $value');
},
),
);
});
完整示例代码
import 'package:flutter/material.dart';
import 'package:flutter_app_dialog/flutter_app_dialog.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return ScreenUtilInit(
builder: (BuildContext context, child) => MaterialApp(
theme: ThemeData(
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter App Dialog'),
),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
[@override](/user/override)
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
backgroundColor: Color.fromRGBO(118, 80, 193, 1),
),
body: _body(),
);
}
/// 构建主体内容
Widget _body() {
return Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
alignment: Alignment.center,
child: SingleChildScrollView(
child: Column(
children: [
_baseButton(
buttonName: '基本对话框',
dialogWidget: BasicDialog(
title: '标题',
content: '您的消息',
)),
SizedBox(height: 20),
/// 构建信息对话框
_baseButton(buttonName: '信息对话框', dialogWidget: InfoDialog()),
SizedBox(height: 20),
/// 构建无网络连接对话框
_baseButton(
buttonName: '无网络连接',
dialogWidget: NoInternetConnectionDialog()),
SizedBox(height: 20),
/// 构建支付对话框
_baseButton(
buttonName: '支付对话框', dialogWidget: PaymentDialog()),
SizedBox(height: 20),
/// 构建评分对话框
_baseButton(
buttonName: '评分对话框',
dialogWidget: RatingDialog(
onRatingSubmit: (value) {
print('评分值: $value');
},
)),
],
),
),
);
}
/// 构建基础按钮
Widget _baseButton({String buttonName, Widget dialogWidget}) {
return Container(
width: ScreenUtil().setWidth(200),
height: ScreenUtil().setHeight(40),
child: TextButton(
child: Text(
buttonName ?? '',
textAlign: TextAlign.center,
overflow: TextOverflow.clip,
style: TextStyle(
fontSize: 16,
color: Colors.white,
),
),
style: TextButton.styleFrom(
backgroundColor: Color.fromRGBO(118, 80, 193, 1),
),
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return BaseDialogWidget(
child: dialogWidget ?? InfoDialog(),
);
});
},
));
}
}
更多关于Flutter对话框管理插件flutter_app_dialog的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter对话框管理插件flutter_app_dialog的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
flutter_app_dialog
是一个用于简化 Flutter 应用中对话框管理的插件。它提供了一种更简单的方式来显示和管理对话框,而不需要手动处理 showDialog
和 Navigator
的复杂性。以下是如何使用 flutter_app_dialog
插件的基本步骤:
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 flutter_app_dialog
插件的依赖:
dependencies:
flutter:
sdk: flutter
flutter_app_dialog: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来安装依赖。
2. 导入包
在你的 Dart 文件中导入 flutter_app_dialog
包:
import 'package:flutter_app_dialog/flutter_app_dialog.dart';
3. 使用 AppDialog
显示对话框
flutter_app_dialog
提供了 AppDialog
类来显示不同类型的对话框。以下是一些常见的用法示例:
显示一个简单的对话框
AppDialog.show(
context,
title: '提示',
message: '这是一个简单的对话框',
positiveButtonText: '确定',
positiveButtonAction: () {
print('确定按钮被点击');
},
);
显示带有取消按钮的对话框
AppDialog.show(
context,
title: '提示',
message: '这是一个带有取消按钮的对话框',
positiveButtonText: '确定',
positiveButtonAction: () {
print('确定按钮被点击');
},
negativeButtonText: '取消',
negativeButtonAction: () {
print('取消按钮被点击');
},
);
显示带有图标的对话框
AppDialog.showWithIcon(
context,
icon: Icons.warning,
title: '警告',
message: '这是一个带有图标的对话框',
positiveButtonText: '确定',
positiveButtonAction: () {
print('确定按钮被点击');
},
);
显示自定义样式的对话框
AppDialog.showCustom(
context,
title: '自定义对话框',
message: '这是一个自定义样式的对话框',
positiveButtonText: '确定',
positiveButtonAction: () {
print('确定按钮被点击');
},
negativeButtonText: '取消',
negativeButtonAction: () {
print('取消按钮被点击');
},
dialogStyle: DialogStyle(
backgroundColor: Colors.blue,
titleTextStyle: TextStyle(color: Colors.white, fontSize: 20),
messageTextStyle: TextStyle(color: Colors.white, fontSize: 16),
positiveButtonTextStyle: TextStyle(color: Colors.white),
negativeButtonTextStyle: TextStyle(color: Colors.white),
),
);
4. 自定义对话框
flutter_app_dialog
允许你通过 DialogStyle
类来自定义对话框的外观。你可以设置背景颜色、文本样式、按钮样式等。
5. 处理对话框结果
你可以通过 positiveButtonAction
和 negativeButtonAction
来处理用户点击按钮后的操作。
6. 关闭对话框
通常情况下,对话框会在用户点击按钮后自动关闭。如果你需要手动关闭对话框,可以使用 Navigator.pop(context)
。
7. 其他功能
flutter_app_dialog
还支持其他功能,如显示进度对话框、输入对话框等。你可以查阅插件的文档或源代码以获取更多信息。
示例代码
import 'package:flutter/material.dart';
import 'package:flutter_app_dialog/flutter_app_dialog.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter App Dialog 示例'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
AppDialog.show(
context,
title: '提示',
message: '这是一个简单的对话框',
positiveButtonText: '确定',
positiveButtonAction: () {
print('确定按钮被点击');
},
);
},
child: Text('显示对话框'),
),
),
);
}
}