Skip to content

Commit 789522c

Browse files
committed
Add reactive dropdown example using Flet
Introduces a new example demonstrating a reactive dropdown control in Flet. The example uses a dataclass to manage state and updates the selected color dynamically.
1 parent 6cb86ff commit 789522c

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from dataclasses import dataclass
2+
from typing import cast
3+
4+
import flet as ft
5+
6+
7+
@dataclass
8+
class Form:
9+
color: str = "red"
10+
11+
def change_color(self, new_color: str):
12+
print("New color:", new_color)
13+
self.color = new_color
14+
15+
16+
def main(page: ft.Page):
17+
form = Form()
18+
page.theme_mode = ft.ThemeMode.LIGHT
19+
20+
page.add(
21+
ft.SelectionArea(
22+
ft.StateView(
23+
form,
24+
lambda state: ft.Column(
25+
cast(
26+
list[ft.Control],
27+
[
28+
ft.Text(f"Selected color: {form.color}"),
29+
ft.Column(
30+
[
31+
ft.Dropdown(
32+
editable=True,
33+
label="Color",
34+
value=form.color,
35+
on_change=lambda e: form.change_color(
36+
cast(str, e.control.value)
37+
),
38+
options=[
39+
ft.DropdownOption(key="red", text="Red"),
40+
ft.DropdownOption(
41+
key="green", text="Green"
42+
),
43+
ft.DropdownOption(key="blue", text="Blue"),
44+
],
45+
),
46+
]
47+
),
48+
],
49+
)
50+
),
51+
)
52+
)
53+
)
54+
55+
56+
ft.run(main)

0 commit comments

Comments
 (0)