Skip to content

Commit

Permalink
DropdownMenu cleanup (#147860)
Browse files Browse the repository at this point in the history
## Description

This PR cleans up some typos and formatting issues in `DropdownMenu` implementation.

## Tests

No semantic change.
  • Loading branch information
bleroux committed May 7, 2024
1 parent a727202 commit 986c5a9
Showing 1 changed file with 65 additions and 66 deletions.
131 changes: 65 additions & 66 deletions packages/flutter/lib/src/material/dropdown_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ class DropdownMenu<T> 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}
Expand Down Expand Up @@ -460,10 +460,10 @@ class _DropdownMenuState<T> extends State<DropdownMenu<T>> {

@override
void dispose() {
if (widget.controller == null) {
_localTextEditingController?.dispose();
_localTextEditingController = null;
}
if (widget.controller == null) {
_localTextEditingController?.dispose();
_localTextEditingController = null;
}
super.dispose();
}

Expand Down Expand Up @@ -557,9 +557,10 @@ class _DropdownMenuState<T> extends State<DropdownMenu<T>> {

List<Widget> _buildButtons(
List<DropdownMenuEntry<T>> filteredEntries,
TextDirection textDirection,
{ int? focusedIndex, bool enableScrollToHighlight = true}
) {
TextDirection textDirection, {
int? focusedIndex,
bool enableScrollToHighlight = true,
}) {
final List<Widget> result = <Widget>[];
for (int i = 0; i < filteredEntries.length; i++) {
final DropdownMenuEntry<T> entry = filteredEntries[i];
Expand Down Expand Up @@ -699,9 +700,7 @@ class _DropdownMenuState<T> extends State<DropdownMenu<T>> {

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) {
Expand Down Expand Up @@ -742,60 +741,60 @@ class _DropdownMenuState<T> extends State<DropdownMenu<T>> {
);

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<T> 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);
}
if (!widget.enableSearch) {
currentHighlight = 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<T> entry = filteredEntries[currentHighlight!];
if (entry.enabled) {
_localTextEditingController?.value = TextEditingValue(
text: entry.label,
selection: TextSelection.collapsed(offset: entry.label.length),
);
widget.onSelected?.call(entry.value);
}
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) {
Expand All @@ -809,7 +808,7 @@ class _DropdownMenuState<T> extends State<DropdownMenu<T>> {
width: widget.width,
children: <Widget>[
textField,
for (final Widget item in _initialMenu!) item,
..._initialMenu!,
trailingButton,
leadingButton,
],
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 986c5a9

Please sign in to comment.