mirror of
https://github.com/flutter/flutter
synced 2024-10-13 11:42:54 +00:00
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:
parent
609de08881
commit
8fcc1d4151
|
@ -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,
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue