当前位置: 首页 > news >正文

Flutter 弹窗队列管理:支持优先级的线程安全通用弹窗队列系统

在复杂的 Flutter 应用开发中,弹窗管理是一个常见难题。手动管理弹窗的显示顺序和条件判断不仅繁琐,还容易出错。为此,我们实现了一个支持优先级的线程安全通用弹窗队列管理系统。它能够自动管理弹窗的显示顺序,支持条件判断,并且可以灵活地在任何地方调用。

一、需求分析

  1. 支持弹窗队列:按顺序显示多个弹窗。
  2. 条件判断:弹窗显示前可进行条件判断。
  3. 线程安全:确保在多线程环境下操作安全。
  4. 通用性:可在任何地方调用,不限于 StatefulWidget
  5. 优先级支持:支持弹窗优先级,高优先级弹窗优先显示。

二、实现思路

  1. 单例模式:全局只有一个队列管理实例。
  2. 线程安全:使用 synchronized 包确保操作安全。
  3. 优先级排序:弹窗按优先级排序,高优先级先显示。
  4. 独立函数:提供独立的 showQueueDialog 函数,方便调用。

三、代码实现

1. 弹窗队列管理类

import 'dart:async';import 'package:flutter/material.dart';const _defaultTag = 'default_dialog_queue_tag';typedef BSQueueDialogCondition = FutureOr<bool> Function(BuildContext context);
typedef BSQueueDialogShow = FutureOr<void> Function(BuildContext context);class BSQueueDialog {/// 是否应该显示弹窗final BSQueueDialogCondition? shouldShow;/// 显示弹窗final BSQueueDialogShow show;/// 弹窗优先级final int priority;const BSQueueDialog({this.shouldShow,required this.show,this.priority = 0, // 默认优先级为0});
}class DialogQueueManager {static final _instance = DialogQueueManager._internal();factory DialogQueueManager() => _instance;DialogQueueManager._internal();final _dialogQueue = <String, List<BSQueueDialog>>{};final _displayingDialog = <String, BSQueueDialog>{};Future<void> showQueueDialog<R>({required BuildContext context,BSQueueDialogCondition? shouldShow,required BSQueueDialogShow show,String tag = _defaultTag,int priority = 0, // 添加优先级参数}) async {final dialog =BSQueueDialog(shouldShow: shouldShow, show: show, priority: priority);var queue = _dialogQueue[tag];if (queue == null) {queue = <BSQueueDialog>[];_dialogQueue[tag] = queue;}queue.add(dialog);// 按优先级排序队列queue.sort((a, b) => b.priority.compareTo(a.priority)); // 高优先级在前// 检查是否有正在显示的弹窗final displayingDialog = _displayingDialog[tag];if (displayingDialog == null) {_displayingDialog[tag] = dialog;await _showQueueDialog(context, tag);} else if (dialog.priority > displayingDialog.priority) {// 如果新弹窗优先级高,则关闭当前弹窗并显示新弹窗Navigator.of(context).pop(); // 关闭当前弹窗_displayingDialog.remove(tag);_displayingDialog[tag] = dialog;await _showQueueDialog(context, tag);}}Future<void> _showQueueDialog<R>(BuildContext context, String tag) async {final queue = _dialogQueue[tag];if (queue == null || queue.isEmpty) {_dialogQueue.remove(tag);return;}final dialog = queue.removeAt(0);final shouldShow = await dialog.shouldShow?.call(context) ?? false;if (shouldShow) {_displayingDialog[tag] = dialog;await dialog.show(context);_displayingDialog.remove(tag);}return _showQueueDialog(context, tag);}
}

2. 独立的 showQueueDialog 函数

Future<void> showQueueDialog<R>({required BuildContext context,BSQueueDialogCondition? shouldShow,required BSQueueDialogShow show,String tag = _defaultTag,int priority = 0, // 弹窗优先级
}) async {return DialogQueueManager().showQueueDialog(context: context,shouldShow: shouldShow,show: show,tag: tag,priority: priority,);
}

3. 使用示例

import 'package:flutter/material.dart';void main() {runApp(MyApp());
}class MyApp extends StatelessWidget {Widget build(BuildContext context) {return MaterialApp(title: 'Queue Dialog Example',home: MyHomePage(),);}
}class MyHomePage extends StatelessWidget {Widget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text('Queue Dialog Example'),),body: Center(child: ElevatedButton(onPressed: () {showQueueDialog(context: context,shouldShow: (context) async {// 可以在这里添加条件逻辑return true;},show: (context) async {await showDialog(context: context,builder: (context) => AlertDialog(title: Text('Queue Dialog'),content: Text('This is a queued dialog with priority.'),actions: [TextButton(onPressed: () => Navigator.pop(context),child: Text('Close'),),],),);},priority: 1, // 设置弹窗优先级);},child: Text('Show Queue Dialog'),),),);}
}

四、代码说明

  1. 单例模式:通过 DialogQueueManager 类实现单例模式,确保全局只有一个队列管理实例。
  2. 线程安全:使用 synchronized 包中的 _lock 对象,确保对队列的操作是线程安全的。
  3. 优先级排序:弹窗按优先级排序,高优先级的弹窗会优先显示。
  4. 独立函数:提供独立的 showQueueDialog 函数,可在任何地方调用,不限于 StatefulWidget

五、总结

通过上述实现,我们构建了一个支持优先级的线程安全通用弹窗队列管理系统。它不仅支持弹窗的按序显示和条件判断,还支持弹窗优先级,高优先级的弹窗会优先显示。这种方式更加灵活,适用于更多场景,能够有效简化弹窗的管理逻辑,提高代码的可维护性。


http://www.mrgr.cn/news/100330.html

相关文章:

  • 前端面试之吊打面试官 HTML篇
  • k8s 1.26版部署
  • 网络攻防第一~四集
  • windows下查看idea运行的进程占的JVM情况工具
  • 从后端研发角度出发,使用k8s部署业务系统
  • 在Linux虚拟机下使用vscode,#include无法跳转问题
  • Vue3实现高仿word自定义颜色选择器组件(支持 v-model)
  • 3:QT联合HALCON编程—海康相机SDK二次程序开发
  • CSS简单实用的加载动画、骨架屏有效果图
  • TP4056 电池管理芯片介绍及电路应用
  • java每日精进 4.26【多租户之过滤器及请求处理流程】
  • 零基础上手Python数据分析 (24):Scikit-learn 机器学习初步 - 让数据预测未来!
  • Goland终端PowerShell命令失效
  • 【Linux网络】构建HTTP响应与请求处理系统 - HttpResponse从理解到实现
  • Kafka 面试,java实战贴
  • JAVA多线程(8.0)
  • 多系统安装经验,移动硬盘,ubuntu grub修改/etc/fstab 移动硬盘需要改成nfts格式才能放steam游戏
  • 【Linux网络】打造初级网络计算器 - 从协议设计到服务实现
  • Deep Reinforcement learning for real autonomous mobile robot navigation
  • Linux下编译并打包MNN项目迁移至其他设备