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
10.04k stars 393 forks source link

Unable to focus the text field of a SearchBar programatically #3417

Open chaotic-dev opened 1 month ago

chaotic-dev commented 1 month ago

Other components like the TextField have a focus method. The SearchBar should have one as well seeing as it has the autofocus property already.

Discussed in https://github.com/flet-dev/flet/discussions/3350

Originally posted by **chaotic-dev** May 24, 2024 ### Question I have a `SearchBar` with a `ListView` populated with search suggestions as its control. When I click one of the `ListItem`s, it modifies the `SearchBar`'s `value` with the search suggestion. After doing so, the `SearchBar`'s input loses focus and I'd like it to remain focused for the user to continue typing. Is it possible to focus the `SearchBar`'s input or otherwise get its underlying `TextField` which has a focus method? I've attempted to set `autofocus` to `True` and call its `open_view` method to try and grab focus, but neither option seem to focus the input. Thanks in advance ### Code sample ```python # Stripped-down example search_bar = ft.SearchBar( controls=[ ft.ListTile(title=ft.Text("suggestion"), on_click=apply_suggestion, data="suggestion") ], on_submit=lambda _: page.go(f"/search/{search_bar.value}"), on_change=update_suggestions, autofocus=True, ) def apply_suggestion(e): search_bar.value = e.control.data search_bar.update() search_bar.open_view() ``` Update: OS information: Pop OS Flet package version: 0.22.1 ### Error message _No response_ ### ------------------------------------------------------ - [X] I have searched for answers to my question both in the issues and in previous discussions.
chaotic-dev commented 1 month ago

I've been able to build the flet bundle for web/Linux and a presumed fix to this issue but I am unable to get them to run for tests due to the following error repeated with multiple variations. If anyone knows how to fix this or is willing to test that my changes work, that would be appreciated

** (flet:303499): CRITICAL **:  ... : assertion 'FL_IS_*' failed
ndonkoHenri commented 4 weeks ago

Can you share your code and the steps to repro?

chaotic-dev commented 4 weeks ago

Can you share your code and the steps to repro?

Code with changes is here but I get the same issue with the main branch

I attempted to follow the steps from CONTRIBUTING.md

Flutter installed:

[✓] Flutter (Channel stable, 3.22.2, on Pop!_OS 22.04 LTS
    6.8.0-76060800daily20240311-generic, locale en_US.UTF-8)
[✗] Android toolchain - develop for Android devices
    ✗ Unable to locate Android SDK.
      Install Android Studio from:
      https://developer.android.com/studio/index.html
      On first launch it will assist you in installing the Android SDK
      components.
      (or visit https://flutter.dev/docs/get-started/install/linux#android-setup
      for detailed instructions).
      If the Android SDK has been installed to a custom location, please use
      `flutter config --android-sdk` to update to that location.

[✓] Chrome - develop for the web
[✓] Linux toolchain - develop for Linux desktop
[!] Android Studio (not installed)
[✓] VS Code (version 1.89.1)
[✓] Connected device (2 available)
[✓] Network resources

Cloned the main branch and set the variables, built the project successfully for linux and web

export FLET_VIEW_PATH=/$PATH_TO_FLET/client/build/linux/x64/release/bundle
export FLET_WEB_PATH=/$PATH_TO_FLETt/client/build/web
cd $PATH_TO_FLET/client
flutter build linux
flutter build web

Installed poetry, initialized, and ran test

curl -sSL https://install.python-poetry.org | python3 -
cd $PATH_TO_FLET/sdk/python
poetry install
# Doesn't display properly
poetry run flet run -w -p 8550 playground/hello.py
cd $PATH_TO_FLET/client
# Leads to errors mentioned previously
flutter run 
Connected devices:
Linux (desktop) • linux  • linux-x64      • Pop!_OS 22.04 LTS
6.8.0-76060800daily20240311-generic
Chrome (web)    • chrome • web-javascript • Chromium 125.0.6422.141
[1]: Linux (linux)
[2]: Chrome (chrome)
Please choose one (or "q" to quit): 1

Launching lib/main.dart on Linux in debug mode...
Building Linux application...                                           
✓ Built build/linux/x64/debug/bundle/flet

** (flet:408553): CRITICAL **: 14:54:03.738: FlBinaryMessenger *fl_engine_get_binary_messenger(FlEngine *): assertion 'FL_IS_ENGINE(self)' failed

** (flet:408553): CRITICAL **: 14:54:03.738: FlTextureRegistrar *fl_engine_get_texture_registrar(FlEngine *): assertion 'FL_IS_ENGINE(self)' failed

** (flet:408553): CRITICAL **: 14:54:03.738: FlPluginRegistrar *fl_plugin_registrar_new(FlView *, FlBinaryMessenger *, FlTextureRegistrar *): assertion 'FL_IS_BINARY_MESSENGER(messenger)' failed

** (flet:408553): CRITICAL **: 14:54:03.738: FlBinaryMessenger *fl_plugin_registrar_get_messenger(FlPluginRegistrar *): assertion 'FL_IS_PLUGIN_REGISTRAR(self)' failed

** (flet:408553): CRITICAL **: 14:54:03.738: FlMethodChannel *fl_method_channel_new(FlBinaryMessenger *, const gchar *, FlMethodCodec *): assertion 'FL_IS_BINARY_MESSENGER(messenger)' failed

** (flet:408553): CRITICAL **: 14:54:03.738: FlMethodChannel *fl_method_channel_new(FlBinaryMessenger *, const gchar *, FlMethodCodec *): assertion 'FL_IS_BINARY_MESSENGER(messenger)' failed

** (flet:408553): CRITICAL **: 14:54:03.738: FlEventChannel *fl_event_channel_new(FlBinaryMessenger *, const gchar *, FlMethodCodec *): assertion 'FL_IS_BINARY_MESSENGER(messenger)' failed

** (flet:408553): CRITICAL **: 14:54:03.738: void fl_method_channel_set_method_call_handler(FlMethodChannel *, FlMethodChannelMethodCallHandler, gpointer, GDestroyNotify): assertion 'FL_IS_METHOD_CHANNEL(self)' failed

** (flet:408553): CRITICAL **: 14:54:03.738: void fl_method_channel_set_method_call_handler(FlMethodChannel *, FlMethodChannelMethodCallHandler, gpointer, GDestroyNotify): assertion 'FL_IS_METHOD_CHANNEL(self)' failed

** (flet:408553): CRITICAL **: 14:54:03.738: FlBinaryMessenger *fl_engine_get_binary_messenger(FlEngine *): assertion 'FL_IS_ENGINE(self)' failed

** (flet:408553): CRITICAL **: 14:54:03.738: FlTextureRegistrar *fl_engine_get_texture_registrar(FlEngine *): assertion 'FL_IS_ENGINE(self)' failed

** (flet:408553): CRITICAL **: 14:54:03.738: FlPluginRegistrar *fl_plugin_registrar_new(FlView *, FlBinaryMessenger *, FlTextureRegistrar *): assertion 'FL_IS_BINARY_MESSENGER(messenger)' failed

** (flet:408553): CRITICAL **: 14:54:03.738: FlBinaryMessenger *fl_engine_get_binary_messenger(FlEngine *): assertion 'FL_IS_ENGINE(self)' failed

** (flet:408553): CRITICAL **: 14:54:03.738: FlTextureRegistrar *fl_engine_get_texture_registrar(FlEngine *): assertion 'FL_IS_ENGINE(self)' failed

** (flet:408553): CRITICAL **: 14:54:03.738: FlPluginRegistrar *fl_plugin_registrar_new(FlView *, FlBinaryMessenger *, FlTextureRegistrar *): assertion 'FL_IS_BINARY_MESSENGER(messenger)' failed

** (flet:408553): CRITICAL **: 14:54:03.738: FlBinaryMessenger *fl_plugin_registrar_get_messenger(FlPluginRegistrar *): assertion 'FL_IS_PLUGIN_REGISTRAR(self)' failed

** (flet:408553): CRITICAL **: 14:54:03.738: FlMethodChannel *fl_method_channel_new(FlBinaryMessenger *, const gchar *, FlMethodCodec *): assertion 'FL_IS_BINARY_MESSENGER(messenger)' failed

** (flet:408553): CRITICAL **: 14:54:03.738: void fl_method_channel_set_method_call_handler(FlMethodChannel *, FlMethodChannelMethodCallHandler, gpointer, GDestroyNotify): assertion 'FL_IS_METHOD_CHANNEL(self)' failed

** (flet:408553): CRITICAL **: 14:54:03.738: FlTextureRegistrar *fl_plugin_registrar_get_texture_registrar(FlPluginRegistrar *): assertion 'FL_IS_PLUGIN_REGISTRAR(self)' failed

** (flet:408553): CRITICAL **: 14:54:03.738: FlView *fl_plugin_registrar_get_view(FlPluginRegistrar *): assertion 'FL_IS_PLUGIN_REGISTRAR(self)' failed

** (flet:408553): CRITICAL **: 14:54:03.738: FlBinaryMessenger *fl_engine_get_binary_messenger(FlEngine *): assertion 'FL_IS_ENGINE(self)' failed

** (flet:408553): CRITICAL **: 14:54:03.738: FlTextureRegistrar *fl_engine_get_texture_registrar(FlEngine *): assertion 'FL_IS_ENGINE(self)' failed

** (flet:408553): CRITICAL **: 14:54:03.738: FlPluginRegistrar *fl_plugin_registrar_new(FlView *, FlBinaryMessenger *, FlTextureRegistrar *): assertion 'FL_IS_BINARY_MESSENGER(messenger)' failed

** (flet:408553): CRITICAL **: 14:54:03.738: FlBinaryMessenger *fl_engine_get_binary_messenger(FlEngine *): assertion 'FL_IS_ENGINE(self)' failed

** (flet:408553): CRITICAL **: 14:54:03.738: FlTextureRegistrar *fl_engine_get_texture_registrar(FlEngine *): assertion 'FL_IS_ENGINE(self)' failed

** (flet:408553): CRITICAL **: 14:54:03.738: FlPluginRegistrar *fl_plugin_registrar_new(FlView *, FlBinaryMessenger *, FlTextureRegistrar *): assertion 'FL_IS_BINARY_MESSENGER(messenger)' failed

(flet:408553): GLib-GObject-CRITICAL **: 14:54:03.738: g_object_ref: assertion 'G_IS_OBJECT (object)' failed

** (flet:408553): CRITICAL **: 14:54:03.738: FlBinaryMessenger *fl_plugin_registrar_get_messenger(FlPluginRegistrar *): assertion 'FL_IS_PLUGIN_REGISTRAR(self)' failed

** (flet:408553): CRITICAL **: 14:54:03.738: FlMethodChannel *fl_method_channel_new(FlBinaryMessenger *, const gchar *, FlMethodCodec *): assertion 'FL_IS_BINARY_MESSENGER(messenger)' failed

** (flet:408553): CRITICAL **: 14:54:03.739: void fl_method_channel_set_method_call_handler(FlMethodChannel *, FlMethodChannelMethodCallHandler, gpointer, GDestroyNotify): assertion 'FL_IS_METHOD_CHANNEL(self)' failed

** (flet:408553): CRITICAL **: 14:54:03.739: FlBinaryMessenger *fl_engine_get_binary_messenger(FlEngine *): assertion 'FL_IS_ENGINE(self)' failed

** (flet:408553): CRITICAL **: 14:54:03.739: FlTextureRegistrar *fl_engine_get_texture_registrar(FlEngine *): assertion 'FL_IS_ENGINE(self)' failed

** (flet:408553): CRITICAL **: 14:54:03.739: FlPluginRegistrar *fl_plugin_registrar_new(FlView *, FlBinaryMessenger *, FlTextureRegistrar *): assertion 'FL_IS_BINARY_MESSENGER(messenger)' failed

(flet:408553): GLib-GObject-CRITICAL **: 14:54:03.739: g_object_ref: assertion 'G_IS_OBJECT (object)' failed

** (flet:408553): CRITICAL **: 14:54:03.739: FlBinaryMessenger *fl_plugin_registrar_get_messenger(FlPluginRegistrar *): assertion 'FL_IS_PLUGIN_REGISTRAR(self)' failed

** (flet:408553): CRITICAL **: 14:54:03.739: FlMethodChannel *fl_method_channel_new(FlBinaryMessenger *, const gchar *, FlMethodCodec *): assertion 'FL_IS_BINARY_MESSENGER(messenger)' failed

** (flet:408553): CRITICAL **: 14:54:03.739: void fl_method_channel_set_method_call_handler(FlMethodChannel *, FlMethodChannelMethodCallHandler, gpointer, GDestroyNotify): assertion 'FL_IS_METHOD_CHANNEL(self)' failed

** (flet:408553): CRITICAL **: 14:54:03.739: FlBinaryMessenger *fl_engine_get_binary_messenger(FlEngine *): assertion 'FL_IS_ENGINE(self)' failed

** (flet:408553): CRITICAL **: 14:54:03.739: FlTextureRegistrar *fl_engine_get_texture_registrar(FlEngine *): assertion 'FL_IS_ENGINE(self)' failed

** (flet:408553): CRITICAL **: 14:54:03.739: FlPluginRegistrar *fl_plugin_registrar_new(FlView *, FlBinaryMessenger *, FlTextureRegistrar *): assertion 'FL_IS_BINARY_MESSENGER(messenger)' failed

(flet:408553): GLib-GObject-CRITICAL **: 14:54:03.739: g_object_ref: assertion 'G_IS_OBJECT (object)' failed

** (flet:408553): CRITICAL **: 14:54:03.739: FlView *fl_plugin_registrar_get_view(FlPluginRegistrar *): assertion 'FL_IS_PLUGIN_REGISTRAR(self)' failed

** (flet:408553): CRITICAL **: 14:54:03.739: FlView *fl_plugin_registrar_get_view(FlPluginRegistrar *): assertion 'FL_IS_PLUGIN_REGISTRAR(self)' failed

(flet:408553): GLib-GObject-WARNING **: 14:54:03.739: invalid (NULL) pointer instance

(flet:408553): GLib-GObject-CRITICAL **: 14:54:03.739: g_signal_handler_find: assertion 'G_TYPE_CHECK_INSTANCE (instance)' failed

** (flet:408553): CRITICAL **: 14:54:03.739: FlView *fl_plugin_registrar_get_view(FlPluginRegistrar *): assertion 'FL_IS_PLUGIN_REGISTRAR(self)' failed

(flet:408553): GLib-GObject-WARNING **: 14:54:03.739: invalid (NULL) pointer instance

(flet:408553): GLib-GObject-CRITICAL **: 14:54:03.739: g_signal_connect_data: assertion 'G_TYPE_CHECK_INSTANCE (instance)' failed

** (flet:408553): CRITICAL **: 14:54:03.739: FlView *fl_plugin_registrar_get_view(FlPluginRegistrar *): assertion 'FL_IS_PLUGIN_REGISTRAR(self)' failed

(flet:408553): GLib-GObject-WARNING **: 14:54:03.739: invalid (NULL) pointer instance

(flet:408553): GLib-GObject-CRITICAL **: 14:54:03.739: g_signal_connect_data: assertion 'G_TYPE_CHECK_INSTANCE (instance)' failed

** (flet:408553): CRITICAL **: 14:54:03.739: FlView *fl_plugin_registrar_get_view(FlPluginRegistrar *): assertion 'FL_IS_PLUGIN_REGISTRAR(self)' failed

(flet:408553): GLib-GObject-WARNING **: 14:54:03.739: invalid (NULL) pointer instance

(flet:408553): GLib-GObject-CRITICAL **: 14:54:03.739: g_signal_connect_data: assertion 'G_TYPE_CHECK_INSTANCE (instance)' failed

** (flet:408553): CRITICAL **: 14:54:03.739: FlView *fl_plugin_registrar_get_view(FlPluginRegistrar *): assertion 'FL_IS_PLUGIN_REGISTRAR(self)' failed

(flet:408553): GLib-GObject-WARNING **: 14:54:03.739: invalid (NULL) pointer instance

(flet:408553): GLib-GObject-CRITICAL **: 14:54:03.739: g_signal_connect_data: assertion 'G_TYPE_CHECK_INSTANCE (instance)' failed

** (flet:408553): CRITICAL **: 14:54:03.739: FlView *fl_plugin_registrar_get_view(FlPluginRegistrar *): assertion 'FL_IS_PLUGIN_REGISTRAR(self)' failed

(flet:408553): GLib-GObject-WARNING **: 14:54:03.739: invalid (NULL) pointer instance

(flet:408553): GLib-GObject-CRITICAL **: 14:54:03.739: g_signal_connect_data: assertion 'G_TYPE_CHECK_INSTANCE (instance)' failed

** (flet:408553): CRITICAL **: 14:54:03.739: FlView *fl_plugin_registrar_get_view(FlPluginRegistrar *): assertion 'FL_IS_PLUGIN_REGISTRAR(self)' failed

(flet:408553): GLib-GObject-WARNING **: 14:54:03.739: invalid (NULL) pointer instance

(flet:408553): GLib-GObject-CRITICAL **: 14:54:03.739: g_signal_connect_data: assertion 'G_TYPE_CHECK_INSTANCE (instance)' failed

** (flet:408553): CRITICAL **: 14:54:03.739: FlView *fl_plugin_registrar_get_view(FlPluginRegistrar *): assertion 'FL_IS_PLUGIN_REGISTRAR(self)' failed

(flet:408553): GLib-GObject-WARNING **: 14:54:03.739: invalid (NULL) pointer instance

(flet:408553): GLib-GObject-CRITICAL **: 14:54:03.739: g_signal_connect_data: assertion 'G_TYPE_CHECK_INSTANCE (instance)' failed

** (flet:408553): CRITICAL **: 14:54:03.739: FlView *fl_plugin_registrar_get_view(FlPluginRegistrar *): assertion 'FL_IS_PLUGIN_REGISTRAR(self)' failed

(flet:408553): GLib-GObject-WARNING **: 14:54:03.739: invalid (NULL) pointer instance

