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

Dismissible.confirmDismiss prop #2359

Merged
merged 5 commits into from
Jan 12, 2024
Merged

Conversation

ndonkoHenri
Copy link
Contributor

@ndonkoHenri ndonkoHenri commented Jan 9, 2024

Closes #2130

Example:

import flet as ft


def main(page):
    page.window_height, page.window_width = 500, 400

    def close_yes_dlg(e):
        page.close_dialog()
        dlg_modal.data.confirm_dismiss(True)

    def close_no_dlg(e):
        page.close_dialog()
        dlg_modal.data.confirm_dismiss(False)

    dlg_modal = ft.AlertDialog(
        modal=True,
        title=ft.Text("Please confirm"),
        content=ft.Text("Do you really want to delete this item?"),
        actions=[
            ft.TextButton("Yes", on_click=close_yes_dlg),
            ft.TextButton("No", on_click=close_no_dlg),
        ],
        actions_alignment=ft.MainAxisAlignment.CENTER,
    )

    def handle_confirm_dismiss(e: ft.DismissibleDismissEvent):
        if e.direction == ft.DismissDirection.END_TO_START:
            # save current dismissible to dialog's data
            dlg_modal.data = e.control
            page.show_dialog(dlg_modal)
        else:
            e.control.confirm_dismiss(True)

    def handle_dismiss(e):
        lv.controls.remove(e.control)
        page.update()

    def handle_update(e: ft.DismissibleUpdateEvent):
        print(f"Update - direction: {e.direction}, progress: {e.progress}, reached: {e.reached}, previous_reached: {e.previous_reached}")

    page.add(
        lv := ft.ListView(
            controls=[
                ft.Dismissible(
                    content=ft.ListTile(title=ft.Text(f"Item {i}")),
                    dismiss_direction=ft.DismissDirection.HORIZONTAL,
                    background=ft.Container(bgcolor=ft.colors.GREEN),
                    secondary_background=ft.Container(bgcolor=ft.colors.RED),
                    on_dismiss=handle_dismiss, 
                    on_update=handle_update,
                    on_confirm_dismiss=handle_confirm_dismiss,
                    dismiss_thresholds={
                        ft.DismissDirection.HORIZONTAL: 0.1,
                        ft.DismissDirection.START_TO_END: 0.1,
                    },
                )
                for i in range(10)
            ],
            expand=True,
        )
    )


ft.app(main)

@FeodorFitsner
Copy link
Contributor

Also, I noticed we need to provide details for on_update handler. It allows having precise control over dismissible position while user swipes it. @ndonkoHenri will you add it?

@FeodorFitsner FeodorFitsner merged commit 1316222 into main Jan 12, 2024
2 checks passed
@ndonkoHenri ndonkoHenri deleted the dismissible-confirm-dismiss branch January 12, 2024 22:40
zrr1999 pushed a commit to zrr1999/flet that referenced this pull request Jul 17, 2024
* initial commit

* Dismissible.on_confirm_dismiss

* rename onDismissed param

* update event details

---------

Co-authored-by: Feodor Fitsner <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
controls enhancement Improvement/Optimization
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Add Dismissible.confirm_dismiss event handler
2 participants