Skip to content

Dropdown default minimumSize when maximumSize is null returns a width of 0 #170970

@EdwynZN

Description

@EdwynZN

Steps to reproduce

  1. Create a Dropdown widget without minimum or maximum width (let it expand to the parent size)
  2. open the dropdown and see the items not be expanded to the same size the Dropdown, it should be the same size that the anchor parent

The problem is in dropdown_menu.dart line 969:

  if (widget.width != null) {
      effectiveMenuStyle = effectiveMenuStyle.copyWith(
        minimumSize: MaterialStateProperty.resolveWith<Size?>((Set<MaterialState> states) {
          final double? effectiveMaximumWidth =
              effectiveMenuStyle!.maximumSize?.resolve(states)?.width;
          return Size(math.min(widget.width!, effectiveMaximumWidth ?? 0.0), 0.0);
        }),
      );
    } else if (anchorWidth != null) {
      effectiveMenuStyle = effectiveMenuStyle.copyWith(
        minimumSize: MaterialStateProperty.resolveWith<Size?>((Set<MaterialState> states) {
          final double? effectiveMaximumWidth =
              effectiveMenuStyle!.maximumSize?.resolve(states)?.width;
          return Size(math.min(anchorWidth, effectiveMaximumWidth ?? 0.0), 0.0);
        }),
      );
    }

When a maximum size is not being applied in the theme or directly in the widget this instructions return a minumim size of Size(0.0, 0.0) because when using math.min of effectiveMaximumWidth (and this is null) ?? 0.0 will always be <= anchorWidth or widget.width.

Instead when effectiveMaximumWidth is null this should use the size of the anchor or the one given in width:

Size(math.min(anchorWidth, effectiveMaximumWidth ?? anchorWidth)

Size(math.min(widget.width!, effectiveMaximumWidth ?? widget.width)

Expected results

This was the result prior 3.32

Actual results

Code sample

Code sample
import 'package:flutter/material.dart';

void main() {
  runApp(const MainApp());
}

class MainApp extends StatelessWidget {
  const MainApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.light().copyWith(
        dropdownMenuTheme: DropdownMenuThemeData(
          textStyle: const TextStyle(fontSize: 14),
          inputDecorationTheme: const InputDecorationTheme(
            border: UnderlineInputBorder(),
          ),
          menuStyle: MenuStyle(
            shape: const WidgetStatePropertyAll(
              RoundedRectangleBorder(
                borderRadius: BorderRadius.all(Radius.circular(8)),
              ),
            ),
            /// This is the only way to make it expand to the same size the 
            /// parent dropdown (if there is no constraint of the anchro to the parent)
            /// but shouldn't be a requirement
            maximumSize: const WidgetStatePropertyAll(Size.infinite),
            padding: const WidgetStatePropertyAll(EdgeInsets.zero),
            visualDensity: const VisualDensity(vertical: -2),
            side: WidgetStatePropertyAll(
              BorderSide(color: Colors.grey.shade200),
            ),
            backgroundColor: WidgetStateColor.fromMap({
              WidgetState.disabled: Colors.grey.shade400,
              WidgetState.selected: Colors.purple.shade600,
              WidgetState.any: Colors.white,
            }),
          ),
        ),
      ),
      home: const Scaffold(
        body: SafeArea(minimum: EdgeInsets.all(16), child: DropdownExample()),
      ),
    );
  }
}

enum Gender { male, female }

class DropdownExample extends StatelessWidget {
  const DropdownExample({super.key});

  @override
  Widget build(BuildContext context) {
    return DropdownMenu(
      dropdownMenuEntries: const [
        DropdownMenuEntry(value: Gender.female, label: 'Female'),
        DropdownMenuEntry(value: Gender.male, label: 'Male'),
      ],
      enableSearch: false,
      expandedInsets: EdgeInsets.zero,
      hintText: 'Select an option',
      selectedTrailingIcon: const Icon(Icons.keyboard_arrow_up, size: 16),
      trailingIcon: const Icon(Icons.keyboard_arrow_down, size: 16),
    );
  }
}

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.32.2, on macOS 15.5 24F74 darwin-arm64, locale en-MX)
[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 16.2)
[✗] Chrome - develop for the web (Cannot find Chrome executable at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome)
    ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.
[✓] Android Studio (version 2024.3)
[✓] VS Code (version 1.101.1)
[✓] Connected device (2 available)
[✓] Network resources

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listf: material designflutter/packages/flutter/material repository.found in release: 3.32Found to occur in 3.32found in release: 3.33Found to occur in 3.33frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onr: fixedIssue is closed as already fixed in a newer versionteam-designOwned by Design Languages teamtriaged-designTriaged by Design Languages team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions