flet-dev / flet

Flet enables developers to easily build realtime web, mobile and desktop apps in Python. No frontend experience required.
https://flet.dev
Apache License 2.0
11.46k stars 447 forks source link

TextField loses keyboard focus after every typed character if 'key' is not None #1460

Closed rulerofthehuns closed 1 year ago

rulerofthehuns commented 1 year ago

If the 'key' property of a TextField instance is not null, textfield loses focus after each typed character. Seems to work file if 'key' is None

Code example to reproduce the issue:

import flet as ft

def main(page: ft.Page):
    page.add( ft.TextField(
        key="losing focus" # remove me to not lose focus 
        ) )

ft.app(
    target=main,
    view=ft.WEB_BROWSER
)

Describe the results you received: Click in textfield, start typing, keyboard focus disappears after 1 character. Happens with or without 'view=ft.WEB_BROWSER'

Describe the results you expected: Be able to type

Additional information you deem important (e.g. issue happens only occasionally):

Flet version (pip show flet):

Name: flet
Version: 0.7.4
Summary: Flet for Python - easily build interactive multi-platform apps in Python
Home-page:
Author: Appveyor Systems Inc.
Author-email: hello@flet.dev
License: Apache-2.0
Location: D:\dev\spacetraders\.venv\Lib\site-packages
Requires: flet-core, httpx, oauthlib, packaging, watchdog, websocket-client, websockets
Required-by:

Operating system: Windows

Additional environment details:

FeodorFitsner commented 1 year ago

What the purpose of using key property?

rulerofthehuns commented 1 year ago

It seems to be some kind of ID, but I haven't found it in the documentation. I use it to find a control with a specific key in a page.

It's not 'private' and has a setter property, so changing it should not cause this issue.

I've got the idea from here: https://docs.flutter.dev/cookbook/testing/widget/finders#2-find-a-widget-with-a-specific-key

ndonkoHenri commented 1 year ago

Here are the docs for key. Looking at your usecase, I think using data could certainly be better. Adv: it can take/store Any data type - int, str, dict... etc.

ndonkoHenri commented 1 year ago

@FeodorFitsner, there seems to be a great issue. Try this:

import flet as ft

def main(page: ft.Page):
    page.theme_mode = "light"

    page.add(
        ft.ListView(
            controls=[
                ft.TextField(
                    key=None
                ),
                ft.TextField(
                    key="2"
                ),
                ft.TextField(
                    key="3"
                )

            ]
        )
    )

ft.app(
    target=main,
    # view=ft.WEB_BROWSER
)

Try typing some text in each of the fields (the last two to be more precise) and you will notice that the focus is not only lost by these last two when typing in a character(as mentioned by @rulerofthehuns), but it is always given to the first one. That really not good. Hope you see what I mean...

FeodorFitsner commented 1 year ago

Feels like we need to rename that property to scroll_key to avoid confusion?

ndonkoHenri commented 1 year ago

Yeah, I think too. And precise in the docs that it shouldn't be used to globally identify controls.

Nevertheless @FeodorFitsner, the error I mentioned above will still exist!

fccoelho commented 4 months ago

Wow! I lost hours yesterday chasing the mysterious reason for this loss of focus on text fields!!! thanks to this issue I now fixed my problems! I am now using data attribute, which by the way is not documented.