我正在开发一个应用,我想在其中添加一项功能,该功能在用户打开应用时每天显示一个警报对话框。我完成了对话框我使用了 boolean variable
如果这个布尔值是 true
然后显示 dialog 否则不显示 dialog .点击按钮打开对话框后,我设置了 bool value to false
。但我的问题是它如何在 true
处再次设置 end of the day?
我应该怎么做才能在一天结束时将 bool 值再次设置为 true?
答案 0 :(得分:1)
您需要显示对话框的最后日期/时间,而不是使用布尔值。
每次出现您可能需要显示对话框的情况时,您现在都会获取当前日期/时间,并将其与上次显示的对话框的日期/时间进行比较。
因为您无法确定该应用是会被终止还是现在被终止,所以您还需要存储该应用在磁盘上显示的最后日期/时间。
答案 1 :(得分:0)
您可以使用 Shared Preferences 存储用户的最后访问时间,并检查用户今天是否在使用该应用。
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setInt('lastAccess', DateTime.now().millisecondsSinceEpoch)
// Get last access
final int lastAccess = prefs.getInt('lastAccess');
if(lastAccess!=null){
// Get last access as DateTime
final DateTime lastAccessTime = DateTime.fromMillisecondsSinceEpoch(lastAccess);
// Check if he opened the app
final opened = lastAccessTime.isAfter(DateTime.now());
if(!opened){
// Show Dialog
}
}
答案 2 :(得分:-1)
timer = Timer.periodic(
Duration(days: 1,),
(Timer t) => {
//callback
});