flutter - 允许用户每天只访问一次特定功能
问题描述
如果我想让页面在上午 12:00 或特定时间后自行重置怎么办?
假设我希望用户每天使用一次特定功能。例如,如果用户点击一个按钮将看到一张狗的图片,并且在上午 12:00 之后,用户可以再次点击该按钮以查看存储在应用程序。这是如何在 Flutter 中实现的?
解决方案
肯定有不同的方法可以做到这一点。假设您想在客户端处理逻辑,您可以seen
在SharedPreferences
.
然后在您的应用程序中的某个位置(例如启动),如果时间在午夜之后,您可以将值更新为 false。假设您已经SharedPreferences
设置并且它位于一个名为的变量中sharedPreferences
DateTime time = DateTime.now();
String resetTime = "${time.year}-${time.month.toString().padLeft(2,'0')}-${time.day.toString().padLeft(2, '0')} 00:00:00";
if(time.isAfter(DateTime.parse(resetTiem){
sharedPreferences.setBool('seen', false)
}
现在在代码的另一边,用户点击查看图片,您可以检查 sharedPreferences 值是 true 还是 false 以显示图片。
if(sharedPreferences.getBool('seen')){
return SomeWidgetWithThePicture;
} else {
return AnotherWidgetWithoutPicture // tell the user they can see it after midnight
}
推荐阅读
- r - 在r中的json字符串中获取所有可能的键/键路径
- piranha-cms - 从 Piranha CMS 中的图像文件名中删除 guid
- python - 为什么 set().union(*list1) 给了我一个列表中两个列表的并集?
- docusignapi - 无法将 docusign 与第三方应用程序集成
- git - 从远程仓库拉取时,GitLab 镜像功能不起作用
- html - 为什么我的溢出和文本溢出:省略号代码在 MS Edge 中不起作用?
- unity3d - 单击播放按钮时如何使对象保持在其位置
- kubernetes - Docker for mac 80 端口对 k8s + ingress 没有用,我该如何修复它们?
- python - keras 中的 Conv2d 函数以及对下层的理解
- machine-learning - 使用数据集训练模型