Loading...
Loading...
This skill should be used when the user asks to "build a Flet app", "create a Python GUI", "use Flet framework", "write a Flet control", or needs guidance on cross-platform Python UI development with Flet.
npx skill4agent add the-perfect-developer/the-perfect-opencode python-fletmainft.Pageft.run(main)import flet as ft
def main(page: ft.Page):
page.title = "My App"
page.add(ft.Text("Hello, Flet!"))
ft.run(main)pip install 'flet[all]'flet run main.pyflet run --web main.pyflet run --recursive main.pymy-app/
├── pyproject.toml
└── src/
├── assets/
│ └── icon.png
└── main.pyflet createuv run flet createft.Pagedef main(page: ft.Page):
page.title = "App Title"
page.theme_mode = ft.ThemeMode.LIGHT
page.theme = ft.Theme(color_scheme_seed=ft.Colors.BLUE)
page.vertical_alignment = ft.MainAxisAlignment.CENTER
page.horizontal_alignment = ft.CrossAxisAlignment.CENTER
page.scroll = ft.ScrollMode.AUTOupdate()page.update()control.update()text = ft.Text("Initial")
page.add(text)
def change(e):
text.value = "Changed"
text.update() # prefer: update only this control
# page.update() # fallback: updates entire pagecontrol.update()page.update()| Control | Purpose |
|---|---|
| Vertical stack |
| Horizontal stack |
| Absolute positioning |
| Box with padding, color, border, animation |
| Efficient vertical/horizontal scrollable list |
| Efficient scrollable grid |
page.add(
ft.Row(
controls=[ft.Text("Left"), ft.Text("Right")],
alignment=ft.MainAxisAlignment.SPACE_BETWEEN,
)
)expand=Trueft.ListView(expand=True, spacing=10)@ft.control
class PrimaryButton(ft.Button):
bgcolor: ft.Colors = ft.Colors.BLUE_700
color: ft.Colors = ft.Colors.WHITE@ft.control@dataclass@ft.control
class TaskItem(ft.Row):
text: str = ""
def init(self):
self.checkbox = ft.Checkbox()
self.label = ft.Text(value=self.text)
self.controls = [self.checkbox, self.label]
def toggle(self, e):
self.label.color = ft.Colors.GREY if self.checkbox.value else None
self.update()| Method | When called | Use for |
|---|---|---|
| After | Setup sub-controls |
| When assigned | Platform-dependent logic |
| After added to page | Start timers, fetch data |
| Before removed | Clean up, cancel tasks |
| Every | Sync derived state |
self.update()is_isolated = Trueis_isolatedTruepage.on_route_changepage.viewsdef main(page: ft.Page):
def route_change():
page.views.clear()
page.views.append(ft.View("/", controls=[...]))
if page.route == "/settings":
page.views.append(ft.View("/settings", controls=[...]))
page.update()
async def view_pop(e):
page.views.remove(e.view)
await page.push_route(page.views[-1].route)
page.on_route_change = route_change
page.on_view_pop = view_pop
route_change()
ft.run(main)await page.push_route("/settings")troute = ft.TemplateRoute(page.route)
if troute.match("/items/:id"):
print(troute.id)/page.viewspage.on_route_changepage.on_view_pop# App-wide theme
page.theme = ft.Theme(color_scheme_seed=ft.Colors.GREEN)
page.dark_theme = ft.Theme(color_scheme_seed=ft.Colors.BLUE)
page.theme_mode = ft.ThemeMode.SYSTEM # LIGHT | DARK | SYSTEM
# Nested theme (scoped to a container)
ft.Container(
theme=ft.Theme(color_scheme=ft.ColorScheme(primary=ft.Colors.PINK)),
content=ft.Button("Pink button"),
)mainasyncioasyncio.sleep()time.sleep()import asyncio
import flet as ft
async def main(page: ft.Page):
async def on_click(e):
await asyncio.sleep(1)
page.add(ft.Text("Done!"))
page.add(ft.Button("Click me", on_click=on_click))
ft.run(main)page.run_task(coro)did_mount()ListViewGridViewColumnRow# Efficient: renders only visible items
lv = ft.ListView(expand=True, spacing=10, item_extent=50)
for i in range(5000):
lv.controls.append(ft.Text(f"Line {i}"))
page.add(lv)
# Batch updates to avoid large WebSocket messages
for i in range(5000):
lv.controls.append(ft.Text(f"Line {i}"))
if i % 500 == 0:
page.update()
page.update()animate_*# Opacity fade
container = ft.Container(
width=150, height=150,
bgcolor=ft.Colors.BLUE,
animate_opacity=300, # ms
)
# Scale with bounce curve
ft.Container(
animate_scale=ft.Animation(
duration=600,
curve=ft.AnimationCurve.BOUNCE_OUT,
)
)
# Position animation (inside Stack or page.overlay)
ft.Container(animate_position=1000)
# Animated content switcher
ft.AnimatedSwitcher(
content,
transition=ft.AnimatedSwitcherTransition.SCALE,
duration=500,
)page.title = "My App"
page.window.width = 800
page.window.height = 600
page.window.resizable = True
page.window.always_on_top = False
page.window.center()ft.Text("Hello", size=20, weight=ft.FontWeight.BOLD)
ft.Button("Click", on_click=handler)
ft.IconButton(ft.Icons.ADD, on_click=handler)
ft.TextField(label="Name", on_change=handler)
ft.Checkbox(label="Check me", on_change=handler)
ft.Dropdown(options=[ft.dropdown.Option("A"), ft.dropdown.Option("B")])
ft.Image(src="photo.jpg") # from assets/
ft.Image(src="https://example.com/img.png") # remote
ft.AppBar(title=ft.Text("Title"))
ft.NavigationDrawer(controls=[...])
ft.AlertDialog(title=ft.Text("Alert"), content=ft.Text("Body"))references/controls-and-layout.mdreferences/patterns-and-best-practices.mdexamples/counter-app.pyexamples/custom-control.py