Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a DropdownButton with a list of string items.
Flutter
DropdownButton<String>(
value: selectedValue,
onChanged: (String? newValue) {
setState(() {
selectedValue = newValue!;
});
},
items: [1].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that is not defined.
Forgetting to map the list to DropdownMenuItem widgets.
✗ Incorrect
The DropdownButton's items property expects a list to map over. Here, 'itemsList' is the correct list variable name.
2fill in blank
mediumComplete the code to update the selected value when a new item is chosen.
Flutter
onChanged: (String? newValue) {
setState(() {
selectedValue = [1];
});
}, Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning the wrong variable or forgetting to update state.
Using the old selectedValue instead of the newValue.
✗ Incorrect
The onChanged callback provides the new selected value as 'newValue', which should be assigned to 'selectedValue'.
3fill in blank
hardFix the error in the DropdownButton code by completing the missing property.
Flutter
DropdownButton<String>( [1]: selectedValue, onChanged: (String? newValue) { setState(() { selectedValue = newValue!; }); }, items: itemsList.map<DropdownMenuItem<String>>((String value) { return DropdownMenuItem<String>( value: value, child: Text(value), ); }).toList(), )
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect property names like 'selected' or 'defaultValue'.
Omitting the value property causing the dropdown to not show selection.
✗ Incorrect
The DropdownButton uses the 'value' property to know which item is currently selected.
4fill in blank
hardFill both blanks to create a DropdownButton with a hint and disabled state.
Flutter
DropdownButton<String>( value: selectedValue, hint: [1], onChanged: [2] ? null : (String? newValue) { setState(() { selectedValue = newValue!; }); }, items: itemsList.map<DropdownMenuItem<String>>((String value) { return DropdownMenuItem<String>( value: value, child: Text(value), ); }).toList(), )
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of a Text widget for hint.
Not setting onChanged to null to disable the dropdown.
✗ Incorrect
The 'hint' property shows a placeholder text. The 'onChanged' is null when 'isDisabled' is true to disable the dropdown.
5fill in blank
hardFill all three blanks to create a DropdownButton with custom icon, underline, and expanded properties.
Flutter
DropdownButton<String>( value: selectedValue, icon: [1], underline: [2], isExpanded: [3], onChanged: (String? newValue) { setState(() { selectedValue = newValue!; }); }, items: itemsList.map<DropdownMenuItem<String>>((String value) { return DropdownMenuItem<String>( value: value, child: Text(value), ); }).toList(), )
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a boolean for icon instead of a widget.
Not providing a widget for underline.
Forgetting to set isExpanded to true for full width.
✗ Incorrect
The 'icon' property sets a custom icon widget. The 'underline' property sets a custom widget below the dropdown. 'isExpanded: true' makes the dropdown fill its container width.