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

Repositioning of context menu when the content expands or contracts #5

Open
jlnrrg opened this issue Nov 21, 2021 · 3 comments
Open

Comments

@jlnrrg
Copy link

jlnrrg commented Nov 21, 2021

  1. I would assume the widget not to jump around on click within.
  2. I assume a Navigator.pop(context); would close the context menu, but this does not seem to be the case.
  3. The cardBuilder of ContextMenuOverlay does not seem to work. (It is to note, that my ContextMenuOverlay is placed under the MaterialApp as of ContextMenuOverlay does not work as shown in example #4.
  4. Also it can happen that the context menu is repositions such that some of its area is placed outside of the visible screen. (Might also be because I work with ExpansionTile)

Regarding 1, here is an explanation video:

context_menu.mp4
@jlnrrg
Copy link
Author

jlnrrg commented Nov 21, 2021

For people comming after me, I can recommend to use the showMenu of flutter until the issues are fixed. stackoverflow

example classes
class ContextMenu extends StatelessWidget {
  const ContextMenu(
      {Key? key, this.menuBuilder, this.secondaryTap, required this.child})
      : super(key: key);

  final List<Widget> Function(BuildContext)? menuBuilder;
  final VoidCallback? secondaryTap;
  final Widget child;

  void showContextMenu(BuildContext context, Offset globalPosition) {
    final RenderBox? overlay =
        Overlay.of(context)?.context.findRenderObject() as RenderBox?;
    showMenu<void>(
        context: context,
        position: RelativeRect.fromRect(
            globalPosition & const Size(40, 40), // smaller rect, the touch area
            Offset.zero &
                (overlay?.size ?? Size.zero) // Bigger rect, the entire screen
            ),
        items: (menuBuilder?.call(context) ?? [])
            .map((e) => _CustomPopupEntry(child: e))
            .toList());
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
                    onLongPressDown: (d) =>
                        showContextMenu(context, d.globalPosition),
                    onSecondaryTapDown: (d) =>
                        showContextMenu(context, d.globalPosition),
                    child: child);
  }
}
class _CustomPopupEntry extends PopupMenuEntry<void> {
  _CustomPopupEntry({Key? key, required this.child}) : super(key: key);

  final Widget child;

  @override
  __CustomPopupEntryState createState() => __CustomPopupEntryState();

  @override
  final double height = 100;
  // height doesn't matter, as long as we are not giving
  // initialValue to showMenu().

  @override
  bool represents(void value) {
    return true;
    // doesn't matter, as I use [PopupMenuEntry<void>]
  }
}

class __CustomPopupEntryState extends State<_CustomPopupEntry> {
  @override
  Widget build(BuildContext context) {
    return widget.child;
  }
}

@SAGARSURI
Copy link

is there any update on this problem? @gskinner

@esDotDev
Copy link
Contributor

Thanks for the report, we don't currently test context menus that change size after opening.

It's a bit tricky, as when widgets expand there are situation where you will have no choice but to reposition the widget, otherwise content will be clipped. In some cases the repositioning might be the desired behavior...Maybe we can add a repositionOnSizeChange flag or something.

As for the rest,
2. Feel free to file a feature request for observing Navigator and closing on pop
3. It seems to be working fine, I've added a test for it in the example.

@esDotDev esDotDev changed the title Repositioning of context menu on click Repositioning of context menu when the content expands or contracts May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants