【问题标题】:How to set value like HTML in Flutter CheckboxListTile?如何在 Flutter CheckboxListTile 中设置 HTML 等值?
【发布时间】:2021-08-19 18:06:06
【问题描述】:

在 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) { ... },
)

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

【问题讨论】:

    标签: flutter


    【解决方案1】:

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

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

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

    【讨论】:

    • 我明白了。谢谢!:)
    猜你喜欢
    • 1970-01-01
    • 2020-02-12
    • 1970-01-01
    • 1970-01-01
    • 2020-10-19
    • 1970-01-01
    • 2020-10-17
    • 2021-07-19
    • 2020-11-10
    相关资源
    最近更新 更多