0
0
Fluttermobile~20 mins

Radio buttons in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Radio Button Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
Radio Button Selection Behavior

What will be the displayed text when the user selects the second radio button?

Flutter
import 'package:flutter/material.dart';

class RadioExample extends StatefulWidget {
  @override
  State<RadioExample> createState() => _RadioExampleState();
}

class _RadioExampleState extends State<RadioExample> {
  int _selectedValue = 1;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Radio<int>(
          value: 1,
          groupValue: _selectedValue,
          onChanged: (value) {
            setState(() {
              _selectedValue = value!;
            });
          },
        ),
        Radio<int>(
          value: 2,
          groupValue: _selectedValue,
          onChanged: (value) {
            setState(() {
              _selectedValue = value!;
            });
          },
        ),
        Text('Selected: $_selectedValue'),
      ],
    );
  }
}
ASelected: 1
BSelected: null
CSelected: 2
DNo text displayed
Attempts:
2 left
💡 Hint

Check how the _selectedValue changes when a radio button is tapped.

📝 Syntax
intermediate
2:00remaining
Correct Radio Button GroupValue Type

Which option correctly fixes the type error in this Flutter radio button snippet?

Flutter
Radio(
  value: 'A',
  groupValue: selectedValue,
  onChanged: (value) {
    setState(() {
      selectedValue = value;
    });
  },
)
AChange <code>value</code> to int and <code>selectedValue</code> to int.
BChange <code>selectedValue</code> type to String and cast <code>value</code> to String in <code>onChanged</code>.
CRemove <code>groupValue</code> property.
DSet <code>onChanged</code> to null.
Attempts:
2 left
💡 Hint

Radio button value and groupValue must be the same type.

lifecycle
advanced
2:00remaining
State Update Timing with Radio Buttons

What happens if you call setState outside the onChanged callback after selecting a radio button?

AThe UI does not update because <code>setState</code> was not called inside <code>onChanged</code>.
BThe UI updates immediately reflecting the new selection.
CThe app crashes with a runtime error.
DThe UI updates only after a hot reload.
Attempts:
2 left
💡 Hint

Consider when Flutter rebuilds widgets after state changes.

navigation
advanced
2:00remaining
Radio Button Selection and Navigation

In a Flutter app, you want to navigate to a new screen only when a specific radio button is selected. Which code snippet correctly performs this navigation?

Flutter
Radio<int>(
  value: 2,
  groupValue: selectedValue,
  onChanged: (value) {
    setState(() {
      selectedValue = value!;
    });
    // Navigation code here
  },
)
Aif (value == 2) Navigator.push(context, MaterialPageRoute(builder: (_) => NewScreen()));
BNavigator.push(context, MaterialPageRoute(builder: (_) => NewScreen()));
Cif (selectedValue == 2) Navigator.pop(context);
DNavigator.pop(context);
Attempts:
2 left
💡 Hint

Check the condition before navigation and use Navigator.push to go forward.

🔧 Debug
expert
2:00remaining
Debugging Radio Button State Not Updating

Why does this Flutter radio button not update the UI when tapped?

Flutter
class MyWidget extends StatefulWidget {
  @override
  State<MyWidget> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  int selected = 0;

  @override
  Widget build(BuildContext context) {
    return Radio<int>(
      value: 1,
      groupValue: selected,
      onChanged: (val) {
        setState(() {
          selected = val!;
        });
      },
    );
  }
}
ABecause <code>selected</code> is initialized to 0 instead of 1.
BBecause <code>groupValue</code> and <code>value</code> types do not match.
CBecause <code>onChanged</code> is null.
DBecause <code>setState</code> is not called after updating <code>selected</code>.
Attempts:
2 left
💡 Hint

State changes must be wrapped in setState to trigger UI rebuild.