Coder Social home page Coder Social logo

Comments (3)

Titanantony avatar Titanantony commented on June 1, 2024 1

_It looks like you're trying to position a PopupMenu in Flutter using showMenu with a RelativeRect. However, the positioning you've described might not work as expected because RelativeRect.fromLTRB sets the left, top, right, and bottom offsets of the rectangle relative to the parent widget's edges, but it doesn't take into account the menu's size.

To achieve the desired separation from the bottom right corner, you can calculate the position dynamically based on the menu's size. Here's an updated version of your code that should position the menu 20 units from the bottom and right edges:_

CustomElevatedButton(
icon: Icons.more_vert,
onPressed: () {
final RenderBox button = context.findRenderObject() as RenderBox;
final Offset buttonPosition = button.localToGlobal(Offset.zero);

final double screenHeight = MediaQuery.of(context).size.height;
final double screenWidth = MediaQuery.of(context).size.width;

final double dx = screenWidth - buttonPosition.dx - 20;
final double dy = screenHeight - buttonPosition.dy - 20;

showMenu(
  context: context,
  position: RelativeRect.fromLTRB(dx, dy, 0, 0),
  items: const [
    PopupMenuItem(
      value: 'Añadir',
      child: Text('Añadir'),
    ),
    PopupMenuItem(
      value: 'Filtrar',
      child: Text('Filtrar'),
    ),
  ],
  elevation: 8.0,
).then((choice) {
  if (choice != null) {
    if (choice == 'Añadir') {
      showDialogMethod();
    } else if (choice == 'Filtrar') {
      // Handle 'Filtrar' choice
    }
  }
});

},
)
This code calculates the position of the button relative to the screen and then adjusts the menu's position accordingly to ensure it is 20 units from the bottom and right edges.

``

from flutter.

Rafapeerez avatar Rafapeerez commented on June 1, 2024

You're right!! The menu have been placed 20 units from the bottom edges. The 20 units of the right edge have not worked in this way, what I have done is to collect both options for it to work correctly:
CustomElevatedButton(
icon: Icons.more_vert,
onPressed: () {
final RenderBox button = context.findRenderObject() as RenderBox;
final Offset buttonPosition = button.localToGlobal(Offset.zero);

              final double screenHeight = MediaQuery.of(context).size.height;
              final double screenWidth = MediaQuery.of(context).size.width;

              final double dx = screenWidth - buttonPosition.dx;
              final double dy = screenHeight - buttonPosition.dy - 150;

              showMenu(
                context: context,
                position: RelativeRect.fromLTRB(dx, dy, 20, 0),
                items: const [
                  PopupMenuItem(
                    value: 'Añadir',
                    child: Text('Añadir'),
                  ),
                  PopupMenuItem(
                    value: 'Filtrar',
                    child: Text('Filtrar'),
                  ),
                ],
                elevation: 8.0,
              ).then((choice) {
                if (choice != null) {
                  if (choice == 'Añadir') {
                    showDialogMethod();
                  } else if (choice == 'Filtrar') {
                    // Handle 'Filtrar' choice
                  }
                }
              });
            },
          )

Thank you very much!!!!

from flutter.

danagbemava-nc avatar danagbemava-nc commented on June 1, 2024

Hi @Rafapeerez, has your issue been resolved?

from flutter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.