popup_manager.dart
3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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<void> 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);
}
}
}