(flet:408553): GLib-GObject-CRITICAL **: 14:54:03.739: g_signal_connect_data: assertion 'G_TYPE_CHECK_INSTANCE (instance)' failed

** (flet:408553): CRITICAL **: 14:54:03.739: FlView *fl_plugin_registrar_get_view(FlPluginRegistrar *): assertion 'FL_IS_PLUGIN_REGISTRAR(self)' failed

(flet:408553): GLib-GObject-WARNING **: 14:54:03.739: invalid (NULL) pointer instance

(flet:408553): GLib-GObject-CRITICAL **: 14:54:03.739: g_signal_connect_data: assertion 'G_TYPE_CHECK_INSTANCE (instance)' failed

** (flet:408553): CRITICAL **: 14:54:03.739: FlView *fl_plugin_registrar_get_view(FlPluginRegistrar *): assertion 'FL_IS_PLUGIN_REGISTRAR(self)' failed

(flet:408553): Gtk-CRITICAL **: 14:54:03.739: gtk_container_forall: assertion 'GTK_IS_CONTAINER (container)' failed

** (flet:408553): CRITICAL **: 14:54:03.739: FlBinaryMessenger *fl_plugin_registrar_get_messenger(FlPluginRegistrar *): assertion 'FL_IS_PLUGIN_REGISTRAR(self)' failed

** (flet:408553): CRITICAL **: 14:54:03.739: FlMethodChannel *fl_method_channel_new(FlBinaryMessenger *, const gchar *, FlMethodCodec *): assertion 'FL_IS_BINARY_MESSENGER(messenger)' failed

** (flet:408553): CRITICAL **: 14:54:03.739: void fl_method_channel_set_method_call_handler(FlMethodChannel *, FlMethodChannelMethodCallHandler, gpointer, GDestroyNotify): assertion 'FL_IS_METHOD_CHANNEL(self)' failed

** (flet:408553): CRITICAL **: 14:54:03.739: FlBinaryMessenger *fl_engine_get_binary_messenger(FlEngine *): assertion 'FL_IS_ENGINE(self)' failed

** (flet:408553): CRITICAL **: 14:54:03.739: FlTextureRegistrar *fl_engine_get_texture_registrar(FlEngine *): assertion 'FL_IS_ENGINE(self)' failed

** (flet:408553): CRITICAL **: 14:54:03.739: FlPluginRegistrar *fl_plugin_registrar_new(FlView *, FlBinaryMessenger *, FlTextureRegistrar *): assertion 'FL_IS_BINARY_MESSENGER(messenger)' failed

(flet:408553): GLib-GObject-CRITICAL **: 14:54:03.739: g_object_ref: assertion 'G_IS_OBJECT (object)' failed

** (flet:408553): CRITICAL **: 14:54:03.739: FlBinaryMessenger *fl_plugin_registrar_get_messenger(FlPluginRegistrar *): assertion 'FL_IS_PLUGIN_REGISTRAR(self)' failed

** (flet:408553): CRITICAL **: 14:54:03.739: FlMethodChannel *fl_method_channel_new(FlBinaryMessenger *, const gchar *, FlMethodCodec *): assertion 'FL_IS_BINARY_MESSENGER(messenger)' failed

** (flet:408553): CRITICAL **: 14:54:03.739: void fl_method_channel_set_method_call_handler(FlMethodChannel *, FlMethodChannelMethodCallHandler, gpointer, GDestroyNotify): assertion 'FL_IS_METHOD_CHANNEL(self)' failed
package:media_kit_libs_linux registered.

Test files:

hello.py

import flet
from flet import Page, Text

def main(page: Page):
    page.add(Text("Hello, world!"))

flet.app(target=main)

test.py

import flet as ft

def main(page: ft.Page):
    page.horizontal_alignment = ft.CrossAxisAlignment.CENTER

    def close_anchor(e):
        text = f"Color {e.control.data}"
        anchor.value = text
        anchor.update()
        # print(f"closing view from {text}")
        # anchor.close_view(text)

    anchor = ft.SearchBar(
        view_elevation=4,
        divider_color=ft.colors.AMBER,
        bar_hint_text="Search colors...",
        view_hint_text="Choose a color from the suggestions...",
        controls=[
            ft.ListTile(title=ft.Text(f"Color {i}"), on_click=close_anchor, data=i)
            for i in range(10)
        ],
    )

    page.add(
        ft.Row(
            alignment=ft.MainAxisAlignment.CENTER,
            controls=[
                ft.OutlinedButton(
                    "Open Search View",
                    on_click=lambda _: anchor.open_view(),
                ),
            ],
        ),
        anchor,
    )

ft.app(main)