首页 > 解决方案 > 允许用户每天只访问一次特定功能

问题描述

如果我想让页面在上午 12:00 或特定时间后自行重置怎么办?

假设我希望用户每天使用一次特定功能。例如,如果用户点击一个按钮将看到一张狗的图片,并且在上午 12:00 之后,用户可以再次点击该按钮以查看存储在应用程序。这是如何在 Flutter 中实现的?

标签: flutterdart

解决方案


肯定有不同的方法可以做到这一点。假设您想在客户端处理逻辑,您可以seenSharedPreferences.

然后在您的应用程序中的某个位置(例如启动),如果时间在午夜之后,您可以将值更新为 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
} 
  

推荐阅读