Skip to content

Items in the overflow of the TextSelectionToolbar should take the width of the largest item #144089

@JannikWinter

Description

@JannikWinter

Steps to reproduce

  1. create a Material style TextSelectionToolbar with contextMenuBuilder of a TextField, TextFormField or similar
  2. make the context menu items overflow to show the overflow button
    2.1. if you are in DartPad and the menu does not overflow, you can drag the vertical divider between the code and the display to make the display smaller.
    2.2. similarly, you can add more ContextMenuButtonItems to the Toolbar to make it wider
  3. open the overflow menu

Expected results

All items in the overflow menu should have the width of the largest item

Actual results

All items have their own minimal width resulting in a non-clickable area beside them

Code sample

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

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

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

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

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();

    if (kIsWeb) {
      BrowserContextMenu.disableContextMenu();
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: TextFormField(
            initialValue: "Example Text",
            contextMenuBuilder: (context, editableTextState) {
              final anchors = editableTextState.contextMenuAnchors;
              final buttonItems = [
                ContextMenuButtonItem(label: "More items", onPressed: () {}),
                ContextMenuButtonItem(label: "To make the menu", onPressed: () {}),
                ContextMenuButtonItem(label: "Overflow", onPressed: () {}),
                ...editableTextState.contextMenuButtonItems,
                ContextMenuButtonItem(label: "Long example menu item", onPressed: () {}),
                ContextMenuButtonItem(label: "Short", onPressed: () {}),
              ];

              // Follwing is the code that AdaptiveTextSelectionToolbar uses when the platform is android.
              // I copied it to force the material style toolbar

              final List<Widget> buttons = <Widget>[];
              for (int i = 0; i < buttonItems.length; i++) {
                final ContextMenuButtonItem buttonItem = buttonItems[i];
                buttons.add(TextSelectionToolbarTextButton(
                  padding: TextSelectionToolbarTextButton.getPadding(
                      i, buttonItems.length),
                  onPressed: buttonItem.onPressed,
                  child: Text(AdaptiveTextSelectionToolbar.getButtonLabel(
                      context, buttonItem)),
                ));
              }

              return TextSelectionToolbar(
                anchorAbove: anchors.primaryAnchor,
                anchorBelow: anchors.secondaryAnchor == null
                    ? anchors.primaryAnchor
                    : anchors.secondaryAnchor!,
                children: buttons,
              );
            },
          ),
        ),
      ),
    );
  }
}

Screenshots or Video

Screenshots / Video demonstration

grafik
The buttons with shorter text do not take the full width, resulting in a non-clickable area on their right hand side

Logs

No response

Flutter Doctor output

Doctor output none

The example code was executed in DartPad with Dart 3.3.0 and Flutter 3.19.0 on the Stable channel

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work listf: material designflutter/packages/flutter/material repository.f: selectionSelectableRegion, SelectionArea, SelectionContainer, Selectable, and related APIsfound in release: 3.19Found to occur in 3.19found in release: 3.20Found to occur in 3.20frameworkflutter/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

Status

Done (PR merged)

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions