flutter - AlertDialog 中的 Flutter NumberPicker 无法正常工作
问题描述
当我用 NumberPicker 选择一个新值时,它总是跳回到以前的值而不是当前选择的值。
我在 AlertDialog 中使用 NumberPicker,并使用 pickValue() 函数调用 NumberPicker。
void pickValue() {
showDialog<int>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Wähle eine Aufgabenanzahl"),
content: NumberPicker(
selectedTextStyle: TextStyle(color: Colors.red),
value: currentValue,
minValue: 1,
maxValue: 10,
onChanged: (value) => setState(() => currentValue = value),
),
actions: [
TextButton(
child: Text("OK", style: TextStyles.body),
onPressed: () {
Navigator.of(context).pop();
},
)
],
);
});
}
解决方案
将警报对话框的内容包装在StatefulBuilder
. 它将提供一个新的上下文和一个 setState 函数来重建小部件。
void pickValue() {
showDialog<int>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Wähle eine Aufgabenanzahl"),
content:StatefulBuilder(
builder: (context, setState) {
return NumberPicker(
selectedTextStyle: TextStyle(color: Colors.red),
value: currentValue,
minValue: 1,
maxValue: 10,
onChanged: (value) => setState(() => currentValue = value),
);
}
),
actions: [
TextButton(
child: Text("OK", style: TextStyles.body),
onPressed: () {
Navigator.of(context).pop();
},
)
],
);
});
}
推荐阅读
- vba - 当设置用户权限为特定用户打开特定表单时,我从 DLookup 语句中收到运行时错误
- c# - IdentityUI 无法加载静态文件
- java - 无法在 Android Studio 中将 Java 代码转换为 Kotlin
- asp.net - Firefox 完全忽略 Access-Control-Allow-Origin 标头
- delphi - 复制时如何保留控件之间的连接?
- django - Django UpdateView 用户验证不起作用
- elasticsearch - 动态设置fluentD弹性搜索索引
- xml - 如何在使用 STRIP_OUTER_ELEMENT = TRUE 时获取大型 xml 雪花中的 xml 标头标记数据
- java - 父抽象类的构造
- kubernetes - kubectl 允许用户只复制文件进出 pod