From 986c5a9d7f186e969d4a77229a72fc971de01426 Mon Sep 17 00:00:00 2001 From: Bruno Leroux Date: Tue, 7 May 2024 09:57:21 +0200 Subject: [PATCH] DropdownMenu cleanup (#147860) ## Description This PR cleans up some typos and formatting issues in `DropdownMenu` implementation. ## Tests No semantic change. --- .../lib/src/material/dropdown_menu.dart | 131 +++++++++--------- 1 file changed, 65 insertions(+), 66 deletions(-) diff --git a/packages/flutter/lib/src/material/dropdown_menu.dart b/packages/flutter/lib/src/material/dropdown_menu.dart index 8258aceb85..47d04ada81 100644 --- a/packages/flutter/lib/src/material/dropdown_menu.dart +++ b/packages/flutter/lib/src/material/dropdown_menu.dart @@ -376,7 +376,7 @@ class DropdownMenu extends StatefulWidget { /// Defaults to null. final EdgeInsets? expandedInsets; - /// When [DropdownMenu.enableSearch] is true, this callback is used to compute + /// When [DropdownMenu.enableSearch] is true, this callback is used to compute /// the index of the search result to be highlighted. /// /// {@tool snippet} @@ -460,10 +460,10 @@ class _DropdownMenuState extends State> { @override void dispose() { - if (widget.controller == null) { - _localTextEditingController?.dispose(); - _localTextEditingController = null; - } + if (widget.controller == null) { + _localTextEditingController?.dispose(); + _localTextEditingController = null; + } super.dispose(); } @@ -557,9 +557,10 @@ class _DropdownMenuState extends State> { List _buildButtons( List> filteredEntries, - TextDirection textDirection, - { int? focusedIndex, bool enableScrollToHighlight = true} - ) { + TextDirection textDirection, { + int? focusedIndex, + bool enableScrollToHighlight = true, + }) { final List result = []; for (int i = 0; i < filteredEntries.length; i++) { final DropdownMenuEntry entry = filteredEntries[i]; @@ -699,9 +700,7 @@ class _DropdownMenuState extends State> { final TextStyle? effectiveTextStyle = widget.textStyle ?? theme.textStyle ?? defaults.textStyle; - MenuStyle? effectiveMenuStyle = widget.menuStyle - ?? theme.menuStyle - ?? defaults.menuStyle!; + MenuStyle? effectiveMenuStyle = widget.menuStyle ?? theme.menuStyle ?? defaults.menuStyle!; final double? anchorWidth = getWidth(_anchorKey); if (widget.width != null) { @@ -742,60 +741,60 @@ class _DropdownMenuState extends State> { ); final Widget leadingButton = Padding( - padding: const EdgeInsets.all(8.0), - child: widget.leadingIcon ?? const SizedBox() + padding: const EdgeInsets.all(8.0), + child: widget.leadingIcon ?? const SizedBox.shrink() ); final Widget textField = TextField( - key: _anchorKey, - mouseCursor: effectiveMouseCursor, - focusNode: widget.focusNode, - canRequestFocus: canRequestFocus(), - enableInteractiveSelection: canRequestFocus(), - textAlignVertical: TextAlignVertical.center, - style: effectiveTextStyle, - controller: _localTextEditingController, - onEditingComplete: () { - if (currentHighlight != null) { - final DropdownMenuEntry entry = filteredEntries[currentHighlight!]; - if (entry.enabled) { - _localTextEditingController?.value = TextEditingValue( - text: entry.label, - selection: TextSelection.collapsed(offset: entry.label.length), - ); - widget.onSelected?.call(entry.value); - } - } else { - widget.onSelected?.call(null); + key: _anchorKey, + mouseCursor: effectiveMouseCursor, + focusNode: widget.focusNode, + canRequestFocus: canRequestFocus(), + enableInteractiveSelection: canRequestFocus(), + textAlignVertical: TextAlignVertical.center, + style: effectiveTextStyle, + controller: _localTextEditingController, + onEditingComplete: () { + if (currentHighlight != null) { + final DropdownMenuEntry entry = filteredEntries[currentHighlight!]; + if (entry.enabled) { + _localTextEditingController?.value = TextEditingValue( + text: entry.label, + selection: TextSelection.collapsed(offset: entry.label.length), + ); + widget.onSelected?.call(entry.value); } - if (!widget.enableSearch) { - currentHighlight = null; - } - controller.close(); - }, - onTap: () { - handlePressed(controller); - }, - onChanged: (String text) { - controller.open(); - setState(() { - filteredEntries = widget.dropdownMenuEntries; - _enableFilter = widget.enableFilter; - }); - }, - inputFormatters: widget.inputFormatters, - decoration: InputDecoration( - enabled: widget.enabled, - label: widget.label, - hintText: widget.hintText, - helperText: widget.helperText, - errorText: widget.errorText, - prefixIcon: widget.leadingIcon != null ? Container( - key: _leadingKey, - child: widget.leadingIcon - ) : null, - suffixIcon: trailingButton, - ).applyDefaults(effectiveInputDecorationTheme) + } else { + widget.onSelected?.call(null); + } + if (!widget.enableSearch) { + currentHighlight = null; + } + controller.close(); + }, + onTap: () { + handlePressed(controller); + }, + onChanged: (String text) { + controller.open(); + setState(() { + filteredEntries = widget.dropdownMenuEntries; + _enableFilter = widget.enableFilter; + }); + }, + inputFormatters: widget.inputFormatters, + decoration: InputDecoration( + enabled: widget.enabled, + label: widget.label, + hintText: widget.hintText, + helperText: widget.helperText, + errorText: widget.errorText, + prefixIcon: widget.leadingIcon != null ? Container( + key: _leadingKey, + child: widget.leadingIcon + ) : null, + suffixIcon: trailingButton, + ).applyDefaults(effectiveInputDecorationTheme) ); if (widget.expandedInsets != null) { @@ -809,7 +808,7 @@ class _DropdownMenuState extends State> { width: widget.width, children: [ textField, - for (final Widget item in _initialMenu!) item, + ..._initialMenu!, trailingButton, leadingButton, ], @@ -1032,21 +1031,21 @@ class _RenderDropdownMenuBody extends RenderBox } @override - double computeMinIntrinsicHeight(double height) { + double computeMinIntrinsicHeight(double width) { final RenderBox? child = firstChild; double width = 0; if (child != null) { - width = math.max(width, child.getMinIntrinsicHeight(height)); + width = math.max(width, child.getMinIntrinsicHeight(width)); } return width; } @override - double computeMaxIntrinsicHeight(double height) { + double computeMaxIntrinsicHeight(double width) { final RenderBox? child = firstChild; double width = 0; if (child != null) { - width = math.max(width, child.getMaxIntrinsicHeight(height)); + width = math.max(width, child.getMaxIntrinsicHeight(width)); } return width; }