Replies: 4 comments 1 reply
-
you are on the right track. Use the Textfield on_change to trigger a function that sorts a list, e.g. controls inside of Column.controls with whatever logic you want. Example below.
then just add |
Beta Was this translation helpful? Give feedback.
-
I'd like have that searchable dropdown in Flet as well. I did try the following code from Copilot. Still figuring out how to 'enable text typing in the ft.Dropdown object ' import flet as ft
def main(page: ft.Page):
def search(e):
# Get the search query from the TextField
search_query = e.control.value.lower()
# Filter the dropdown options based on the search query
filtered_options = [option for option in all_options if search_query in option.lower()]
# Update the dropdown options
dropdown.options = [ft.dropdown.Option(value) for value in filtered_options]
page.update()
# List of all options for the dropdown
all_options = ["english", "German", "Canadian English", "Italiano"]
# Create a TextField for entering the search text
search_field = ft.TextField(label="Search", on_change=search)
# Create a Dropdown to display the options
dropdown = ft.Dropdown(options=[ft.dropdown.Option(value) for value in all_options])
# Add the TextField and Dropdown to the page
page.add(search_field, dropdown)
ft.app(target=main) |
Beta Was this translation helpful? Give feedback.
-
Can the SearchBar not be of help? : https://flet.dev/docs/controls/searchbar |
Beta Was this translation helpful? Give feedback.
-
Searchbar does help. With the help of Copilot, I created a class to perform 'search as you type on a ft.searchbar' Here's the code (file available for download: https://github.com/clueple/pythonbeginner/blob/master/flet_searchbar_base.py): from dataclasses import dataclass, field
import flet as ft
from flet import ListView, Page, ListTile, app, SearchBar, Text
@dataclass
class SearchableDropdown:
items: list[str] = field(default_factory=list)
lv: ListView = field(default_factory=ListView)
search_bar: SearchBar = field(init=False)
def __post_init__(self):
self.search_bar = SearchBar(
bar_hint_text="Type to search...",
on_change=self.handle_change,
controls=[self.lv]
)
self.populate_list_view()
def populate_list_view(self):
for item in self.items:
self.lv.controls.append(ListTile(title=ft.Text(item), on_click=self.close_anchor, data=item))
def handle_change(self, e):
self.lv.controls.clear()
search_query = e.control.value.lower()
for item in self.items:
if search_query in item.lower():
self.lv.controls.append(ListTile(title=ft.Text(item), on_click=self.close_anchor, data=item))
self.lv.update()
def close_anchor(self, e):
selected_item = e.control.data
self.search_bar.close_view(selected_item)
self.search_bar.update()
def main(page: Page):
all_lang = ['English', 'Japanese', 'Cantonese', 'English Canadian']
diff_list = ['abc','abcdef','def', 'xyz']
searchable_dropdown = SearchableDropdown(all_lang)
new_dropdown = SearchableDropdown(diff_list)
page.add(
searchable_dropdown.search_bar,
new_dropdown.search_bar
)
if __name__ == '__main__':
app(target=main This is how I tweak the code above for app development: |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi what would be the way to do a search as you type list item selector in Flet?
I am about to re-write an ui built in streamlit as the state is getting too complex to manage with what streamlit has to offer at the moment. Flet seems great, but there are no search ahead list selectors. I have to deal with a lot long lists and dropdowns will not quite do. There is the on_change attribute in the text input widget- but ideally that will need to generate a filtered list that is clickable like the streamlit widget. Is that tricky to do? Are there plans to have that as a standard widget in flet?
Thanks in advance.
Beta Was this translation helpful? Give feedback.
All reactions