From 228651ed158a20289130e7f93e33e8c051b6166b Mon Sep 17 00:00:00 2001 From: wuqifeng <540416539@qq.com> Date: Sun, 27 Oct 2024 22:33:30 +0800 Subject: [PATCH] feat:弹窗最多次数逻辑 --- lib/common/utils/popup_manager.dart | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/models/popup_entity.dart | 1 + lib/pages/home/view.dart | 61 ++----------------------------------------------------------- pubspec.yaml | 2 ++ 4 files changed, 101 insertions(+), 59 deletions(-) create mode 100644 lib/common/utils/popup_manager.dart diff --git a/lib/common/utils/popup_manager.dart b/lib/common/utils/popup_manager.dart new file mode 100644 index 0000000..7d51e21 --- /dev/null +++ b/lib/common/utils/popup_manager.dart @@ -0,0 +1,96 @@ +import 'package:flutter/material.dart'; +import 'package:intl/intl.dart'; + +import '../../models/popup_entity.dart'; +import '../../pages/home/PopupType.dart'; +import '../../route/route.dart'; +import '../../utils/log_util.dart'; +import '../../utils/sp_util.dart'; +import '../widgets/ow_image_widget.dart'; + +class PopupManager { + // 获取今日弹窗次数的键 + static String _getPopupCountKey(String popupId) { + String today = DateFormat('yyyy-MM-dd').format(DateTime.now()); + return 'popup_count_$popupId\_$today'; + } + + // 获取今日弹窗次数 + static int _getPopupCount(String popupId) { + String key = _getPopupCountKey(popupId); + return SpUtil.getInstance().get(key) ?? 0; + } + + // 更新今日弹窗次数 + static void _incrementPopupCount(String popupId) { + String key = _getPopupCountKey(popupId); + int currentCount = _getPopupCount(popupId); + SpUtil.getInstance().setData(key, currentCount + 1); + } + + // 检查是否可以弹窗 + static bool canShowPopup(PopupEntity popupEntity) { + int todayPopupCount = _getPopupCount(popupEntity.id); + return todayPopupCount < popupEntity.dayNum; + } + + // 显示弹窗逻辑 + static Future showPopupIfAllowed( + BuildContext context, PopupEntity popupEntity) async { + if (popupEntity.imageUrl.isEmpty) { + return; + } + if (canShowPopup(popupEntity)) { + /// 弹窗内容:图片和关闭按钮 + await showDialog( + context: context, + barrierDismissible: false, + builder: (BuildContext context) { + return Dialog( + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(8.0)), + child: Stack( + alignment: Alignment.topRight, + children: [ + // 图片内容区域 + ClipRRect( + borderRadius: BorderRadius.circular(16), // 可选:圆角 + child: GestureDetector( + onTap: () { + if (popupEntity.actionType == PopupType.h5.name && + popupEntity.actionValue.isNotEmpty) { + Navigator.of(context).pop(); + Navigator.of(context).pushNamed(AppRouteName.webView, + arguments: {'urlStr': popupEntity.actionValue}); + } + }, + child: OwImageWidget( + height: MediaQuery.of(context).size.height * 0.8, + name: popupEntity.imageUrl, + fit: BoxFit.fitHeight), + ), + ), + Positioned( + top: 0, + right: 0, + child: IconButton( + icon: const Icon(Icons.close, + color: Colors.white, + opticalSize: 2, + shadows: [Shadow(color: Colors.black, blurRadius: 24)]), + onPressed: () { + Navigator.of(context).pop(); + }, + ), + ), + ], + ), + ); + }, + ); + + // 更新今日弹窗次数 + _incrementPopupCount(popupEntity.id); + } + } +} diff --git a/lib/models/popup_entity.dart b/lib/models/popup_entity.dart index 6eb690b..1c66efa 100644 --- a/lib/models/popup_entity.dart +++ b/lib/models/popup_entity.dart @@ -6,6 +6,7 @@ import 'dart:convert'; class PopupEntity { late String actionType; late String actionValue; + ///一天最多弹窗次数 late int dayNum; late String id; late String imageUrl; diff --git a/lib/pages/home/view.dart b/lib/pages/home/view.dart index 8dd6ae3..57ab494 100644 --- a/lib/pages/home/view.dart +++ b/lib/pages/home/view.dart @@ -11,9 +11,8 @@ import 'package:url_launcher/url_launcher.dart'; import 'package:wow_english/common/core/app_config_helper.dart'; import 'package:wow_english/common/core/app_consts.dart'; import 'package:wow_english/common/extension/string_extension.dart'; +import 'package:wow_english/common/utils/popup_manager.dart'; import 'package:wow_english/models/app_version_entity.dart'; -import 'package:wow_english/models/popup_entity.dart'; -import 'package:wow_english/pages/home/PopupType.dart'; import 'package:wow_english/pages/home/state.dart'; import 'package:wow_english/pages/home/widgets/BaseHomeHeaderWidget.dart'; import 'package:wow_english/pages/home/widgets/ShakeImage.dart'; @@ -23,7 +22,6 @@ import 'package:wow_english/utils/audio_player_util.dart'; import '../../common/core/user_util.dart'; import '../../common/dialogs/show_dialog.dart'; import '../../common/utils/click_with_music_controller.dart'; -import '../../common/widgets/ow_image_widget.dart'; import 'bloc.dart'; import 'event.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; @@ -54,7 +52,7 @@ class _HomePageView extends StatelessWidget { _showUpdateDialog( context, state.forceUpdate, state.appVersionEntity); } else if (state is PopupDialogState) { - _showPopupDialog(context, state.popupEntity); + PopupManager.showPopupIfAllowed(context, state.popupEntity); } }, ), @@ -320,59 +318,4 @@ class _HomePageView extends StatelessWidget { throw 'Could not launch $url'; } } - - ///popup对话框 - void _showPopupDialog(BuildContext context, PopupEntity popupEntity) { - if (popupEntity.imageUrl.isEmpty) { - return; - } - showDialog( - context: context, - barrierDismissible: false, - builder: (BuildContext context) { - return Dialog( - backgroundColor: Colors.transparent, - child: Stack( - alignment: Alignment.topRight, - children: [ - // 图片内容区域 - ClipRRect( - borderRadius: BorderRadius.circular(16), // 可选:圆角 - child: GestureDetector( - onTap: () { - if (popupEntity.actionType == PopupType.h5.name && - popupEntity.actionValue.isNotEmpty) { - Navigator.of(context).pop(); - Navigator.of(context).pushNamed(AppRouteName.webView, - arguments: {'urlStr': popupEntity.actionValue}); - } - }, - child: OwImageWidget( - height: MediaQuery.of(context).size.height * 0.8, - name: popupEntity.imageUrl, - fit: BoxFit.fitHeight), - ), - ), - Positioned( - top: 0, - right: 0, - child: IconButton( - icon: const Icon( - Icons.close, - color: Colors.white, - opticalSize: 2, - shadows: [Shadow(color: Colors.black, blurRadius: 24)], - ), - onPressed: () { - Navigator.of(context).pop(); - }, - ), - ), - ], - // 图片宽度和高度是屏幕高度的80%,右上角有一个关闭按钮,点击关闭按钮关闭对话框,图片水平居中,高度在按钮垂直方向下方 - ), - ); - }, - ); - } } diff --git a/pubspec.yaml b/pubspec.yaml index cff0b2c..7b5747c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -120,6 +120,8 @@ dependencies: percent_indicator: ^4.2.3 # lottie动画 https://pub.dev/packages/lottie lottie: ^3.1.2 + # 提供国际化和本地化功能,包括消息翻译、复数和性别、日期/数字格式设置和解析以及双向文本。https://pub.dev/packages/intl + intl: ^0.19.0 dependency_overrides: # lottie 3.1.2 depends on http ^1.0.0 umeng_apm_sdk >=2.0.1 depends on http ^0.13.1 -- libgit2 0.22.2