babincc / flutter_workshop

This repo houses add-ons, plug-ins, and helpful code to make Flutter programming easier.
5 stars 1 forks source link

Radio Button Not Checked When Values Not Constant List. #19

Open muslimmuda15 opened 4 days ago

muslimmuda15 commented 4 days ago

This code is working well:

RadioGroup(
                controller: radioController,
                indexOfDefault: vehicleIndex,
                orientation: RadioGroupOrientation.vertical,
                values: const [
                  Padding(
                    padding: EdgeInsets.only(left: 4),
                    child: Text(
                      "Ship",
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.only(left: 4),
                    child: Text(
                      "Car",
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.only(left: 4),
                    child: Text(
                      "Bike",
                    ),
                  )
                ],
                onChanged: (value) {

                },

but when the values change using variable, all radio button suddendly not checked all after selecting:

List<String> vehicle = ["Car", "Ship", "Bike"]
RadioGroup(
                controller: radioController,
                indexOfDefault: vehicleIndex,
                orientation: RadioGroupOrientation.vertical,
                values: const [
                  Padding(
                    padding: EdgeInsets.only(left: 4),
                    child: Text(
                      vehicle[0],
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.only(left: 4),
                    child: Text(
                      vehicle[1],,
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.only(left: 4),
                    child: Text(
                      vehicle[12],,
                    ),
                  )
                ],
                onChanged: (value) {

                },

And I when I using map, it still doesn't work

RadioGroup(
                controller: radioController,
                indexOfDefault: vehicleIndex,
                orientation: RadioGroupOrientation.vertical,
                values: vehicle.map((v) {
                  return Padding(
                    padding: EdgeInsets.only(left: 4),
                    child: Text(
                      v,
                    ),
                  ),
                }).toList(),
                onChanged: (value) {

                },

How to solve this?

babincc commented 2 days ago

The code in examples 2 and 3 has some small syntax errors. These could be causing the issue.

Example 2:

Example 3:

After fixing these syntax errors, each version of the code works for me.

Helpful tip: After updating the code in these radio groups, do a hot restart to make sure all of the state data is up to date.

Recommended code: I recommend using the labelBuilder property if all of your widgets will be the same.

// Note: This RadioGroupController is specifically designated with the <String> type tag.
// Use the type that matches the data in your list
final RadioGroupController<String> radioController = RadioGroupController();
final List<String> vehicle = const ["Car", "Ship", "Bike"];

RadioGroup(
  controller: radioController,
  indexOfDefault: vehicleIndex,
  orientation: RadioGroupOrientation.vertical,
  values: vehicle,
  labelBuilder: (value) => Padding(
    padding: const EdgeInsets.only(left: 4),
    child: Text(value),
  ),
  onChanged: (value) {},
),