Flet - modal dialog - text field doesn't work properly if created by assign the key

42 Views Asked by At

I was met a problem while testing creating modal window using Flet. The problem is that if i create in alertdialog content a text field with key for example: content=ft.TextField(key='any_key_name', value="change that text"), after running and open that modal window and trying to write in that field i can write there only one symbol (letter, number, etc).

below is the default code from flet documentation, with only one change, i added the line with creation TextField by assign the key.

import flet as ft


def example():
    def open_dlg_modal(e):
        e.control.page.dialog = dlg_modal
        dlg_modal.open = True
        e.control.page.update()

    def close_dlg(e):
        dlg_modal.open = False
        e.control.page.update()

    dlg_modal = ft.AlertDialog(
        modal=True,
        title=ft.Text("Please confirm"),
        # content=ft.TextField(key='any_key_name', value="change that text"), # with key don't work properly
        content=ft.TextField(value="change that text"),
        actions=[
            ft.TextButton("Yes", on_click=close_dlg),
            ft.TextButton("No", on_click=close_dlg),
        ],
        actions_alignment=ft.MainAxisAlignment.END,
    )

    return ft.Column(
        [
            ft.ElevatedButton("Open modal dialog", on_click=open_dlg_modal),
        ]
    )


def main(page):
    page.title = "Card Example"
    page.add(
        example()
    )


ft.app(target=main)







Also i found a crazy way to solve that problem by creating a function set_focus and bind in the TextField on_focus=lambda ev: set_focus(ev):

def set_focus(event):
    event.controls.focus

but in that case cursor is always focused in that field and blinks so fast. Another way is to set TextField property autofocus= True, but in that case the focus will be always in the first field, even if you select another it will change focus immediately.

If anyone have a solution, please help. Thanks all and good code :)

0

There are 0 best solutions below