Flutter自适应进度对话框插件adaptive_progress_dialog的使用
Flutter自适应进度对话框插件adaptive_progress_dialog的使用
adaptive_progress_dialog
是一个Flutter插件,它提供了一种简单的方法来显示进度对话框,并根据平台的不同自适应样式。AdaptiveProgressDialog
小部件是通用的,它接受一个类型参数 T
,用于表示从对话框执行的异步操作返回的数据。数据通过 AdaptiveProgressDialogResult
类返回。
使用方法
1. 安装插件
首先,在 pubspec.yaml
文件中添加 adaptive_progress_dialog
依赖:
dependencies:
adaptive_progress_dialog: ^latest_version
然后运行 flutter pub get
来安装插件。
2. 导入插件
在Dart代码中导入插件:
import 'package:adaptive_progress_dialog/adaptive_progress_dialog.dart';
3. 创建和显示对话框
以下是一个完整的示例,展示了如何创建和显示一个自适应进度对话框:
import 'package:adaptive_progress_dialog/adaptive_progress_dialog.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Adaptive Progress Dialog Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String? dialogData;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter Demo Home Page'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
if (dialogData != null)
Text(
dialogData!,
style: const TextStyle(fontSize: 26),
),
const SizedBox(height: 64),
ElevatedButton(
onPressed: () async {
// 创建一个 AdaptiveProgressDialog 实例
final dialog = AdaptiveProgressDialog<String>(
title: 'Progress Dialog', // 对话框标题
content: 'Do you want to perform async operation?', // 对话框内容
confirmationButtonLabel: 'Yes', // 确认按钮标签
cancelButtonLabel: 'No, close', // 取消按钮标签
confirmButtonCallback: () async {
// 模拟一个耗时的异步操作
await Future.delayed(const Duration(seconds: 5));
return "Dialog Result Data"; // 返回操作结果
},
adaptiveProgressDialogStyle: AdaptiveProgressDialogStyle(
confirmButtonTextStyle: const TextStyle(color: Colors.red), // 确认按钮文本样式
),
);
// 显示对话框并等待结果
final adaptiveProgressDialogResult = await dialog.show(context);
// 根据对话框的结果进行处理
switch (adaptiveProgressDialogResult.status) {
case DialogStatus.success:
setState(() {
dialogData = adaptiveProgressDialogResult.data; // 更新状态
});
break;
case DialogStatus.canceled:
print("Dialog canceled by pressing on cancel button"); // 用户点击取消按钮
break;
case DialogStatus.error:
print("Dialog error: ${adaptiveProgressDialogResult.error}"); // 发生错误
break;
case DialogStatus.closed:
print("Dialog canceled by pressing outside dialog"); // 用户点击对话框外部
break;
}
},
child: const Text("Show adaptive dialog"), // 按钮文本
)
],
),
),
);
}
}
参数说明
AdaptiveProgressDialog
小部件有以下几个参数:
title
: 对话框的标题。content
: 对话框的内容。confirmationButtonLabel
: 确认按钮的标签,默认为 “Ok”。cancelButtonLabel
: 取消按钮的标签,默认为 “Cancel”。confirmButtonCallback
: 当确认按钮被按下时调用的回调函数。如果回调返回类型为T
的数据,则可以通过AdaptiveProgressDialogResult
的data
属性获取该数据。如果回调失败,则返回带有error
状态的AdaptiveProgressDialogResult
。adaptiveProgressDialogStyle
: 用于自定义对话框样式的AdaptiveProgressDialogStyle
对象。如果不提供,默认应用默认主题。isDismissible
: 是否可以通过点击对话框外部关闭对话框,默认为true
。
样式定制
你可以通过 adaptiveProgressDialogStyle
参数来自定义对话框的样式。例如:
AdaptiveProgressDialog(
title: 'Loading',
content: 'Please wait...',
adaptiveProgressDialogStyle: AdaptiveProgressDialogStyle(
titleTextStyle: TextStyle(
fontSize: 24.0,
fontWeight: FontWeight.bold,
color: Colors.blue,
),
contentTextStyle: TextStyle(
fontSize: 16.0,
color: Colors.grey,
),
confirmButtonTextStyle: TextStyle(
fontSize: 20.0,
color: Colors.white,
),
cancelButtonTextStyle: TextStyle(
fontSize: 20.0,
color: Colors.red,
),
materialContentPadding: EdgeInsets.all(16.0),
materialActionsPadding: EdgeInsets.only(right: 8.0, bottom: 8.0),
materialActionsAlignment: MainAxisAlignment.end,
),
confirmButtonCallback: () async {
// Do some work here...
},
);
更多关于Flutter自适应进度对话框插件adaptive_progress_dialog的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter自适应进度对话框插件adaptive_progress_dialog的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,下面是一个关于如何使用 adaptive_progress_dialog
插件的示例代码。adaptive_progress_dialog
是一个非常有用的 Flutter 插件,用于显示自适应的进度对话框。
首先,确保你已经在 pubspec.yaml
文件中添加了 adaptive_progress_dialog
依赖:
dependencies:
flutter:
sdk: flutter
adaptive_progress_dialog: ^x.y.z # 请将 x.y.z 替换为最新版本号
然后运行 flutter pub get
来获取依赖。
以下是一个完整的示例代码,展示了如何使用 adaptive_progress_dialog
插件:
import 'package:flutter/material.dart';
import 'package:adaptive_progress_dialog/adaptive_progress_dialog.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Adaptive Progress Dialog Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final _dialog = AdaptiveProgressDialog(
title: 'Loading...',
message: 'Please wait while we load the content.',
);
void _showDialog(BuildContext context) {
_dialog.show(context).whenComplete(() {
// Dialog dismissed
});
}
Future<void> _simulateLongRunningTask() async {
await Future.delayed(Duration(seconds: 3)); // Simulate a long-running task
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Adaptive Progress Dialog Demo'),
),
body: Center(
child: ElevatedButton(
onPressed: () async {
_showDialog(context);
await _simulateLongRunningTask();
_dialog.hide();
},
child: Text('Show Progress Dialog'),
),
),
);
}
}
代码说明:
- 依赖导入:在
pubspec.yaml
中添加adaptive_progress_dialog
依赖。 - 创建对话框实例:在
_MyHomePageState
类中,我们创建了一个AdaptiveProgressDialog
实例_dialog
,并设置了标题和消息。 - 显示对话框:在按钮点击事件中,我们调用
_dialog.show(context)
来显示对话框。这里还使用了whenComplete
方法来监听对话框的关闭事件(虽然在这个例子中并没有实际处理)。 - 模拟长时间运行的任务:
_simulateLongRunningTask
方法模拟了一个耗时操作(例如网络请求或数据计算),这里使用Future.delayed
模拟了3秒的延迟。 - 隐藏对话框:在任务完成后,调用
_dialog.hide()
来隐藏对话框。
这个示例展示了如何在 Flutter 应用中使用 adaptive_progress_dialog
插件来显示一个进度对话框,并在任务完成后隐藏它。希望这个示例对你有所帮助!