简体   繁体   English

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

[英]How to set value like HTML in Flutter CheckboxListTile?

In HTML, checkbox has name, value and label:在 HTML 中,复选框具有名称、值和 label:

<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>

If both are checked, I will get an array named fruits which contains "small_apple" and "big_apple" once submitted.如果两者都被选中,我将得到一个名为 fruits 的数组,其中包含“small_apple”和“big_apple”一旦提交。

Flutter CheckboxListTile has a title which appear on screen: Flutter CheckboxListTile 有一个出现在屏幕上的标题:

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

I do not need "Small Apple" to be sent on submit, but "small_apple".我不需要在提交时发送“小苹果”,而是“小苹果”。 "value" in Flutter keeps checkbox state(true/false) only, so how can I set "value" like HTML in Flutter? Flutter 中的“值”仅保持复选框状态(真/假),那么如何在 Flutter 中设置“值”,如 HTML? Or do I have to convert "Small Apple" to "small_apple" before submit?还是我必须在提交之前将“小苹果”转换为“小苹果”?

Well you can declare a String String stringValue;那么你可以声明一个 String String stringValue; and inside your onChanged callback you can set the needed value for each checkbox For example for the small_apple checkbox just add在 onChanged 回调中,您可以为每个复选框设置所需的值例如对于 small_apple 复选框,只需添加

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

And then do the same thing for the other checkbox, Now you must have to set the checkbox value inside the OnChanged And Call setState so the UI state of your checkbox changes然后对另一个复选框做同样的事情,现在您必须在 OnChanged和调用 setState中设置复选框值,以便您的复选框的 UI state 更改

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM