如何在 Flutter CheckboxListTile 中设置像 HTML 一样的值?

时间:2021-06-01 14:20:36

标签: flutter

在 HTML 中,复选框具有名称、值和标签:

<input type="checkbox" id="fruit" name="fruits" value="small_apple"><label for="fruit">Small Apple</label>
<input type="checkbox" id="fruit" name="fruits" value="big_apple"><label for="fruit">Big Apple</label>

如果两者都被选中,我将在提交后得到一个名为fruits的数组,其中包含“small_apple”和“big_apple”。

Flutter CheckboxListTile 有一个出现在屏幕上的标题:

CheckboxListTile(
    title: Text("Small Apple"),
    value: value,
    onChanged: (bool value) { ... },
)

我不需要在提交时发送“Small Apple”,而是“small_apple”。 Flutter 中的“值”仅保持复选框状态(真/假),那么如何在 Flutter 中像 HTML 一样设置“值”?或者我必须在提交前将“Small Apple”转换为“small_apple”?

1 个答案:

答案 0 :(得分:1)

你可以声明一个 String String stringValue; 并且在你的 onChanged 回调中你可以为每个复选框设置所需的值 例如,对于 small_apple 复选框,只需添加

onChanged:(ischecked){
stringValue = "small_apple" ;
value= ischecked;
}

然后对另一个复选框做同样的事情, 现在,您必须在 OnChanged And Call setState 中设置复选框值,以便复选框的 UI 状态发生变化