flet icon indicating copy to clipboard operation
flet copied to clipboard

Control event transparency

Open mikaelho opened this issue 3 years ago • 8 comments

Often there is a need to place a control on top of another, and control which control gets e.g. a mouse click.

I just tested placing Text on top of ElevatedButton, and observed that the Text blocked the click event as well as "semantics" (changing the cursor indicating that a click is possible).

While I would like this to be a parameter on all Controls, flutter's answer seems to be yet another separate wrapping Control, IgnorePointer.

But this stackoverflow answer does point to an alternative using a custom hit detection algorithm.

mikaelho avatar Aug 11 '22 15:08 mikaelho

Could you drop a simple repro code please?

FeodorFitsner avatar Aug 11 '22 15:08 FeodorFitsner

Hmm, slightly confusing, in some contexts the text blocks the event, in another it does not. Stack did not change the behaviour here.

image
import flet
from flet import Column, Container, ElevatedButton, Page, Stack, Text, colors

def clicked(e):
    print(f"{e.control} clicked")

def main(page: Page):

    root = Column(
        [
            Container(
                Text(value="This text does not block Container clicks"),
                bgcolor=colors.BLUE_GREY_100,
                on_click=clicked,
            ),
            Stack(
                [
                    ElevatedButton(text="This is a button", on_click=clicked),
                    Text(value="This text blocks ElevatedButton clicks"),
                ]
            ),
        ]
    )

    page.add(root)
    page.update()

flet.app(target=main)

mikaelho avatar Aug 11 '22 16:08 mikaelho

After a moment of thought, I understand the difference between the two cases above.

My mistake is always thinking of Container as a separate Control, when it is really only applying characteristics to the contained Control.

Thus in the example above, Text in a Container is not event-transparent; instead, the Container makes the Text clickable.

(Part of my confusion comes from the fact that the event.control in this case is the Container, not Text.)

mikaelho avatar Aug 11 '22 16:08 mikaelho

I think we could implement it using Dropdown but there is no way to remove the trailing arrows at the end of dropdown option.

Animedecoder avatar Aug 11 '22 16:08 Animedecoder

I think we could implement it using Dropdown but there is no way to remove the trailing arrows at the end of dropdown option.

Please note that this issue is not about the dropdown menu, but about event handling in general. Please create another issue about adding text to PopupMenuButton.

mikaelho avatar Aug 11 '22 16:08 mikaelho

Did you know that you can properly put Text (or anything) inside ElevatedButton.content?

FeodorFitsner avatar Aug 11 '22 16:08 FeodorFitsner

Did you know that you can properly put Text (or anything) inside ElevatedButton.content?

No, I do not think I had looked at that, and thanks, that's a solid practical tip. 🙂

Also, now that I look at it, PopupMenuButton documentation says that it has content as well, but I was initially looking at the code, where I do not see the content included.

But this issue is still intended to be about managing event handling for overlapping controls.

mikaelho avatar Aug 11 '22 16:08 mikaelho

It's PopupMenuItem has content property, not PopupMenuButton. Don't see any issues adding PopupMenuButton.content though. Will do it in the next release.

FeodorFitsner avatar Aug 11 '22 19:08 FeodorFitsner