@@ -76,15 +76,32 @@ def prettify(token: str) -> str:
7676
7777
7878def main (page : ft .Page ):
79- page .title = "Examples POC "
79+ page .title = "Flet Examples "
8080 page .padding = 20
8181 page .theme_mode = ft .ThemeMode .LIGHT
8282 page .horizontal_alignment = ft .CrossAxisAlignment .START
8383 page .scroll = ft .ScrollMode .AUTO
84+ search_ref : ft .Ref [ft .TextField ] = ft .Ref ()
8485
8586 def open_example (slug : str ):
8687 page .go (f"/{ slug } " )
8788
89+ def filter_controls (query : str ) -> dict [str , list [dict [str , object ]]]:
90+ q = query .strip ().lower ()
91+ if not q :
92+ return CONTROL_EXAMPLES
93+ filtered : dict [str , list [dict [str , object ]]] = {}
94+ for control_name , examples in CONTROL_EXAMPLES .items ():
95+ matches = []
96+ for ex in examples :
97+ slug_match = q in ex ["slug" ].lower ()
98+ title_match = q in pretty_example_title (ex ["slug" ]).lower ()
99+ if slug_match or title_match or q in control_name .lower ():
100+ matches .append (ex )
101+ if matches :
102+ filtered [control_name ] = matches
103+ return filtered
104+
88105 def render_home ():
89106 page .appbar = ft .AppBar (
90107 title = ft .Text ("Flet examples" , weight = ft .FontWeight .W_600 ),
@@ -94,67 +111,89 @@ def render_home():
94111 )
95112 page .clean ()
96113
97- control_cards = []
98- for control_name , examples in CONTROL_EXAMPLES .items ():
99- control_cards .append (
100- ft .Card (
101- elevation = 2 ,
102- content = ft .Container (
103- padding = 12 ,
104- content = ft .Column (
105- spacing = 8 ,
106- controls = [
107- ft .Text (
108- value = control_name ,
109- theme_style = ft .TextThemeStyle .TITLE_LARGE ,
110- weight = ft .FontWeight .W_600 ,
111- ),
112- ft .ListView (
113- spacing = 4 ,
114- expand = True ,
115- scroll = ft .ScrollMode .AUTO ,
116- controls = [
117- ft .ListTile (
118- title = ft .Text (
119- value = pretty_example_title (ex ["slug" ]),
120- weight = ft .FontWeight .W_600 ,
121- ),
122- subtitle = ft .Text (f"/{ ex ['slug' ]} " ),
123- on_click = lambda e ,
124- s = ex ["slug" ]: open_example (s ),
125- trailing = ft .Icon (ft .Icons .CHEVRON_RIGHT ),
126- )
127- for ex in examples
128- ],
129- ),
130- ],
114+ grid_view = ft .GridView (
115+ expand = 1 ,
116+ runs_count = 0 ,
117+ max_extent = 420 ,
118+ spacing = 12 ,
119+ run_spacing = 12 ,
120+ )
121+
122+ def update_controls ():
123+ query = search_ref .current .value if search_ref .current else ""
124+ visible_controls = filter_controls (query or "" )
125+
126+ control_cards = []
127+ for control_name , examples in visible_controls .items ():
128+ control_cards .append (
129+ ft .Card (
130+ elevation = 2 ,
131+ content = ft .Container (
132+ padding = 12 ,
133+ content = ft .Column (
134+ spacing = 8 ,
135+ controls = [
136+ ft .Text (
137+ value = control_name ,
138+ theme_style = ft .TextThemeStyle .TITLE_LARGE ,
139+ weight = ft .FontWeight .W_600 ,
140+ ),
141+ ft .ListView (
142+ spacing = 4 ,
143+ expand = True ,
144+ scroll = ft .ScrollMode .AUTO ,
145+ controls = [
146+ ft .ListTile (
147+ title = ft .Text (
148+ value = pretty_example_title (
149+ ex ["slug" ]
150+ ),
151+ weight = ft .FontWeight .W_600 ,
152+ ),
153+ subtitle = ft .Text (f"/{ ex ['slug' ]} " ),
154+ on_click = lambda e ,
155+ s = ex ["slug" ]: open_example (s ),
156+ trailing = ft .Icon (
157+ ft .Icons .CHEVRON_RIGHT
158+ ),
159+ )
160+ for ex in examples
161+ ],
162+ ),
163+ ],
164+ ),
131165 ),
132- ),
166+ )
133167 )
134- )
168+
169+ grid_view .controls = control_cards
170+ grid_view .update ()
135171
136172 page .add (
173+ ft .TextField (
174+ ref = search_ref ,
175+ prefix_icon = ft .Icons .SEARCH ,
176+ hint_text = "Search controls or examples" ,
177+ on_change = lambda e : update_controls (),
178+ dense = True ,
179+ ),
180+ ft .Divider (),
137181 ft .Text (
138182 "Open examples via tile click or route (e.g. /checkbox/basic)." ,
139183 theme_style = ft .TextThemeStyle .BODY_MEDIUM ,
140184 ),
141185 ft .Divider (),
142- ft .GridView (
143- expand = 1 ,
144- runs_count = 0 ,
145- max_extent = 420 ,
146- spacing = 12 ,
147- run_spacing = 12 ,
148- controls = control_cards ,
149- ),
186+ grid_view ,
150187 )
188+ update_controls ()
151189
152- def prepare_page ():
190+ def reset_page ():
153191 page .appbar = None
154192 page .clean ()
155193 page .overlay .clear ()
156194 page .pop_dialog ()
157195 page .theme = page .dark_theme = page .floating_action_button = None
196+ page .theme_mode = ft .ThemeMode .SYSTEM
158197 page .update ()
159198
160199 def render_example (slug : str ):
@@ -164,7 +203,7 @@ def render_example(slug: str):
164203 return
165204
166205 # Show only the example content.
167- prepare_page ()
206+ reset_page ()
168207 info ["runner" ](page )
169208 page .update ()
170209
0 commit comments