简体   繁体   中英

How to set value like HTML in Flutter CheckboxListTile?

In HTML, checkbox has name, value and 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.

Flutter CheckboxListTile has a title which appear on screen:

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? Or do I have to convert "Small Apple" to "small_apple" before submit?

Well you can declare a 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:(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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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