Unverified Commit f69b8d33 authored by David Darrell's avatar David Darrell Committed by GitHub

DropdownItems with a null value should still display its child (#77666)

parent 8b144544
......@@ -1200,7 +1200,12 @@ class _DropdownButtonState<T> extends State<DropdownButton<T>> with WidgetsBindi
}
void _updateSelectedIndex() {
if (widget.value == null || widget.items == null || widget.items!.isEmpty) {
if (widget.items == null
|| widget.items!.isEmpty
|| (widget.value == null &&
widget.items!
.where((DropdownMenuItem<T> item) => item.value == widget.value)
.isEmpty)) {
_selectedIndex = null;
return;
}
......
......@@ -2492,7 +2492,50 @@ void main() {
expect(value, equals('two'));
});
testWidgets('DropdownButton is activated with the space key', (WidgetTester tester) async {
// Regression test for https://github.com/flutter/flutter/issues/77655.
testWidgets('DropdownButton selecting a null valued item should be selected',
(WidgetTester tester) async {
final List<MapEntry<String?, String>> items = <MapEntry<String?, String>>[
const MapEntry<String?, String>(null, 'None'),
const MapEntry<String?, String>('one', 'One'),
const MapEntry<String?, String>('two', 'Two'),
];
String? selectedItem = 'one';
await tester.pumpWidget(
StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return MaterialApp(
home: Scaffold(
body: DropdownButton<String>(
value: selectedItem,
onChanged: (String? string) {
setState(() {
selectedItem = string;
});
},
items: items.map((MapEntry<String?, String> item) {
return DropdownMenuItem<String>(
child: Text(item.value),
value: item.key,
);
}).toList(),
),
),
);
},
),
);
await tester.tap(find.text('One'));
await tester.pumpAndSettle();
await tester.tap(find.text('None').last);
await tester.pumpAndSettle();
expect(find.text('None'), findsOneWidget);
});
testWidgets('DropdownButton is activated with the space key',
(WidgetTester tester) async {
final FocusNode focusNode = FocusNode(debugLabel: 'DropdownButton');
String? value = 'one';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment