From 1663fde3470d1598019f50badd6b4f6f2c2a825a Mon Sep 17 00:00:00 2001 From: Shi-Hao Hong Date: Tue, 5 Nov 2019 12:32:03 -0800 Subject: [PATCH] Wire selectedItemBuilder through DropdownButtonFormField (#44160) --- .../flutter/lib/src/material/dropdown.dart | 2 + .../material/dropdown_form_field_test.dart | 44 +++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/packages/flutter/lib/src/material/dropdown.dart b/packages/flutter/lib/src/material/dropdown.dart index b542631f643..2106e6fb872 100644 --- a/packages/flutter/lib/src/material/dropdown.dart +++ b/packages/flutter/lib/src/material/dropdown.dart @@ -1299,6 +1299,7 @@ class DropdownButtonFormField extends FormField { Key key, T value, @required List> items, + DropdownButtonBuilder selectedItemBuilder, Widget hint, @required this.onChanged, this.decoration = const InputDecoration(), @@ -1347,6 +1348,7 @@ class DropdownButtonFormField extends FormField { child: DropdownButton( value: value, items: items, + selectedItemBuilder: selectedItemBuilder, hint: hint, onChanged: onChanged == null ? null : field.didChange, disabledHint: disabledHint, diff --git a/packages/flutter/test/material/dropdown_form_field_test.dart b/packages/flutter/test/material/dropdown_form_field_test.dart index c1baab0794b..4000e172caa 100644 --- a/packages/flutter/test/material/dropdown_form_field_test.dart +++ b/packages/flutter/test/material/dropdown_form_field_test.dart @@ -586,4 +586,48 @@ void main() { ); } }); + + testWidgets('DropdownButtonFormField - selectedItemBuilder builds custom buttons', (WidgetTester tester) async { + const List items = [ + 'One', + 'Two', + 'Three', + ]; + String selectedItem = items[0]; + + await tester.pumpWidget( + StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return MaterialApp( + home: Scaffold( + body: DropdownButtonFormField( + value: selectedItem, + onChanged: (String string) => setState(() => selectedItem = string), + selectedItemBuilder: (BuildContext context) { + int index = 0; + return items.map((String string) { + index += 1; + return Text('$string as an Arabic numeral: $index'); + }).toList(); + }, + items: items.map((String string) { + return DropdownMenuItem( + child: Text(string), + value: string, + ); + }).toList(), + ), + ), + ); + }, + ), + ); + + expect(find.text('One as an Arabic numeral: 1'), findsOneWidget); + await tester.tap(find.text('One as an Arabic numeral: 1')); + await tester.pumpAndSettle(); + await tester.tap(find.text('Two')); + await tester.pumpAndSettle(); + expect(find.text('Two as an Arabic numeral: 2'), findsOneWidget); + }); } \ No newline at end of file