Fix DropdownButton crash when hint and selectedItemBuilder are simultaneously defined (#42342)

* Fix DropdownButton crash when hint and selectedItemBuilder are simultaneously defined
This commit is contained in:
Shi-Hao Hong 2019-10-09 16:47:44 -07:00 committed by GitHub
parent 609de08881
commit 8fcc1d4151
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 2 deletions

View file

@ -760,7 +760,7 @@ class DropdownButton<T> extends StatefulWidget {
/// value: selectedItem,
/// onChanged: (String string) => setState(() => selectedItem = string),
/// selectedItemBuilder: (BuildContext context) {
/// return items.map((String item) {
/// return items.map<Widget>((String item) {
/// return Text(item);
/// }).toList();
/// },
@ -1047,7 +1047,7 @@ class _DropdownButtonState<T> extends State<DropdownButton<T>> with WidgetsBindi
if (_enabled) {
items = widget.selectedItemBuilder == null
? List<Widget>.from(widget.items)
: widget.selectedItemBuilder(context).map((Widget item) {
: widget.selectedItemBuilder(context).map<Widget>((Widget item) {
return Container(
constraints: const BoxConstraints(minHeight: _kMenuItemHeight),
alignment: AlignmentDirectional.centerStart,

View file

@ -1359,6 +1359,50 @@ void main() {
expect(find.text('Two as an Arabic numeral: 2'), findsOneWidget);
});
testWidgets('DropdownButton hint displays properly when selectedItemBuilder is defined', (WidgetTester tester) async {
// Regression test for https://github.com/flutter/flutter/issues/42340
final List<String> items = <String>['1', '2', '3'];
String selectedItem;
await tester.pumpWidget(
StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return MaterialApp(
home: Scaffold(
body: DropdownButton<String>(
hint: const Text('Please select an item'),
value: selectedItem,
onChanged: (String string) => setState(() {
selectedItem = string;
}),
selectedItemBuilder: (BuildContext context) {
return items.map((String item) {
return Text('You have selected: $item');
}).toList();
},
items: items.map((String item) {
return DropdownMenuItem<String>(
child: Text(item),
value: item,
);
}).toList(),
),
),
);
},
),
);
// Initially shows the hint text
expect(find.text('Please select an item'), findsOneWidget);
await tester.tap(find.text('Please select an item'));
await tester.pumpAndSettle();
await tester.tap(find.text('1'));
await tester.pumpAndSettle();
// Selecting an item should display its corresponding item builder
expect(find.text('You have selected: 1'), findsOneWidget);
});
testWidgets('Dropdown form field with autovalidation test', (WidgetTester tester) async {
String value = 'one';
int _validateCalled = 0;