我正在尝试显示名为“年龄限制”的弹出窗口。我有一个变量ageRestriction它需要一个字符串。我只想在用户单击基于ageRestriction值的按钮时显示一次弹出窗口,例如:如果ageRestriction = "18" 显示一次,如果稍后它更改为 "20" 再次显示它,所以当 "18" 再次出现时想出现。我正在尝试将其保存到共享首选项列表,然后检查列表中是否存在该值。但它没有用
这是我迄今为止尝试过的代码
// Defining variables
String ageRestriction;
List<String> storedAges;
//Button with pop-up and conditions
InkWell(
onTap: () {
onTap: () {
if(widget.ageRestriction != null && widget.ageRestriction.isNotEmpty){
storedAges.contains(widget.ageRestriction) ? widget.onPressed():
AwesomeDialog(
popContext: false,
context: context,
dialogType: DialogType.WARNING,
animType: AnimType.TOPSLIDE,
btnOkText: S.of(context).yes,
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(S.of(context).are_you_18(widget.ageRestriction),style: TextStyle(
fontSize: 16,
fontFamily: "Montserrat",
fontWeight: FontWeight.bold),),
],
),
btnOkOnPress: () async{
setState(() {
SharedPreferencesHelper().addToAgeList(widget.ageRestriction);
storedAges = SharedPreferencesHelper().getAgeList();
print("Widget stored" + storedAges.length.toString());
});
///Working Code
// SharedPreferencesHelper().setAgeData = widget.ageRestriction;
widget.onPressed();
Navigator.of(context).pop();
},
btnCancelOnPress: () {
// Navigator.pop(context);
},
btnCancelText: S.current.no,
btnOkColor: Theme.of(context).accentColor,
btnCancelColor: Color(0xFF084457).withOpacity(0.9),
).show();
setState(() {
saveAgeData = widget.ageRestriction;
});
}else {
widget.onPressed();
}
},
}),
//Shared preference functions
Future<bool> setAgeList(String age) async {
return await _prefs.setStringList("AgeList", [age]);
}
Future<bool> addToAgeList(String age) async {
return await _prefs.setStringList("AgeList", _prefs.getStringList("AgeList")..add(age));
}
List<String> getAgeList() {
return _prefs.getStringList("AgeList");
}
你不应该使用 setAgeList 然后,如果列表为空,你必须得到空列表
替换 getAgeList;
var list = _prefs.getStringList("AgeList")??<String>[];
list.add(age);
return await _prefs.setStringList("AgeList", list);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句