Created
January 16, 2024 00:57
-
-
Save ndonkoHenri/45bb83df48ebdd4fc323a9156b3c3539 to your computer and use it in GitHub Desktop.
An example of how to use Flet's Dismissible.on_confirm_dismiss event handler.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import flet as ft | |
def main(page): | |
page.window_height, page.window_width = 500, 400 | |
def close_yes_dlg(e): | |
page.close_dialog() | |
dlg.data.confirm_dismiss(True) | |
def close_no_dlg(e): | |
page.close_dialog() | |
dlg.data.confirm_dismiss(False) | |
dlg = 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.data = e.control | |
page.show_dialog(dlg) | |
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(ft.Icon(ft.icons.CHECK), bgcolor=ft.colors.GREEN, alignment=ft.alignment.center_left, padding=ft.padding.only(left=10)), | |
secondary_background=ft.Container(ft.Icon(ft.icons.DELETE), bgcolor=ft.colors.RED, alignment=ft.alignment.center_right, padding=ft.padding.only(right=10)), | |
on_dismiss=handle_dismiss, | |
on_update=handle_update, | |
on_confirm_dismiss=handle_confirm_dismiss, | |
) | |
for i in range(5) | |
], | |
expand=True, | |
) | |
) | |
ft.app(main) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment