diff --git a/packages/flutter/lib/src/material/dropdown.dart b/packages/flutter/lib/src/material/dropdown.dart index 0d5828d580..5e27574a86 100644 --- a/packages/flutter/lib/src/material/dropdown.dart +++ b/packages/flutter/lib/src/material/dropdown.dart @@ -12,6 +12,7 @@ import 'constants.dart'; import 'debug.dart'; import 'icons.dart'; import 'ink_well.dart'; +import 'input_decorator.dart'; import 'material.dart'; import 'material_localizations.dart'; import 'scrollbar.dart'; @@ -763,3 +764,62 @@ class _DropdownButtonState extends State> with WidgetsBindi ); } } + +/// A convenience widget that wraps a [DropdownButton] in a [FormField]. +class DropdownButtonFormField extends FormField { + /// Creates a [DropdownButton] widget wrapped in an [InputDecorator] and + /// [FormField]. + /// + /// The [DropdownButton] [items] parameters must not be null. + DropdownButtonFormField({ + Key key, + T value, + @required List> items, + this.onChanged, + InputDecoration decoration = const InputDecoration(), + FormFieldSetter onSaved, + FormFieldValidator validator, + Widget hint, + }) : assert(decoration != null), + super( + key: key, + onSaved: onSaved, + initialValue: value, + validator: validator, + builder: (FormFieldState field) { + final InputDecoration effectiveDecoration = decoration + .applyDefaults(Theme.of(field.context).inputDecorationTheme); + return InputDecorator( + decoration: effectiveDecoration.copyWith(errorText: field.errorText), + isEmpty: value == null, + child: DropdownButtonHideUnderline( + child: DropdownButton( + isDense: true, + value: value, + items: items, + hint: hint, + onChanged: field.didChange, + ), + ), + ); + } + ); + + /// Called when the user selects an item. + final ValueChanged onChanged; + + @override + FormFieldState createState() => _DropdownButtonFormFieldState(); +} + +class _DropdownButtonFormFieldState extends FormFieldState { + @override + DropdownButtonFormField get widget => super.widget; + + @override + void didChange(T value) { + super.didChange(value); + if (widget.onChanged != null) + widget.onChanged(value); + } +} diff --git a/packages/flutter/test/material/dropdown_test.dart b/packages/flutter/test/material/dropdown_test.dart index 486ce2076c..55ab9b8b29 100644 --- a/packages/flutter/test/material/dropdown_test.dart +++ b/packages/flutter/test/material/dropdown_test.dart @@ -237,6 +237,48 @@ void main() { expect(value, equals('two')); }); + testWidgets('Dropdown form field', (WidgetTester tester) async { + String value = 'one'; + + await tester.pumpWidget( + StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return MaterialApp( + home: Material( + child: DropdownButtonFormField( + value: value, + hint: const Text('Select Value'), + decoration: const InputDecoration( + prefixIcon: Icon(Icons.fastfood) + ), + items: menuItems.map((String val) { + return DropdownMenuItem( + value: val, + child: Text(val) + ); + }).toList(), + onChanged: (String v) { + setState(() { + value = v; + }); + }, + validator: (String v) => v == null ? 'Must select value' : null, + ), + ), + ); + } + ) + ); + + expect(value, equals('one')); + await tester.tap(find.text('one')); + await tester.pumpAndSettle(); + await tester.tap(find.text('three').last); + await tester.pump(); + await tester.pumpAndSettle(); + expect(value, equals('three')); + }); + testWidgets('Dropdown in ListView', (WidgetTester tester) async { // Regression test for https://github.com/flutter/flutter/issues/12053 // Positions a DropdownButton at the left and right edges of the screen,