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

Flutter 弹窗队列管理:实现一个线程安全的通用弹窗队列系统

在开发复杂的 Flutter 应用时,弹窗的管理往往是一个令人头疼的问题。尤其是在多个弹窗需要按顺序显示,或者弹窗的显示需要满足特定条件时,手动管理弹窗的显示和隐藏不仅繁琐,还容易出错。为了解决这个问题,我们可以实现一个通用的弹窗队列管理系统,它能够自动管理弹窗的显示顺序,并且支持条件判断,决定是否显示某个弹窗。

一、需求分析

在实现弹窗队列管理系统之前,我们先明确一下需求:

  1. 支持弹窗队列:能够将多个弹窗按顺序排队,依次显示。
  2. 条件判断:每个弹窗可以指定一个条件函数,只有当条件满足时,弹窗才会显示。
  3. 线程安全:在多线程环境下,确保弹窗队列的操作是安全的。
  4. 通用性:不限制在 StatefulWidget 中使用,可以在任何地方调用。

二、实现思路

为了实现上述需求,我们采用以下设计思路:

  1. 单例模式:使用单例模式管理弹窗队列,确保全局只有一个队列管理实例。
  2. 线程安全:使用 synchronized 包来确保对队列的操作是线程安全的。
  3. 独立函数:提供一个独立的 showQueueDialog 函数,可以在任何地方调用,而不仅仅是 StatefulWidgetState 中。

三、代码实现

以下是完整的代码实现:

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;const BSQueueDialog({this.shouldShow, required this.show});
}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,}) async {final dialog = BSQueueDialog(shouldShow: shouldShow, show: show);await _lock.synchronized(() async {var queue = _dialogQueue[tag];if (queue == null) {queue = <BSQueueDialog>[];_dialogQueue[tag] = queue;}queue.add(dialog);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 函数

// 独立的 showQueueDialog 函数
Future<void> showQueueDialog<R>({required BuildContext context,BSQueueDialogCondition? shouldShow,required BSQueueDialogShow show,String tag = _defaultTag,
}) async {return DialogQueueManager().showQueueDialog(context: context,shouldShow: shouldShow,show: show,tag: tag,);
}

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.'),actions: [TextButton(onPressed: () => Navigator.pop(context),child: Text('Close'),),],),);},);},child: Text('Show Queue Dialog'),),),);}
}

四、代码说明

1. 单例模式

使用 DialogQueueManager 类封装队列管理逻辑,确保全局只有一个实例。通过 factory 构造函数和 _internal 私有构造函数实现单例模式。

2. 线程安全

使用 synchronized 包中的 _lock 对象,确保对 _dialogQueue_displayingDialog 的操作是线程安全的。

3. mounted 检查

在每次使用 context 前,都进行 mounted 检查,确保在 widget 被销毁后不会继续操作 context

4. 循环代替递归

使用 while (true) 循环代替递归调用,避免栈溢出问题。

5. 独立的 showQueueDialog 函数

提供了一个独立的 showQueueDialog 函数,可以在任何地方调用,而不仅仅是 StatefulWidgetState 中。

五、总结

通过上述实现,我们构建了一个通用的、线程安全的弹窗队列管理系统。这个系统不仅支持弹窗的按序显示,还支持条件判断,决定是否显示某个弹窗。通过提供独立的 showQueueDialog 函数,我们确保了这个系统可以在任何地方使用,而不仅仅是 StatefulWidgetState 中。这种方式更加灵活,适用于更多的场景,能够有效简化弹窗的管理逻辑,提高代码的可维护性。

相关文章:

  • 基于尚硅谷FreeRTOS视频笔记——15—系统配制文件说明与数据规范
  • 考公:数字推理
  • 【NLP 66、实践 ⑰ 基于Agent + Prompt Engineering文章阅读】
  • 你的电脑在开“外卖平台”?——作业管理全解析
  • QML Rectangle 组件
  • 卷积神经网络基础(二)
  • 嵌入式单片机通过ESP8266连接物联网实验
  • 06-libVLC的视频播放器:推流RTMP
  • HCIP --- OSPF综合实验
  • office软件中word里面的编号库和列表库功能
  • 在 Node.js 中使用原生 `http` 模块,获取请求的各个部分:**请求行、请求头、请求体、请求路径、查询字符串** 等内容
  • C# 预定义类型全解析
  • 实验扩充 LED显示4*4键位值
  • 单片机毕业设计选题物联网计算机电气电子类
  • Java使用javacv实现的多种音视频格式播放器
  • Qt编写推流程序/支持webrtc265/从此不用再转码/打开新世界的大门
  • ReSearch:基于强化学习的大语言模型推理搜索框架
  • JavaSpring 中使用 Redis
  • PostgreSQL 常用客户端工具
  • DataWhale AI春训营 问题汇总
  • 规模再创新高,超百款新车首发!上海车展明日开幕
  • 美股再遭重挫,标普500指数11个板块全线溃败
  • 人民热评:大尺度色情语聊、撮合卖淫,社交APP岂止跑偏
  • 2025年上海车展后天开幕,所有进境展品已完成通关手续
  • 商务部:支持“来数加工”等新业态新模式,发展游戏出海业务
  • 豫章故郡,剑指演艺经济新高地