Favorite Fruit: (o) Apple ( ) Banana ( ) Cherry [Submit]
Radio buttons in Flutter - Mini App: Build & Ship
import 'package:flutter/material.dart'; class RadioButtonScreen extends StatefulWidget { @override State<RadioButtonScreen> createState() => _RadioButtonScreenState(); } class _RadioButtonScreenState extends State<RadioButtonScreen> { String? _selectedFruit; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('Radio Button Selection')), body: Padding( padding: EdgeInsets.all(16), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text('Favorite Fruit:', style: TextStyle(fontSize: 20)), // TODO: Add radio buttons here SizedBox(height: 20), ElevatedButton( onPressed: () { // TODO: Show SnackBar with selected fruit }, child: Text('Submit'), ), ], ), ), ); } }
import 'package:flutter/material.dart'; class RadioButtonScreen extends StatefulWidget { @override State<RadioButtonScreen> createState() => _RadioButtonScreenState(); } class _RadioButtonScreenState extends State<RadioButtonScreen> { String? _selectedFruit; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('Radio Button Selection')), body: Padding( padding: EdgeInsets.all(16), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text('Favorite Fruit:', style: TextStyle(fontSize: 20)), RadioListTile<String>( title: Text('Apple'), value: 'Apple', groupValue: _selectedFruit, onChanged: (value) { setState(() { _selectedFruit = value; }); }, ), RadioListTile<String>( title: Text('Banana'), value: 'Banana', groupValue: _selectedFruit, onChanged: (value) { setState(() { _selectedFruit = value; }); }, ), RadioListTile<String>( title: Text('Cherry'), value: 'Cherry', groupValue: _selectedFruit, onChanged: (value) { setState(() { _selectedFruit = value; }); }, ), SizedBox(height: 20), ElevatedButton( onPressed: () { final snackBar = SnackBar( content: Text(_selectedFruit == null ? 'Please select a fruit.' : 'You selected: $_selectedFruit'), ); ScaffoldMessenger.of(context).showSnackBar(snackBar); }, child: Text('Submit'), ), ], ), ), ); } }
We use RadioListTile widgets for each fruit option. They share the same groupValue which is _selectedFruit. When a user taps a radio button, onChanged updates the state to the selected fruit. The Submit button shows a SnackBar with the selected fruit or asks the user to select one if none is chosen.
Favorite Fruit: (o) Apple ( ) Banana ( ) Cherry [Submit]
Practice
What is the main purpose of a Radio button in Flutter?
Solution
Step 1: Understand Radio button usage
Radio buttons are designed to let users pick one choice from a small set of options.Step 2: Compare with other UI elements
Text input is for typing, images display pictures, navigation moves screens, so these are not correct.Final Answer:
To allow the user to select only one option from a group -> Option DQuick Check:
Radio button = single choice selector [OK]
- Confusing radio buttons with checkboxes
- Thinking radio buttons accept text input
- Assuming radio buttons navigate screens
Which of the following is the correct way to declare a Radio button with value 1 and groupValue 2?
Radio( ... )Solution
Step 1: Check Dart named parameter syntax
Flutter widgets use named parameters with colons, e.g., value: 1, groupValue: 2.Step 2: Validate parameter types
Values should be integers here, so strings are incorrect. Also, positional parameters are not allowed.Final Answer:
Radio(value: 1, groupValue: 2, onChanged: (val) {}) -> Option BQuick Check:
Use named parameters with colons [OK]
- Using equals sign (=) instead of colon (:)
- Passing values as strings instead of correct type
- Using positional parameters instead of named
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) {}),
],
)Solution
Step 1: Understand groupValue and value relation
The radio button whose value matches groupValue is selected.Step 2: Check the values
groupValue is 2, so the radio with value 2 is selected.Final Answer:
The radio button with value 2 -> Option AQuick Check:
Selected = groupValue match [OK]
- Confusing value and groupValue roles
- Assuming first radio is selected by default
- Ignoring groupValue in selection
Identify the error in this Flutter code snippet for radio buttons:
int selected = 1;
Radio(
value: 1,
groupValue: selected,
onChanged: null,
)Solution
Step 1: Check onChanged parameter
onChanged must be a function to update selection; null disables interaction.Step 2: Validate other parameters
value and groupValue can be int; selected as int is correct; Radio can be inside Column.Final Answer:
onChanged should not be null to allow selection -> Option CQuick Check:
onChanged null disables radio [OK]
- Setting onChanged to null disables selection
- Confusing data types for value/groupValue
- Thinking Radio can't be inside layout widgets
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!;
});
},
),
],
);
}
}Solution
Step 1: Check state update on tap
onChanged calls setState to update selectedColor, which triggers UI refresh.Step 2: Validate types and usage
groupValue and value are Strings, which is allowed and consistent.Step 3: Confirm UI rebuild
setState ensures the widget rebuilds with new selectedColor, updating radio selection.Final Answer:
The code correctly updates selectedColor and UI on tap -> Option AQuick Check:
setState + matching groupValue = UI update [OK]
- Forgetting to call setState to refresh UI
- Using wrong types for groupValue
- Assuming Radio buttons can't use strings
Ready to prove it?
Pass the test and this pattern moves unknown
