Skip to content

Commit a0e7807

Browse files
authored
[material_ui] Add enabled parameter to DropdownButtonFormField (flutter#12610)
Introduced the `enabled` parameter in `DropdownButtonFormField`, allowing proper disabled/read-only usage without needing to pass dummy callbacks or remove existing ones. Previously, the only way to disable a `DropdownButtonFormField` was to set `onChanged: null`. With this change, developers can explicitly control the interactive state via the `enabled` property. For example, you can set `enabled: false` while keeping `onChanged` defined, or set `enabled: true` to have an interactive field even if `onChanged` is omitted. This change is fully backward compatible. If the `enabled` property is omitted (left as `null`), the field will fall back to the legacy behavior: it will be enabled if and only if `onChanged` is non-null. Closes flutter#57953. Port of flutter#182419. ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent eca7601 commit a0e7807

3 files changed

Lines changed: 115 additions & 8 deletions

File tree

‎packages/material_ui/lib/src/dropdown.dart‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,6 +1852,22 @@ class DropdownButtonFormField<T> extends FormField<T> {
18521852
/// Creates a [DropdownButton] widget that is a [FormField], wrapped in an
18531853
/// [InputDecorator].
18541854
///
1855+
/// The `enabled` argument controls whether the user can interact with the
1856+
/// field:
1857+
///
1858+
/// * If true, the field is interactive even if [onChanged] is null. The
1859+
/// selected value is stored in the [FormFieldState].
1860+
/// * If false, the field is not interactive and its own [autovalidateMode]
1861+
/// is ignored, even if [onChanged] is non-null. [FormState.validate] and
1862+
/// the parent [Form]'s [Form.autovalidateMode] still validate it.
1863+
///
1864+
/// * If null, the field is interactive if and only if [onChanged] is
1865+
/// non-null. In either case, it is still validated. This matches the
1866+
/// behavior before `enabled` was added.
1867+
///
1868+
/// The resolved [FormField.enabled] is true unless `enabled` is explicitly
1869+
/// false.
1870+
///
18551871
/// For a description of the `onSaved`, `validator`, or `autovalidateMode`
18561872
/// parameters, see [FormField]. For the rest (other than [decoration]), see
18571873
/// [DropdownButton].
@@ -1868,7 +1884,7 @@ class DropdownButtonFormField<T> extends FormField<T> {
18681884
T? initialValue,
18691885
Widget? hint,
18701886
Widget? disabledHint,
1871-
required this.onChanged,
1887+
this.onChanged,
18721888
VoidCallback? onTap,
18731889
int elevation = 8,
18741890
TextStyle? style,
@@ -1888,6 +1904,7 @@ class DropdownButtonFormField<T> extends FormField<T> {
18881904
super.validator,
18891905
super.errorBuilder,
18901906
super.forceErrorText,
1907+
bool? enabled,
18911908
AutovalidateMode? autovalidateMode,
18921909
double? menuMaxHeight,
18931910
bool? enableFeedback,
@@ -1922,6 +1939,7 @@ class DropdownButtonFormField<T> extends FormField<T> {
19221939
super(
19231940
initialValue: initialValue ?? value,
19241941
autovalidateMode: autovalidateMode ?? AutovalidateMode.disabled,
1942+
enabled: enabled ?? true,
19251943
builder: (FormFieldState<T> field) {
19261944
final state = field as _DropdownButtonFormFieldState<T>;
19271945
InputDecoration effectiveDecoration = (decoration ?? const InputDecoration())
@@ -1930,7 +1948,8 @@ class DropdownButtonFormField<T> extends FormField<T> {
19301948
final bool showSelectedItem =
19311949
items != null &&
19321950
items.where((DropdownMenuItem<T> item) => item.value == state.value).isNotEmpty;
1933-
final bool isDropdownEnabled = onChanged != null && items != null && items.isNotEmpty;
1951+
final bool isDropdownEnabled =
1952+
(enabled ?? onChanged != null) && items != null && items.isNotEmpty;
19341953
// If decoration hintText is provided, use it as the default value for both hint and disabledHint.
19351954
final Widget? decorationHint = effectiveDecoration.hintText != null
19361955
? Text(effectiveDecoration.hintText!)
@@ -1969,7 +1988,7 @@ class DropdownButtonFormField<T> extends FormField<T> {
19691988
value: state.value,
19701989
hint: effectiveHint,
19711990
disabledHint: effectiveDisabledHint,
1972-
onChanged: onChanged == null ? null : state.didChange,
1991+
onChanged: isDropdownEnabled ? state.didChange : null,
19731992
onTap: onTap,
19741993
elevation: elevation,
19751994
style: style,
@@ -2006,6 +2025,9 @@ class DropdownButtonFormField<T> extends FormField<T> {
20062025
/// This callback is invoked after the parent [Form]'s [Form.onChanged] callback.
20072026
/// The field's updated value is available in the [Form.onChanged] callback
20082027
/// via [FormFieldState.value].
2028+
///
2029+
/// If the constructor's `enabled` argument is true, the field is interactive even
2030+
/// when [onChanged] is null.
20092031
final ValueChanged<T?>? onChanged;
20102032

20112033
/// The decoration to show around the dropdown button form field.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
changelog: |
2+
- Adds an `enabled` parameter to `DropdownButtonFormField` and makes its
3+
`onChanged` parameter optional. When `enabled` is true, the field is
4+
interactive without an `onChanged` callback.
5+
version: minor

‎packages/material_ui/test/dropdown_form_field_test.dart‎

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ void main() {
188188
child: DropdownButtonFormField<int?>(
189189
decoration: const InputDecoration(labelText: 'labelText'),
190190
initialValue: value,
191-
onChanged: null, // this disables the menu and shows the disabledHint.
191+
// onChanged is null by default, which disables the menu and shows the disabledHint.
192192
disabledHint: const Text('disabledHint'),
193193
items: const <DropdownMenuItem<int?>>[
194194
DropdownMenuItem<int?>(value: 1, child: Text('One')),
@@ -320,7 +320,7 @@ void main() {
320320
decoration: const InputDecoration(labelText: 'labelText'),
321321
initialValue: value,
322322
hint: const Text('hint'),
323-
onChanged: null, // disabled
323+
// onChanged is null by default, so disabled
324324
disabledHint: const Text('disabledHint'),
325325
items: const <DropdownMenuItem<int?>>[
326326
DropdownMenuItem<int?>(value: 1, child: Text('One')),
@@ -1183,7 +1183,6 @@ void main() {
11831183
) {
11841184
return DropdownMenuItem<String>(value: value, child: Text(value));
11851185
}).toList(),
1186-
onChanged: null,
11871186
),
11881187
),
11891188
),
@@ -1315,7 +1314,6 @@ void main() {
13151314
items: menuItems.map((String value) {
13161315
return DropdownMenuItem<String>(value: value, child: Text(value));
13171316
}).toList(),
1318-
onChanged: null,
13191317
autovalidateMode: AutovalidateMode.disabled,
13201318
),
13211319
DropdownButtonFormField<String>(
@@ -1364,7 +1362,6 @@ void main() {
13641362
}).toList(),
13651363
decoration: const InputDecoration(errorText: decorationErrorText),
13661364
forceErrorText: forceErrorText,
1367-
onChanged: null,
13681365
),
13691366
),
13701367
),
@@ -1529,4 +1526,87 @@ void main() {
15291526
SystemMouseCursors.cell,
15301527
);
15311528
});
1529+
1530+
testWidgets(
1531+
'DropdownButtonFormField enabled: true without onChanged updates FormFieldState.value',
1532+
(WidgetTester tester) async {
1533+
final fieldKey = GlobalKey<FormFieldState<int>>();
1534+
await tester.pumpWidget(
1535+
MaterialApp(
1536+
home: Material(
1537+
child: DropdownButtonFormField<int>(
1538+
key: fieldKey,
1539+
initialValue: 1,
1540+
items: const <DropdownMenuItem<int>>[
1541+
DropdownMenuItem<int>(value: 1, child: Text('one')),
1542+
DropdownMenuItem<int>(value: 2, child: Text('two')),
1543+
],
1544+
enabled: true,
1545+
),
1546+
),
1547+
),
1548+
);
1549+
1550+
// Open the menu,
1551+
await tester.tap(find.text('one'));
1552+
await tester.pumpAndSettle();
1553+
1554+
// Select the second item,
1555+
await tester.tap(find.text('two').last);
1556+
await tester.pumpAndSettle();
1557+
1558+
// Menu should be closed and value updated.
1559+
expect(fieldKey.currentState!.value, 2);
1560+
expect(find.text('two'), findsOneWidget);
1561+
},
1562+
);
1563+
1564+
testWidgets('DropdownButtonFormField enabled: false with onChanged does not open menu', (
1565+
WidgetTester tester,
1566+
) async {
1567+
await tester.pumpWidget(
1568+
MaterialApp(
1569+
home: Material(
1570+
child: DropdownButtonFormField<int>(
1571+
initialValue: 1,
1572+
items: const <DropdownMenuItem<int>>[
1573+
DropdownMenuItem<int>(value: 1, child: Text('one')),
1574+
DropdownMenuItem<int>(value: 2, child: Text('two')),
1575+
],
1576+
enabled: false,
1577+
onChanged: (int? v) {},
1578+
),
1579+
),
1580+
),
1581+
);
1582+
1583+
// Attempt to open the menu.
1584+
await tester.tap(find.text('one'));
1585+
await tester.pumpAndSettle();
1586+
1587+
// Menu should not open, so 'two' should not be visible.
1588+
expect(find.text('two'), findsNothing);
1589+
});
1590+
1591+
testWidgets('DropdownButtonFormField with onChanged: null still autovalidates', (
1592+
WidgetTester tester,
1593+
) async {
1594+
await tester.pumpWidget(
1595+
MaterialApp(
1596+
home: Material(
1597+
child: DropdownButtonFormField<int>(
1598+
initialValue: 1,
1599+
items: const <DropdownMenuItem<int>>[
1600+
DropdownMenuItem<int>(value: 1, child: Text('one')),
1601+
DropdownMenuItem<int>(value: 2, child: Text('two')),
1602+
],
1603+
autovalidateMode: AutovalidateMode.always,
1604+
validator: (int? value) => 'error text',
1605+
),
1606+
),
1607+
),
1608+
);
1609+
1610+
expect(find.text('error text'), findsOneWidget);
1611+
});
15321612
}

0 commit comments

Comments
 (0)