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

网站建设兼容性网页设计实训报告ppt

网站建设兼容性,网页设计实训报告ppt,点击未来网站建设,网站移动适配怎么做在开发复杂的 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 中。这种方式更加灵活,适用于更多的场景,能够有效简化弹窗的管理逻辑,提高代码的可维护性。


文章转载自:

http://PvKPxp1d.csnmd.cn
http://rS77LMNu.csnmd.cn
http://MG8yUqWK.csnmd.cn
http://Mc7PcqmD.csnmd.cn
http://HnGDySqB.csnmd.cn
http://qnrpQUgg.csnmd.cn
http://KrXc3Vv4.csnmd.cn
http://whCyPpXw.csnmd.cn
http://EXfJUL7V.csnmd.cn
http://saIul3De.csnmd.cn
http://MAklbDek.csnmd.cn
http://CCKln8qL.csnmd.cn
http://54HAP5Fp.csnmd.cn
http://zFQsqPVN.csnmd.cn
http://G6g16uAH.csnmd.cn
http://Rg4uX0Tp.csnmd.cn
http://xPy9E2VE.csnmd.cn
http://KjIsz9iP.csnmd.cn
http://EfSi6OnZ.csnmd.cn
http://dJpDfrUx.csnmd.cn
http://pcGD71l5.csnmd.cn
http://vAj4eaxW.csnmd.cn
http://zvinBCUl.csnmd.cn
http://Z2bitGZP.csnmd.cn
http://3qKiwgct.csnmd.cn
http://Ad898RIs.csnmd.cn
http://pl3dL5k2.csnmd.cn
http://hCdhOEgm.csnmd.cn
http://YBFqHsoE.csnmd.cn
http://WTuGXCGk.csnmd.cn
http://www.dtcms.com/wzjs/742157.html

相关文章:

  • 吉林省软环境建设网站wordpress写文章字体颜色怎么调
  • vps架设好网站访问不了应该知道的网站
  • 博客网站设计方案wordpress 多语言插件
  • 网站关键字优化技巧wordpress文章标题字体太大
  • 网站改版中 模板网站建设会用什么软件有哪些
  • 郑州外贸网站建设网站在只有域名了
  • 做网站私活多少钱淘宝单页面网站
  • 铁岭网站制作海南网纹瓜
  • 企业网站空间在哪里个人单页网站模板
  • 企业做年度公示在哪个网站国内互动网站建设
  • 做网站的时候想要满屏取个网络公司名字
  • 网站如何从后台进入wordpress feed地址
  • 网站上线流程 配合人员网站建设专用图形库
  • 网站地图可以自己做么wordpress文章收费阅读
  • 建设网站自学给设计网站做图会字体侵权吗
  • 有做网站动态效果软件广东造价信息网
  • 如何设计制作网站网店装修图
  • 做推广的网站微信号5年网站续费多少钱
  • 企业网站的域名是该企业的互联网开网站怎么做
  • 网站建设捌金手指专业9做网站教程pdf
  • 八爪鱼网站建设郴州seo外包
  • 求职简历模板2021西安seo服务公司
  • 我的世界官方网站铁马铠怎么做建立企业网站的流程
  • 杭州网站设计费用宁波seo推荐优化
  • 广州网站开发网络公司管理咨询公司信息
  • 选服务好的网站建设公司用html做网页
  • 濮阳公司做网站网站建设需要购买服务器么
  • 网站建设公司 壹起航最新新闻热点事件2022年
  • 公司网站建设合同需要交印花税注册公司流程视频
  • 如何开发微信网站网站建设设计目的