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

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

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

一、需求分析

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

二、实现思路

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

三、代码实现

1. 弹窗队列管理类

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:synchronized/synchronized.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 DialogQueueManager _instance = DialogQueueManager._internal();factory DialogQueueManager() => _instance;DialogQueueManager._internal();final _dialogQueue = <String, List<BSQueueDialog>>{};final _displayingDialog = <String, BSQueueDialog>{};final _lock = Lock();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);await _lock.synchronized(() async {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(tag, context);}});}Future<void> _showQueueDialog(String tag, BuildContext context) async {while (true) {await _lock.synchronized(() async {final queue = _dialogQueue[tag];if (queue == null || queue.isEmpty) {_dialogQueue.remove(tag);_displayingDialog.remove(tag);return;}final dialog = queue.removeAt(0);if (!mounted) return;final shouldShow = await dialog.shouldShow?.call(context) ?? false;if (!mounted) return;if (mounted && shouldShow) {_displayingDialog[tag] = dialog;} else {return; // 如果不应该显示,则直接返回}});if (!mounted) return;await dialog.show(context);await _lock.synchronized(() {_displayingDialog.remove(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

五、总结

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

相关文章:

  • keil修改字体无效,修改字体为“微软雅黑”方法
  • BitNet: 微软开源的 1-bit 大模型推理框架
  • (Go Gin)上手Go Gin 基于Go语言开发的Web框架,本文介绍了各种路由的配置信息;包含各场景下请求参数的基本传入接收
  • vscode 打开csv乱码
  • 服务网格助力云原生后端系统升级:原理、实践与案例剖析
  • 巧记英语四级单词 Unit5-中【晓艳老师版】
  • 【数据结构】红黑树原理及实现
  • 如何在 MinGW 和 Visual Studio (MSVC) 之间共享 DLL
  • 动态规划(1)(java)(面试题)三步问题
  • SAM 2 (Segment Anything ):图像与视频通用分割模型
  • Steam游戏服务器攻防全景解读——如何构建游戏级抗DDoS防御体系?
  • Android ioctl 第二个参数命令码以及BINDER_FREEZE示例
  • vue3项目中eslint.config.ts配置rules
  • 18.ArkUI Video的介绍和使用
  • ECharts 地图开发入门
  • HD Tune Pro v6.10 简体中文汉化单文件版
  • C++_数据结构_详解红黑树
  • Winform(1.Winform控件学习)
  • 每天学一个 Linux 命令(31):md5sum
  • Linux安全模块:SELinux与AppArmor深度解析
  • 龚曙光:散文家永远只有一个视角,就是“我与时代”
  • 政治局会议:积极维护多边主义,反对单边霸凌行径
  • 朱守科任西藏自治区政府副主席、公安厅厅长
  • 东阿至聊城公交票价取消八折优惠:运行成本高昂
  • 北大学者:过度依赖技术工具可能会削弱人类主动思考的能力
  • 2025欧亚经济合作发展论坛在沪举办