Bird
Raised Fist0
Fluttermobile~10 mins

Radio buttons in Flutter - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a radio button with value 1.

Flutter
Radio<int>(value: [1], groupValue: selectedValue, onChanged: (int? val) { setState(() { selectedValue = val!; }); })
Drag options to blanks, or click blank then click option'
A1
Btrue
C0
D"one"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of an integer for the value.
Setting value to true or false instead of a number.
2fill in blank
medium

Complete the code to update the selected radio button value inside onChanged.

Flutter
onChanged: (int? val) { setState(() { selectedValue = [1]; }); }
Drag options to blanks, or click blank then click option'
Aval!
B1
CselectedValue
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning a fixed number instead of the passed value.
Forgetting to unwrap the nullable value.
3fill in blank
hard

Fix the error in the code by completing the missing property to show the selected radio button.

Flutter
Radio<int>(value: 2, groupValue: [1], onChanged: onChanged)
Drag options to blanks, or click blank then click option'
AonChanged
BselectedValue
Cnull
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Setting groupValue to a fixed number instead of the state variable.
Setting groupValue to null or the onChanged function.
4fill in blank
hard

Fill both blanks to create a group of two radio buttons with values 1 and 2.

Flutter
Column(children: [Radio<int>(value: [1], groupValue: selectedValue, onChanged: onChanged), Radio<int>(value: [2], groupValue: selectedValue, onChanged: onChanged)])
Drag options to blanks, or click blank then click option'
A1
B2
C3
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same value for both radio buttons.
Using values outside the expected range.
5fill in blank
hard

Fill all three blanks to create a radio button with label and update state on change.

Flutter
ListTile(title: Text([1]), leading: Radio<int>(value: [2], groupValue: selectedValue, onChanged: (int? val) { setState(() { selectedValue = [3]; }); }))
Drag options to blanks, or click blank then click option'
A"Option 1"
B1
Cval!
D"Choice"
Attempts:
3 left
💡 Hint
Common Mistakes
Using mismatched values between label and radio button.
Not updating the state with the passed value.

Practice

(1/5)
1.

What is the main purpose of a Radio button in Flutter?

easy
A. To display an image
B. To let the user enter text input
C. To navigate between screens
D. To allow the user to select only one option from a group

Solution

  1. Step 1: Understand Radio button usage

    Radio buttons are designed to let users pick one choice from a small set of options.
  2. Step 2: Compare with other UI elements

    Text input is for typing, images display pictures, navigation moves screens, so these are not correct.
  3. Final Answer:

    To allow the user to select only one option from a group -> Option D
  4. Quick Check:

    Radio button = single choice selector [OK]
Hint: Radio buttons always pick one from many [OK]
Common Mistakes:
  • Confusing radio buttons with checkboxes
  • Thinking radio buttons accept text input
  • Assuming radio buttons navigate screens
2.

Which of the following is the correct way to declare a Radio button with value 1 and groupValue 2?

Radio( ... )
easy
A. Radio(1, 2, onChanged: (val) {})
B. Radio(value: 1, groupValue: 2, onChanged: (val) {})
C. Radio(value: '1', groupValue: '2', onChanged: (val) {})
D. Radio(value = 1, groupValue = 2, onChanged = (val) {})

Solution

  1. Step 1: Check Dart named parameter syntax

    Flutter widgets use named parameters with colons, e.g., value: 1, groupValue: 2.
  2. Step 2: Validate parameter types

    Values should be integers here, so strings are incorrect. Also, positional parameters are not allowed.
  3. Final Answer:

    Radio(value: 1, groupValue: 2, onChanged: (val) {}) -> Option B
  4. Quick Check:

    Use named parameters with colons [OK]
Hint: Use colons for named parameters in Flutter widgets [OK]
Common Mistakes:
  • Using equals sign (=) instead of colon (:)
  • Passing values as strings instead of correct type
  • Using positional parameters instead of named
3.

What will be the selected radio button after this code runs?

int selected = 2;

Column(
  children: [
    Radio(value: 1, groupValue: selected, onChanged: (val) {}),
    Radio(value: 2, groupValue: selected, onChanged: (val) {}),
    Radio(value: 3, groupValue: selected, onChanged: (val) {}),
  ],
)

medium
A. The radio button with value 2
B. The radio button with value 1
C. The radio button with value 3
D. No radio button selected

Solution

  1. Step 1: Understand groupValue and value relation

    The radio button whose value matches groupValue is selected.
  2. Step 2: Check the values

    groupValue is 2, so the radio with value 2 is selected.
  3. Final Answer:

    The radio button with value 2 -> Option A
  4. Quick Check:

    Selected = groupValue match [OK]
Hint: Selected radio matches groupValue [OK]
Common Mistakes:
  • Confusing value and groupValue roles
  • Assuming first radio is selected by default
  • Ignoring groupValue in selection
4.

Identify the error in this Flutter code snippet for radio buttons:

int selected = 1;

Radio(
  value: 1,
  groupValue: selected,
  onChanged: null,
)

medium
A. value and groupValue must be strings
B. selected should be a boolean
C. onChanged should not be null to allow selection
D. Radio widget cannot be used inside Column

Solution

  1. Step 1: Check onChanged parameter

    onChanged must be a function to update selection; null disables interaction.
  2. Step 2: Validate other parameters

    value and groupValue can be int; selected as int is correct; Radio can be inside Column.
  3. Final Answer:

    onChanged should not be null to allow selection -> Option C
  4. Quick Check:

    onChanged null disables radio [OK]
Hint: onChanged must be non-null for radio to work [OK]
Common Mistakes:
  • Setting onChanged to null disables selection
  • Confusing data types for value/groupValue
  • Thinking Radio can't be inside layout widgets
5.

You want to create a group of radio buttons for colors: Red, Green, Blue. The selected color should update the UI when tapped. Which code snippet correctly implements this behavior?

class ColorSelector extends StatefulWidget {
  @override
  State<ColorSelector> createState() => _ColorSelectorState();
}

class _ColorSelectorState extends State<ColorSelector> {
  String selectedColor = 'Red';

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Radio<String>(
          value: 'Red',
          groupValue: selectedColor,
          onChanged: (val) {
            setState(() {
              selectedColor = val!;
            });
          },
        ),
        Radio<String>(
          value: 'Green',
          groupValue: selectedColor,
          onChanged: (val) {
            setState(() {
              selectedColor = val!;
            });
          },
        ),
        Radio<String>(
          value: 'Blue',
          groupValue: selectedColor,
          onChanged: (val) {
            setState(() {
              selectedColor = val!;
            });
          },
        ),
      ],
    );
  }
}
hard
A. The code correctly updates selectedColor and UI on tap
B. Missing setState call inside onChanged
C. groupValue should be a boolean, not String
D. Radio buttons cannot use String values

Solution

  1. Step 1: Check state update on tap

    onChanged calls setState to update selectedColor, which triggers UI refresh.
  2. Step 2: Validate types and usage

    groupValue and value are Strings, which is allowed and consistent.
  3. Step 3: Confirm UI rebuild

    setState ensures the widget rebuilds with new selectedColor, updating radio selection.
  4. Final Answer:

    The code correctly updates selectedColor and UI on tap -> Option A
  5. Quick Check:

    setState + matching groupValue = UI update [OK]
Hint: Use setState in onChanged to update selection [OK]
Common Mistakes:
  • Forgetting to call setState to refresh UI
  • Using wrong types for groupValue
  • Assuming Radio buttons can't use strings
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard