Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DropdownMenu cleanup #147860

Merged
merged 1 commit into from
May 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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