flet: SearchBar 'on_change' and 'on_submit' not working

Description

The ‘on_change’ and ‘on_submit’ functions seem to not work on the new SearchBar Control.

Code example to reproduce the issue:

import flet as ft


def main(page):
    page.theme_mode = ft.ThemeMode.LIGHT

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

    def handle_change(e):
        print("handle_change |  e.data:", e.data)
        print(f"Value: {anchor.value}")

    def handle_submit(e):
        print("handle_submit |  e.data:", e.data)
        print(f"Value: {anchor.value}")

    def handle_tap(e):
        print("handle_tap")
        print(f"Value: {anchor.value}")

    def get_value(e):
        print("get_value")
        print(f"Value: {anchor.value}")
        e.control.text = anchor.value
        page.update()

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

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


ft.app(target=main)

Describe the results you received:

Both typing into the SearchBar and pressing Enter gives no console output.

Describe the results you expected:

handle_change | e.data: *e.data*
Value: *anchor.value*
handle_submit | e.data: *e.data*
Value: *anchor.value*

in console output.

Flet version (pip show flet):

image

Operating system:

Windows 10

About this issue

  • Original URL
  • State: closed
  • Created 6 months ago
  • Comments: 20

Most upvoted comments

While waiting on the next official release, you can try the fix from the latest prerelease:

pip install flet --pre

There’s no such way, I mean, I use this lib like a 5 months, and I tried even using a automatic checker, like this code:

# Threaded function
old_value = search.value
while True:
     sleep(0.1)
     if search.value != old_value:
          old_value = search.value
          do_things_and_add_controls()

I tried this way, and I thought it should work. But he even didn’t add any of my new controls (my search query).

So I think is better wait instead of doing a go horse that isn’t working either.

thats so sad, I really want this control