flutter - 在颤动中只显示一次 AlertDialog
问题描述
嗨伙计们。我有一个由 initState 自动启动的警报对话框,但是这个 AlertDialog 必须只为用户显示一次。这是我的代码:
@override
void initState() {
super.initState();
Future.delayed(const Duration(milliseconds: 1000), () {
showAlertDialogue();
});
}
showAlertDialogue() {
showDialog(
context: context,
child: InfoAlert(),//this is the AlertDialog from another file
);
}
有什么帮助可以使这个对话只是一次对话吗?
解决方案
将此添加到您的依赖项中:
shared_preferences: ^0.5.12+2
在您的页面中添加此导入:
import 'package:shared_preferences/shared_preferences.dart';
更新您的代码:
@override
void initState() {
super.initState();
SharedPreferences.getInstance().then((prefs) {
final int dialogOpen = prefs.getInt('dialog_open') ?? 0;
if (dialogOpen == 0) {//show dialog for one time only
Future.delayed(const Duration(milliseconds: 1000), () {
showAlertDialogue();
prefs.setInt("dialog_open", 1);
});
}
});
}
推荐阅读
- java - JavaFx tableview 和 tablecolumn 中的两个不同的类
- xamarin.forms - EF Database.EnsureDeleted() 和 Database.EnsureCreated() 方法上的 System.Reflection.TargetParameterCountException
- asp.net-core - 如何验证嵌入 OKTA 的 PowerBI
- bash - 我的 max 函数抛出错误,即使它与我的 min 函数相同但翻转了,找不到错误?
- r - R - 根据匹配的名称逐个元素地组合任意列表
- ios - 如何根据远程通知类型将用户重定向到各种屏幕
- c# - 无法在 Mac OSX 上启动 picoe/eto 应用程序
- laravel - 手动传递外键值
- javascript - 这两种在Javascript中创建构造函数的方式有什么区别?
- c# - 如何动态绑定集合中的枚举值?(WPF)