Skip to content

Conversation

@TahaTesser
Copy link
Member

@TahaTesser TahaTesser commented Oct 8, 2024

Fixes Add option to control whether the root DropdownMenu can be closed or not

This introduces DropdownMenu.closeBehavior to provide control over DropdownMenu can be closed in nested menus.

Code sample

expand to view the code sample
import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String selectedValue = "1";

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              Column(
                spacing: 16.0,
                mainAxisSize: MainAxisSize.min,
                children: [
                  const Text("DropdownMenuCloseBehavior.none"),
                  MenuAnchor(
                    menuChildren: <Widget>[
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: DropdownMenu(
                          closeBehavior: DropdownMenuCloseBehavior.none,
                          label: const Text('Menu'),
                          initialSelection: selectedValue,
                          onSelected: (String? value) {
                            if (value != null) {
                              setState(() {
                                selectedValue = value;
                              });
                            }
                          },
                          dropdownMenuEntries: ["1", "2", "3"]
                              .map(
                                (it) => DropdownMenuEntry(
                                  value: it,
                                  label: it,
                                ),
                              )
                              .toList(),
                        ),
                      )
                    ],
                    child: const Text('Open Menu'),
                    builder: (context, controller, child) {
                      return ElevatedButton(
                        onPressed: () {
                          controller.open();
                        },
                        child: child,
                      );
                    },
                  ),
                ],
              ),
              Column(
                spacing: 16.0,
                mainAxisSize: MainAxisSize.min,
                children: [
                  const Text("DropdownMenuCloseBehavior.self"),
                  MenuAnchor(
                    menuChildren: <Widget>[
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: DropdownMenu(
                          closeBehavior: DropdownMenuCloseBehavior.self,
                          label: const Text('Menu'),
                          initialSelection: selectedValue,
                          onSelected: (String? value) {
                            if (value != null) {
                              setState(() {
                                selectedValue = value;
                              });
                            }
                          },
                          dropdownMenuEntries: ["1", "2", "3"]
                              .map(
                                (it) => DropdownMenuEntry(
                                  value: it,
                                  label: it,
                                ),
                              )
                              .toList(),
                        ),
                      )
                    ],
                    child: const Text('Open Menu'),
                    builder: (context, controller, child) {
                      return ElevatedButton(
                        onPressed: () {
                          controller.open();
                        },
                        child: child,
                      );
                    },
                  ),
                ],
              ),
              Column(
                spacing: 16.0,
                mainAxisSize: MainAxisSize.min,
                children: [
                  const Text("DropdownMenuCloseBehavior.all"),
                  MenuAnchor(
                    menuChildren: <Widget>[
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: DropdownMenu(
                          closeBehavior: DropdownMenuCloseBehavior.all,
                          label: const Text('Menu'),
                          initialSelection: selectedValue,
                          onSelected: (String? value) {
                            if (value != null) {
                              setState(() {
                                selectedValue = value;
                              });
                            }
                          },
                          dropdownMenuEntries: ["1", "2", "3"]
                              .map(
                                (it) => DropdownMenuEntry(
                                  value: it,
                                  label: it,
                                ),
                              )
                              .toList(),
                        ),
                      )
                    ],
                    child: const Text('Open Menu'),
                    builder: (context, controller, child) {
                      return ElevatedButton(
                        onPressed: () {
                          controller.open();
                        },
                        child: child,
                      );
                    },
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Demo

dropdown.mp4

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@github-actions github-actions bot added framework flutter/packages/flutter repository. See also f: labels. f: material design flutter/packages/flutter/material repository. labels Oct 8, 2024
@TahaTesser TahaTesser requested a review from QuncCccccc October 8, 2024 18:29
@guidezpl
Copy link
Member

guidezpl commented Oct 9, 2024

drive-by comment to learn what the screen recording app is?

@TahaTesser
Copy link
Member Author

drive-by comment to learn what the screen recording app is?

Screen Studio

}

/// Defines the behavior for closing the dropdown menu when an item is selected.
enum CloseBehavior {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since enums are in the global namespace, this should be called something like DropdownMenuCloseBehavior so it doesn't get confused with something that applies to all kinds of closing something.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree! Thank you so much.

Copy link
Contributor

@gspencergoog gspencergoog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

32384589-a60f0e74-c078-11e7-9bc1-e5b5287aea9d

@TahaTesser TahaTesser added the autosubmit Merge PR when tree becomes green via auto submit App label Oct 11, 2024
@auto-submit auto-submit bot merged commit 45033a2 into flutter:master Oct 11, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 12, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 12, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 12, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 13, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 13, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 14, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 16, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 17, 2024
@TahaTesser TahaTesser deleted the DropdownMenu.closeBehavior branch October 22, 2024 07:40
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 12, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 13, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 13, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 6, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

autosubmit Merge PR when tree becomes green via auto submit App f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add option to control whether the root DropdownMenu can be closed or not

3 participants