### Install dependencies Source: https://github.com/flet-dev/website/blob/main/README.md Run this command to install all required project dependencies. ```bash $ yarn ``` -------------------------------- ### Install Full Flet Desktop Client Source: https://github.com/flet-dev/website/blob/main/blog/2024-11-27-flet-v-0-25-release-announcement.md Use this command to install the full Flet desktop client, including Audio and Video controls. ```bash pip install flet-desktop ``` -------------------------------- ### Start local development server Source: https://github.com/flet-dev/website/blob/main/README.md Launches a local server with live reloading for development purposes. ```bash $ yarn start ``` -------------------------------- ### Install Flet pre-release Source: https://github.com/flet-dev/website/blob/main/blog/2022-11-16-flet-versioning-and-pre-releases.md Use the --pre flag with pip to install the latest developmental build of Flet. ```bash pip install flet --pre ``` -------------------------------- ### Install Flet v1 Alpha with uv Source: https://github.com/flet-dev/website/blob/main/blog/2025-06-26-introducing-flet-1-0-alpha.md Install the Flet v1 Alpha pre-release using uv. Ensure to allow pre-releases. ```bash uv add 'flet[all]>=0.70.0.dev0' --prerelease=allow ``` -------------------------------- ### Manage Android emulators Source: https://github.com/flet-dev/website/blob/main/blog/2025-12-02-flet-debug-the-new-cli-for-testing-flet-apps-on-mobile-devices.md Commands for listing, creating, and starting Android emulators. ```bash flet emulators ``` ```bash flet emulators create my-emulator ``` ```bash flet emulators start my-emulator ``` -------------------------------- ### Install Flet pre-release Source: https://github.com/flet-dev/website/blob/main/blog/2024-10-10-flet-new-packaging-pre-release.md Use this command to install the Flet pre-release version. It is recommended to use a dedicated virtual environment. ```bash pip install flet==0.25.0.dev3519 ``` -------------------------------- ### FilePicker with Data Example Source: https://github.com/flet-dev/website/blob/main/blog/2026-02-24-flet-v-0-81-release-announcement.md Example of using FilePicker to pick files and retrieve their content as bytes. Requires Flet library. ```python import flet as ft def main(page: ft.Page): async def pick_text_file(_): files = await ft.FilePicker().pick_files(with_data=True) print(files[0].bytes if files else None) page.add(ft.Button("Pick file", on_click=pick_text_file)) ft.run(main) ``` -------------------------------- ### Install Flet Source: https://github.com/flet-dev/website/blob/main/blog/2024-11-27-flet-v-0-25-release-announcement.md Use this command to install the development version of the Flet package with all dependencies. ```bash pip install 'flet[all]' ``` -------------------------------- ### Implement Camera Control Source: https://github.com/flet-dev/website/blob/main/blog/2026-02-24-flet-v-0-81-release-announcement.md This snippet demonstrates how to add a Camera control for live preview. Ensure `flet_camera` is installed. ```python import flet as ft import flet_camera as fc def main(page: ft.Page): page.add( fc.Camera( expand=True, preview_enabled=True, ) ) ft.run(main) ``` -------------------------------- ### Run the Flet FastAPI application Source: https://github.com/flet-dev/website/blob/main/blog/2023-08-30-flet-for-fastapi.md Command to start the application using the Uvicorn ASGI server. ```bash uvicorn hello:app ``` -------------------------------- ### Install libmpv for Video Control on Linux Source: https://github.com/flet-dev/website/blob/main/blog/2024-02-14-flet-adaptive-and-custom-controls.md To use the Video control on Linux, ensure the libmpv package is installed. This command is for Ubuntu/Debian-based systems. ```bash sudo apt install libmpv-dev mpv ``` -------------------------------- ### Size-Aware Layout Example Source: https://github.com/flet-dev/website/blob/main/blog/2026-02-24-flet-v-0-81-release-announcement.md Demonstrates creating a size-aware container that updates its text content based on its dimensions. Requires Flet library. ```python import flet as ft def main(page: ft.Page): def handle_size_change(e: ft.LayoutSizeChangeEvent[ft.Container]): e.control.content.value = f"{int(e.width)} x {int(e.height)}" page.add( ft.Container( expand=True, content=ft.Text(), on_size_change=handle_size_change, ) ) ft.run(main) ``` -------------------------------- ### Event handler subscription example Source: https://github.com/flet-dev/website/blob/main/blog/2024-08-31-flet-v-0-24-release-announcement.md Demonstrates the legacy behavior of subscribing multiple callbacks to a single event handler. ```python import flet as ft def main(page: ft.Page): def print_one(e): print("1") def print_two(e): print("2") def print_three(e): print("3") c = ft.Container( bgcolor=ft.Colors.random_color(), width=300, height=300, ) # subscribe callbacks c.on_tap_down = print_one c.on_tap_down = print_two c.on_tap_down = print_three page.add(c) ft.run(main) ``` -------------------------------- ### Install Flet pre-release Source: https://github.com/flet-dev/website/blob/main/blog/2024-10-15-pyproject-toml-support-for-flet-build-command.md Use this command to install the specific pre-release version of Flet required for pyproject.toml support. ```bash pip install flet==0.25.0.dev3526 ``` -------------------------------- ### Install Flet v1 Alpha with pip Source: https://github.com/flet-dev/website/blob/main/blog/2025-06-26-introducing-flet-1-0-alpha.md Install the Flet v1 Alpha pre-release using pip. It is recommended to use a new virtual environment. ```bash pip install --pre 'flet[all]>=0.70.0.dev0' ``` -------------------------------- ### Install Flet Source: https://github.com/flet-dev/website/blob/main/blog/2023-02-06-standalone-flet-web-apps-with-pyodide.md Install the latest Flet package using pip. This is a prerequisite for using Flet and its Pyodide features. ```bash pip install flet --upgrade ``` -------------------------------- ### Custom Tooltip Example Source: https://github.com/flet-dev/website/blob/main/blog/2022-11-13-responsive-row-and-mobile-controls.md Demonstrates how to implement a custom tooltip for UI elements. Refer to the Flet documentation for detailed usage. ```python import flet as ft def main(page: ft.Page): page.title = "Tooltip Example" page.vertical_alignment = ft.MainAxisAlignment.CENTER page.add( ft.Tooltip( message="This is a custom tooltip!", child=ft.ElevatedButton("Hover me"), ) ) ft.app(target=main) ``` -------------------------------- ### Example: Video Control Implementation in Dart Source: https://github.com/flet-dev/website/blob/main/blog/2024-02-14-flet-adaptive-and-custom-controls.md This is an example of the `createControl` function implementation for the Video control in Dart, demonstrating how to create a Flutter widget for a Flet control. ```dart Widget createControl(CreateControlArgs args) { // ... implementation details ... return Video(args=args); } ``` -------------------------------- ### Matrix4 Transforms and RotatedBox Example Source: https://github.com/flet-dev/website/blob/main/blog/2026-02-24-flet-v-0-81-release-announcement.md Demonstrates using Matrix4 for advanced transforms and RotatedBox for layout-aware rotation. Requires Flet library. ```python import flet as ft from math import pi def main(page: ft.Page): page.add( ft.Container( width=220, height=130, bgcolor=ft.Colors.CYAN_300, transform=ft.Transform( matrix=ft.Matrix4.identity().set_entry(3, 2, 0.0018).rotate_y(pi / 8) ), ), ft.RotatedBox(quarter_turns=1, content=ft.Text("Rotated")), ) ft.run(main) ``` -------------------------------- ### Basic Flet Controls Source: https://github.com/flet-dev/website/blob/main/blog/2025-10-08-introducing-declarative-ui-in-flet.md Examples of common Flet controls like Text, Button, Row, and Column. ```python ft.Text("Hello") ft.Button("Click me") ft.Column([ft.Text("A"), ft.Text("B")]) ``` -------------------------------- ### Example: Video Control Implementation in Python Source: https://github.com/flet-dev/website/blob/main/blog/2024-02-14-flet-adaptive-and-custom-controls.md This Python class demonstrates how to implement a custom control in Flet by inheriting from `Control` or `ConstrainedControl`. ```python class Video(Control): # ... implementation details ... pass ``` -------------------------------- ### Flet Navigation Example: Route Change Handler Source: https://github.com/flet-dev/website/blob/main/blog/2022-07-23-navigation-and-routing.md This snippet demonstrates how to set up `page.on_route_change` and `page.on_view_pop` to manage views based on the current route. It includes navigation between a home page and a store page using `page.go()`. ```python import flet as ft def main(page: ft.Page): page.title = "Routes Example" def route_change(route): page.views.clear() page.views.append( ft.View( "/", [ ft.AppBar(title=ft.Text("Flet app"), bgcolor=ft.Colors.SURFACE_VARIANT), ft.ElevatedButton("Visit Store", on_click=lambda _: page.go("/store")), ], ) ) if page.route == "/store": page.views.append( ft.View( "/store", [ ft.AppBar(title=ft.Text("Store"), bgcolor=ft.Colors.SURFACE_VARIANT), ft.ElevatedButton("Go Home", on_click=lambda _: page.go("/")), ], ) ) page.update() def view_pop(view): page.views.pop() top_view = page.views[-1] page.go(top_view.route) page.on_route_change = route_change page.on_view_pop = view_pop page.go(page.route) ft.run(main, view=ft.AppView.WEB_BROWSER) ``` -------------------------------- ### Build Flet Extension App for macOS Source: https://github.com/flet-dev/website/blob/main/blog/2025-01-24-flet-v-0-26-release-announcement.md After creating your extension and developing your app, use the `flet build` command to compile your application for a specific platform. This example shows building for macOS. ```bash flet build macos -v ``` -------------------------------- ### Loading fonts from URL and assets Source: https://github.com/flet-dev/website/blob/main/blog/2022-06-12-using-custom-fonts-in-flet-app.md This example demonstrates loading fonts from both an external URL (GitHub) and the local assets directory. Ensure `assets_dir` is specified in `flet.app()` for local assets. The 'Kanit' font is set as the default theme font, while 'Open Sans' is applied to a specific Text control. ```python import flet as ft def main(page: ft.Page): page.title = "Custom fonts" page.fonts = { "Kanit": "https://raw.githubusercontent.com/google/fonts/master/ofl/kanit/Kanit-Bold.ttf", "Open Sans": "fonts/OpenSans-Regular.ttf", } page.theme = ft.Theme(font_family="Kanit") page.add( ft.Text("This is rendered with Kanit font"), ft.Text("This is Open Sans font example", font_family="Open Sans"), ) ft.run(main, assets_dir="assets") ``` -------------------------------- ### Transparent App Window Example Source: https://github.com/flet-dev/website/blob/main/blog/2022-09-27-user-authentication.md Set the page and window background to transparent and hide the window title bar for a frameless, floating appearance. Adjust window position with `window_left` and `window_top`. ```python import flet as ft def main(page: ft.Page): page.window_bgcolor = ft.Colors.TRANSPARENT page.bgcolor=ft.Colors.TRANSPARENT page.window_title_bar_hidden = True page.window_frameless = True page.window_left = 400 page.window_top = 400 page.add(ft.ElevatedButton("I'm a floating button!")) ft.run(main) ``` -------------------------------- ### Markdown Code Highlight Example Source: https://github.com/flet-dev/website/blob/main/blog/2022-11-13-responsive-row-and-mobile-controls.md Shows how to use Markdown in Flet to highlight code blocks. Ensure the Flet module is up-to-date for full functionality. ```python import flet as ft def main(page: ft.Page): page.title = "Markdown Code Highlight" page.vertical_alignment = ft.MainAxisAlignment.START page.add( ft.Markdown( """```python # This is a Python code block def hello_world(): print("Hello, world!") ``` ```javascript // This is a JavaScript code block function greet() { console.log("Hello, world!"); } ```""", extension_set=ft.FontExtensionSet( web="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap" ), code_style=ft.TextThemeStyle.BODY_LARGE, tooltip=ft.Tooltip("Code snippet") ) ) ft.app(target=main) ``` -------------------------------- ### Basic Flet App with Direct Control References Source: https://github.com/flet-dev/website/blob/main/blog/2022-08-03-control-refs.md This example shows a simple Flet application where controls are directly referenced using variables. It's useful for understanding the fundamental structure before introducing `Ref`. ```python import flet as ft def main(page): first_name = ft.TextField(label="First name", autofocus=True) last_name = ft.TextField(label="Last name") greetings = ft.Column() def btn_click(e): greetings.controls.append(ft.Text(f"Hello, {first_name.value} {last_name.value}!")) first_name.value = "" last_name.value = "" page.update() first_name.focus() page.add( first_name, last_name, ft.ElevatedButton("Say hello!", on_click=btn_click), greetings, ) ft.run(main) ``` -------------------------------- ### Flet App with Observable State and use_state Hook Source: https://github.com/flet-dev/website/blob/main/blog/2025-10-08-introducing-declarative-ui-in-flet.md An example Flet application demonstrating observable state management with the use_state hook and an asyncio counter. ```python import asyncio from dataclasses import dataclass import flet as ft @dataclass @ft.observable class AppState: counter: float async def start_counter(self): self.counter = 0 for _ in range(0, 10): self.counter += 0.1 await asyncio.sleep(0.5) @ft.component def App(): state, _ = ft.use_state(AppState(counter=0)) return [ ft.ProgressBar(state.counter), ft.Button("Run!", on_click=state.start_counter), ] ft.run(lambda page: page.render(App)) ``` -------------------------------- ### Migrate Badge Property in NavigationBar Source: https://github.com/flet-dev/website/blob/main/blog/2024-11-27-flet-v-0-25-release-announcement.md Example demonstrating the migration of the Badge control within NavigationBar destinations from `icon_content` to the new `icon` property with badge support. ```python # before page.navigation_bar = ft.NavigationBar( destinations=[ ft.NavigationBarDestination( icon_content=ft.Badge( content=ft.Icon(ft.Icons.PHONE), text=10, ), label="Calls", ), ] ) # after page.navigation_bar = ft.NavigationBar( destinations=[ ft.NavigationBarDestination( icon=ft.Icon( ft.Icons.PHONE, badge="10", ), label="Calls", ), ] ) ``` -------------------------------- ### Set Android Permissions and Features in Command Line Source: https://github.com/flet-dev/website/blob/main/blog/2024-11-27-flet-v-0-25-release-announcement.md Control Android permissions and features using the `flet build` command with `--android-permissions` and `--android-features` flags. This example grants read/write storage permissions and disables network location features. ```bash flet build \ --android-permissions android.permission.READ_EXTERNAL_STORAGE=True \ android.permission.WRITE_EXTERNAL_STORAGE=True \ --android-features android.hardware.location.network=False ``` -------------------------------- ### Flet Counter App Example Source: https://github.com/flet-dev/website/blob/main/blog/2023-02-06-standalone-flet-web-apps-with-pyodide.md A simple Flet counter application demonstrating basic UI elements and event handling. This app can be packaged into a standalone web app. ```python import flet as ft def main(page: ft.Page): page.title = "Flet counter example" page.vertical_alignment = ft.MainAxisAlignment.CENTER txt_number = ft.TextField(value="0", text_align=ft.TextAlign.RIGHT, width=100) def minus_click(e): txt_number.value = str(int(txt_number.value) - 1) page.update() def plus_click(e): txt_number.value = str(int(txt_number.value) + 1) page.update() page.add( ft.Row( [ ft.IconButton(ft.Icons.REMOVE, on_click=minus_click), txt_number, ft.IconButton(ft.Icons.ADD, on_click=plus_click), ], alignment=ft.MainAxisAlignment.CENTER, ) ) ft.run(main) ``` -------------------------------- ### ResponsiveRow with Breakpoint-Specific Columns Source: https://github.com/flet-dev/website/blob/main/blog/2022-11-13-responsive-row-and-mobile-controls.md Configure column spans for different screen sizes using breakpoints. This example shows columns that take 6 units on small screens and collapse on larger ones. ```python import flet as ft ft.ResponsiveRow([ ft.Column(col={"sm": 6}, controls=ft.Text("Column 1")), ft.Column(col={"sm": 6}, controls=ft.Text("Column 2")) ]) ``` -------------------------------- ### Manage Linux desktop client versions Source: https://github.com/flet-dev/website/blob/main/blog/2024-10-10-flet-new-packaging-pre-release.md Commands to switch between the light-weight Linux client and the full desktop client. ```bash pip uninstall flet-desktop-light --yes ``` ```bash pip install flet-desktop==0.25.0.dev3519 ``` -------------------------------- ### Configure requirements.txt for pre-release Source: https://github.com/flet-dev/website/blob/main/blog/2024-10-15-pyproject-toml-support-for-flet-build-command.md Ensure your requirements.txt file points to the correct pre-release version or includes the --pre flag. ```text flet==0.25.0.dev3526 ``` ```text --pre flet ``` -------------------------------- ### Declarative Hello World (Alternative) Source: https://github.com/flet-dev/website/blob/main/blog/2025-10-08-introducing-declarative-ui-in-flet.md An alternative way to write the declarative 'Hello, world!' app without using lambdas, defining a separate `main` function for clarity. ```python @ft.component def App(): return ft.Text("Hello, Flet!") def main(page: ft.Page): page.render(App) ft.run(main) # as before ``` -------------------------------- ### Manage iOS simulators Source: https://github.com/flet-dev/website/blob/main/blog/2025-12-02-flet-debug-the-new-cli-for-testing-flet-apps-on-mobile-devices.md Commands for listing simulator types, runtimes, and creating new instances. ```bash xcrun simctl list devicetypes ``` ```bash xcrun simctl list runtimes ``` ```bash xcrun simctl create "" "" "" ``` ```bash xcrun simctl create "My iPhone 17 Pro" "iPhone 17 Pro" com.apple.CoreSimulator.SimRuntime.iOS-26-1 ``` ```bash open -a Simulator ``` -------------------------------- ### Initialize Flet Application Source: https://context7.com/flet-dev/website/llms.txt The entry point for Flet applications, demonstrating both imperative style and different view modes. ```python import flet as ft # Imperative style - traditional approach def main(page: ft.Page): page.title = "My Flet App" page.vertical_alignment = ft.MainAxisAlignment.CENTER # Add controls directly to page page.add( ft.Text("Hello, Flet!", size=30), ft.ElevatedButton("Click me", on_click=lambda e: print("Clicked!")) ) ft.run(main) # Run as web app in browser ft.run(main, view=ft.AppView.WEB_BROWSER) # Run as desktop window ft.run(main, view=ft.AppView.FLET_APP) ``` -------------------------------- ### Basic 'Hello World' with Pyobjus Source: https://github.com/flet-dev/website/blob/main/blog/2025-02-23-tap-into-native-android-and-ios-apis-with-pyjnius-and-pyobjus.md Demonstrates the simplest usage of Pyobjus by creating an NSString object and printing its UTF8String representation. ```python from pyobjus import autoclass NSString = autoclass('NSString') text = NSString.alloc().initWithUTF8String_('Hello world') print(text.UTF8String()) ``` -------------------------------- ### Install Flet FastAPI dependencies Source: https://github.com/flet-dev/website/blob/main/blog/2023-08-30-flet-for-fastapi.md Required packages for running Flet applications within a FastAPI environment. ```bash pip install flet-fastapi pip install uvicorn ``` -------------------------------- ### Implement CodeEditor Control Source: https://github.com/flet-dev/website/blob/main/blog/2026-02-24-flet-v-0-81-release-announcement.md This example shows how to embed a CodeEditor for source code display. Requires `flet_code_editor`. ```python import flet as ft import flet_code_editor as fce def main(page: ft.Page): page.add( fce.CodeEditor( language=fce.CodeLanguage.PYTHON, code_theme=fce.CodeTheme.ATOM_ONE_LIGHT, value="print('Hello, Flet')", expand=True, ) ) ft.run(main) ``` -------------------------------- ### Serve Static Website Locally Source: https://github.com/flet-dev/website/blob/main/blog/2023-02-06-standalone-flet-web-apps-with-pyodide.md Serve the generated static website using Python's built-in HTTP server. Navigate to `http://localhost:8000` in your browser to view the app. ```bash python -m http.server --directory dist ``` -------------------------------- ### Configure app package contents Source: https://github.com/flet-dev/website/blob/main/blog/2024-10-15-pyproject-toml-support-for-flet-build-command.md Settings for module paths, source directories, and compilation options. ```toml [tool.flet] app.module = "main" # --module-name app.path = "src" # path to Python app relative to `pyproject.toml` app.exclude = ["assets"] # --exclude compile.app = false # --compile-app compile.packages = false # --compile-packages compile.cleanup = false # --cleanup-on-compile ``` ```toml [tool.flet.app] module = "main" path = "src" exclude = ["assets"] [tool.flet.compile] app = false packages = false cleanup = false ``` -------------------------------- ### Draw Shapes with Canvas Source: https://github.com/flet-dev/website/blob/main/blog/2023-04-26-canvas.md Example of drawing a smiley face using Circle and Arc shapes with specific Paint styles. ```python import math import flet as ft import flet.canvas as cv def main(page: ft.Page): stroke_paint = paint = ft.Paint(stroke_width=2, style=ft.PaintingStyle.STROKE) fill_paint = paint = ft.Paint(style=ft.PaintingStyle.FILL) cp = cv.Canvas( [ cv.Circle(100, 100, 50, stroke_paint), cv.Circle(80, 90, 10, stroke_paint), cv.Circle(84, 87, 5, fill_paint), cv.Circle(120, 90, 10, stroke_paint), cv.Circle(124, 87, 5, fill_paint), cv.Arc(70, 95, 60, 40, 0, math.pi, paint=stroke_paint), ], width=float("inf"), expand=True, ) page.add(cp) ft.run(main) ``` -------------------------------- ### Build static site Source: https://github.com/flet-dev/website/blob/main/README.md Generates the production-ready static files in the build directory. ```bash $ yarn build ``` -------------------------------- ### Apply Sweep Gradient to Container Source: https://github.com/flet-dev/website/blob/main/blog/2022-08-04-gradients-button-textfield-styles.md Uses SweepGradient to create a circular sweep pattern defined by start and end angles. ```python import math import flet as ft def main(page: ft.Page): page.add( ft.Container( alignment=ft.alignment.center, gradient=SweepGradient( center=ft.alignment.center, start_angle=0.0, end_angle=math.pi * 2, colors=[ "0xFF4285F4", "0xFF34A853", "0xFFFBBC05", "0xFFEA4335", "0xFF4285F4", ], stops=[0.0, 0.25, 0.5, 0.75, 1.0], ), width=150, height=150, border_radius=5, ) ) ft.run(main) ``` -------------------------------- ### Flet Extension: extension.dart (v1.0) Source: https://github.com/flet-dev/website/blob/main/blog/2025-06-26-introducing-flet-1-0-alpha.md Example of a Flet 1.0 extension implementing `FletExtension` with `createWidget` and `createService` methods for exporting controls and services. ```dart import 'package:flet/flet.dart';\nimport 'package:flutter/cupertino.dart';\nimport 'package:google_mobile_ads/google_mobile_ads.dart';\n\nimport 'banner.dart';\nimport 'interstitial.dart';\n\nclass Extension extends FletExtension {\n @override\n void ensureInitialized() {\n if (isMobilePlatform()) {\n MobileAds.instance.initialize();\n }\n }\n\n @override\n FletService? createService(Control control) {\n switch (control.type) {\n case "InterstitialAd":\n return InterstitialAdService(control: control);\n default:\n return null;\n }\n }\n\n @override\n Widget? createWidget(Key? key, Control control) {\n switch (control.type) {\n case "BannerAd":\n return BannerAdControl(control: control);\n default:\n return null;\n }\n }\n} ``` -------------------------------- ### Create New Flet Extension Project Source: https://github.com/flet-dev/website/blob/main/blog/2025-01-24-flet-v-0-26-release-announcement.md Use the `flet create` command with the `extension` template to scaffold a new Flet extension project. This sets up a basic structure for custom controls. ```bash flet create --template extension --project-name my-control ``` -------------------------------- ### Flet Extension: create_control.dart (v0.x) Source: https://github.com/flet-dev/website/blob/main/blog/2025-06-26-introducing-flet-1-0-alpha.md Example of an older Flet extension using `createControl` factory for widget creation. This pattern is superseded in Flet 1.0. ```dart import 'package:flet/flet.dart';\nimport 'package:google_mobile_ads/google_mobile_ads.dart';\n\nimport 'banner.dart';\nimport 'interstitial.dart';\n\nCreateControlFactory createControl = (CreateControlArgs args) {\n switch (args.control.type) {\n case "banner_ad":\n return BannerAdControl(\n parent: args.parent, control: args.control, backend: args.backend);\n case "interstitial_ad":\n return InterstitialAdControl(\n parent: args.parent, control: args.control, backend: args.backend);\n default:\n return null;\n }\n};\n\nvoid ensureInitialized() {\n if (isMobilePlatform()) {\n MobileAds.instance.initialize();\n }\n} ``` -------------------------------- ### Imperative UI Update Example Source: https://github.com/flet-dev/website/blob/main/blog/2025-10-08-introducing-declarative-ui-in-flet.md This snippet illustrates an imperative UI update, where specific controls are directly manipulated and their properties changed in response to an event. ```python left_column.visible = False right_column.visible = True right_column.controls.append(ft.Text("Complete!")) ``` -------------------------------- ### Declarative Hello World App in Flet Source: https://github.com/flet-dev/website/blob/main/blog/2025-10-08-introducing-declarative-ui-in-flet.md A minimal Flet application demonstrating the declarative 'Hello, world!' pattern. It uses `page.render()` for bootstrapping the declarative UI. ```python import flet as ft def App(): return ft.Text("Hello, world!") ft.run(lambda page: page.render(App)) ``` -------------------------------- ### Upgrade Flet to v0.26.0 Source: https://github.com/flet-dev/website/blob/main/blog/2025-01-24-flet-v-0-26-release-announcement.md Use this command to upgrade Flet and all its related packages to the latest version. The `[all]` specifier ensures installation of `flet`, `flet-cli`, `flet-desktop`, and `flet-web`. ```bash pip install 'flet[all]' --upgrade ``` -------------------------------- ### Build iOS Simulator App Source: https://github.com/flet-dev/website/blob/main/blog/2026-02-24-flet-v-0-81-release-announcement.md Command to build an unsigned .app for iOS simulator, allowing drag-and-drop deployment. ```bash flet build ios-simulator ``` -------------------------------- ### Configure splash screen settings Source: https://github.com/flet-dev/website/blob/main/blog/2024-10-15-pyproject-toml-support-for-flet-build-command.md Define splash screen colors and platform-specific visibility. ```toml [tool.flet.splash] color = "" # --splash-color dark_color = "" # --splash-dark-color web = false # --no-web-splash ios = false # --no-ios-splash android = false # --no-android-splash ``` -------------------------------- ### Variable Weight Font Example Source: https://github.com/flet-dev/website/blob/main/blog/2022-11-13-responsive-row-and-mobile-controls.md Illustrates the use of variable fonts in Flet for dynamic text styling. Upgrade Flet to the latest version to utilize this feature. ```python import flet as ft def main(page: ft.Page): page.title = "Variable Weight Font" page.vertical_alignment = ft.MainAxisAlignment.CENTER page.add( ft.Text("Variable font weight:", size=30), ft.Text("Thin", weight=ft.FontWeight.W_100, size=30), ft.Text("Light", weight=ft.FontWeight.W_200, size=30), ft.Text("Normal", weight=ft.FontWeight.W_400, size=30), ft.Text("Medium", weight=ft.FontWeight.W_500, size=30), ft.Text("SemiBold", weight=ft.FontWeight.W_600, size=30), ft.Text("Bold", weight=ft.FontWeight.W_700, size=30), ft.Text("ExtraBold", weight=ft.FontWeight.W_800, size=30), ft.Text("Black", weight=ft.FontWeight.W_900, size=30), ) ft.app(target=main) ``` -------------------------------- ### Get iOS OS Version with Pyobjus Source: https://github.com/flet-dev/website/blob/main/blog/2025-02-23-tap-into-native-android-and-ios-apis-with-pyjnius-and-pyobjus.md Retrieve the iOS operating system version string by loading the Foundation framework and accessing NSProcessInfo using Pyobjus. ```python from pyobjus import autoclass, objc_str from pyobjus.dylib_manager import load_framework, INCLUDE # Load Foundation framework load_framework(INCLUDE.Foundation) # Get NSProcessInfo instance NSProcessInfo = autoclass('NSProcessInfo') process_info = NSProcessInfo.processInfo() # Retrieve OS version as a string os_version = process_info.operatingSystemVersionString.UTF8String() print(f"iOS Version: {os_version}") ``` -------------------------------- ### Configure Android permissions and features Source: https://github.com/flet-dev/website/blob/main/blog/2024-10-10-flet-new-packaging-pre-release.md Manage Android manifest permissions and hardware features using the --android-permissions and --android-features flags. ```bash flet build --android-permissions permission=True|False ... --android-features feature_name=True|False ``` ```bash flet build \ --android-permissions android.permission.READ_EXTERNAL_STORAGE=True \ android.permission.WRITE_EXTERNAL_STORAGE=True \ --android-features android.hardware.location.network=False ``` ```bash flet build --android-permissions android.permission.INTERNET=False ``` -------------------------------- ### Work with App User Settings on iOS Source: https://github.com/flet-dev/website/blob/main/blog/2025-02-23-tap-into-native-android-and-ios-apis-with-pyjnius-and-pyobjus.md Manage application user defaults on iOS using Pyobjus. This example shows how to set and retrieve a string value associated with a specific key. ```python from pyobjus import autoclass, objc_str NSUserDefaults = autoclass('NSUserDefaults') key = "pyobjus_hello_world_key" value = "Hello, world!" # set key NSUserDefaults.standardUserDefaults().setObject_forKey_(objc_str(value), objc_str(key)) # get key ret = NSUserDefaults.standardUserDefaults().stringForKey_(objc_str(key)) assert ret.UTF8String() == value ``` -------------------------------- ### Integrate Video and Audio Controls Source: https://context7.com/flet-dev/website/llms.txt Utilizes flet_video and flet_audio packages for media playback. Requires including these packages during the build process. ```python import flet as ft def main(page: ft.Page): # Video player (requires flet_video package) video = ft.Video( playlist=[ ft.VideoMedia("https://example.com/video.mp4"), ft.VideoMedia("/local/path/to/video.mp4"), ], playlist_mode=ft.PlaylistMode.LOOP, fill_color=ft.Colors.BLACK, aspect_ratio=16/9, volume=100, autoplay=False, filter_quality=ft.FilterQuality.HIGH, on_loaded=lambda e: print("Video loaded"), on_error=lambda e: print(f"Video error: {e.data}"), ) # Audio player (requires flet_audio package) audio = ft.Audio( src="https://example.com/audio.mp3", autoplay=False, volume=1, balance=0, on_loaded=lambda e: print("Audio loaded"), on_position_changed=lambda e: print(f"Position: {e.data}"), ) page.add( video, audio, ft.Row([ ft.ElevatedButton("Play", on_click=lambda _: video.play()), ft.ElevatedButton("Pause", on_click=lambda _: video.pause()), ft.ElevatedButton("Stop", on_click=lambda _: video.stop()), ]) ) ft.run(main) # Build with video support: # flet build apk --include-packages flet_video ``` -------------------------------- ### Configure Android Bundle Signing Options Source: https://github.com/flet-dev/website/blob/main/blog/2024-10-15-pyproject-toml-support-for-flet-build-command.md Specify Android bundle signing options, including the key store path and key alias. Passwords can be provided via command-line options or environment variables. ```toml [tool.flet.android.signing] # store and key passwords can be passed with `--android-signing-key-store-password` # and `--android-signing-key-password` options or # FLET_ANDROID_SIGNING_KEY_STORE_PASSWORD # and FLET_ANDROID_SIGNING_KEY_PASSWORD environment variables. key_store = "path/to/store.jks" # --android-signing-key-store key_alias = "upload" ``` -------------------------------- ### Set iOS Location Permissions in Command Line Source: https://github.com/flet-dev/website/blob/main/blog/2024-11-27-flet-v-0-25-release-announcement.md Use the `--info-plist` flag with the `flet build` command to specify permissions for iOS bundles. This example sets the description for location service usage. ```bash flet build --info-plist NSLocationWhenInUseUsageDescription="This app uses location service when in use." ``` -------------------------------- ### Deploy to GitHub Pages Source: https://github.com/flet-dev/website/blob/main/README.md Builds the site and pushes the output to the gh-pages branch using SSH authentication. ```bash $ GIT_USER= USE_SSH=true yarn deploy ``` -------------------------------- ### Get Android OS Details with Pyjnius Source: https://github.com/flet-dev/website/blob/main/blog/2025-02-23-tap-into-native-android-and-ios-apis-with-pyjnius-and-pyobjus.md Access Android OS build information such as device model, manufacturer, brand, hardware, product, device, OS version, and SDK integer using Pyjnius. ```python from jnius import autoclass # Get Build and Build.VERSION classes Build = autoclass('android.os.Build') Version = autoclass('android.os.Build$VERSION') # Get OS details device_model = Build.MODEL manufacturer = Build.MANUFACTURER brand = Build.BRAND hardware = Build.HARDWARE product = Build.PRODUCT device = Build.DEVICE os_version = Version.RELEASE sdk_version = Version.SDK_INT ``` -------------------------------- ### Flet App Using Ref for Control Management Source: https://github.com/flet-dev/website/blob/main/blog/2022-08-03-control-refs.md This example refactors the initial Flet application to use the `Ref` class for managing control references. It improves clarity by separating control definition from their usage in event handlers. ```python import flet as ft def main(page): first_name = ft.Ref[ft.TextField]() last_name = ft.Ref[ft.TextField]() greetings = ft.Ref[ft.Column]() def btn_click(e): greetings.current.controls.append( ft.Text(f"Hello, {first_name.current.value} {last_name.current.value}!") ) first_name.current.value = "" last_name.current.value = "" page.update() first_name.current.focus() page.add( ft.TextField(ref=first_name, label="First name", autofocus=True), ft.TextField(ref=last_name, label="Last name"), ft.ElevatedButton("Say hello!", on_click=btn_click), ft.Column(ref=greetings), ) ft.run(main) ``` -------------------------------- ### Manage Flet Projects with CLI Source: https://context7.com/flet-dev/website/llms.txt Commands for creating, running, building, and debugging Flet applications across various platforms. ```bash # Create a new Flet project flet create my_app cd my_app # Run in development mode with hot reload flet run # Run as web app in browser flet run --web # Build for different platforms flet build apk # Android APK flet build aab # Android App Bundle (for Play Store) flet build ipa # iOS (requires macOS) flet build macos # macOS app flet build windows # Windows executable flet build linux # Linux executable flet build web # Static website # Include additional Flutter packages flet build apk --include-packages flet_video,flet_audio # Debug on real devices flet devices # List connected devices flet emulators # List available emulators flet debug # Start debugging session # Upgrade Flet pip install 'flet[all]' --upgrade # Or with uv uv sync --upgrade-package flet ``` -------------------------------- ### Implement custom controls by inheriting base classes Source: https://github.com/flet-dev/website/blob/main/blog/2024-03-06-flet-fastapi-and-async-api-improvements.md Shows how to create a custom control by inheriting from a base Flet control instead of using the deprecated UserControl class. ```python import asyncio import flet as ft class Countdown(ft.Text): def __init__(self, seconds): super().__init__() self.seconds = seconds def did_mount(self): self.running = True self.page.run_task(self.update_timer) def will_unmount(self): self.running = False async def update_timer(self): while self.seconds and self.running: mins, secs = divmod(self.seconds, 60) self.value = "{:02d}:{:02d}".format(mins, secs) self.update() await asyncio.sleep(1) self.seconds -= 1 def main(page: ft.Page): page.add(Countdown(120), Countdown(60)) ft.run(main) ``` -------------------------------- ### Controlled Input with Flet State Management Source: https://github.com/flet-dev/website/blob/main/blog/2025-10-08-introducing-declarative-ui-in-flet.md Implement controlled inputs by storing control values in the app's state. Use `on_change` to update the state and `value` to bind the state to the control. This example demonstrates a form with text input and buttons. ```python from dataclasses import dataclass from typing import cast import flet as ft @dataclass @ft.observable class Form: name: str = "" def set_name(self, value): self.name = value async def submit(self, e: ft.Event[ft.Button]): e.page.show_dialog( ft.AlertDialog( title="Hello", content=ft.Text(f"Hello, {self.name}!"), ) ) async def reset(self): self.name = "" @ft.component def App(): form, _ = ft.use_state(Form()) return [ ft.TextField( label="Your name", value=form.name, on_change=lambda e: form.set_name(e.control.value), ), ft.Row( cast( list[ft.Control], [ ft.FilledButton("Submit", on_click=form.submit), ft.FilledTonalButton("Reset", on_click=form.reset), ], ) ), ] ft.run(lambda page: page.render(App)) ``` -------------------------------- ### Configure Web Renderer Source: https://github.com/flet-dev/website/blob/main/blog/2024-10-15-pyproject-toml-support-for-flet-build-command.md Choose the rendering engine for the web application ('canvaskit' or 'html'). This corresponds to the `--web-renderer` command-line option. ```toml renderer = "canvaskit" # --web-renderer ``` -------------------------------- ### Implement Draggable Containers with GestureDetector Source: https://github.com/flet-dev/website/blob/main/blog/2022-10-11-gesture-detector.md This example demonstrates how to make a container draggable within a Stack using the GestureDetector control. It utilizes the on_vertical_drag_update event to update the container's position based on drag movements. Ensure the GestureDetector is placed within a Stack for proper positioning. ```python import flet as ft def main(page: ft.Page): def on_pan_update(e: ft.DragUpdateEvent): e.control.top = max(0, e.control.top + e.delta_y) e.control.left = max(0, e.control.left + e.delta_x) e.control.update() gd = ft.GestureDetector( mouse_cursor=ft.MouseCursor.MOVE, on_vertical_drag_update=on_pan_update, left=100, top=100, content=ft.Container(bgcolor=ft.Colors.BLUE, width=50, height=50, border_radius=5), ) page.add( ft.Stack([gd], expand=True)) ft.run(main) ``` -------------------------------- ### Configure macOS entitlements Source: https://github.com/flet-dev/website/blob/main/blog/2024-10-10-flet-new-packaging-pre-release.md Use the --macos-entitlements flag to define security entitlements for macOS builds. ```bash flet build --macos-entitlements name_1=True|False name_2=True|False ... ``` -------------------------------- ### Elaborate Responsive Layout with Multiple Breakpoints and Spacing Source: https://github.com/flet-dev/website/blob/main/blog/2022-11-13-responsive-row-and-mobile-controls.md Demonstrates a complex responsive layout using ResponsiveRow with varying column spans for different breakpoints (sm, md, xl). It also shows how to apply run spacing for smaller screens. ```python import flet as ft def main(page: ft.Page): def page_resize(e): pw.value = f"{page.width} px" pw.update() page.on_resize = page_resize pw = ft.Text(bottom=50, right=50, style="displaySmall") page.overlay.append(pw) page.add( ft.ResponsiveRow( [ ft.Container( ft.Text("Column 1"), padding=5, bgcolor=ft.Colors.YELLOW, col={"sm": 6, "md": 4, "xl": 2}, ), ft.Container( ft.Text("Column 2"), padding=5, bgcolor=ft.Colors.GREEN, col={"sm": 6, "md": 4, "xl": 2}, ), ft.Container( ft.Text("Column 3"), padding=5, bgcolor=ft.Colors.BLUE, col={"sm": 6, "md": 4, "xl": 2}, ), ft.Container( ft.Text("Column 4"), padding=5, bgcolor=ft.Colors.PINK_300, col={"sm": 6, "md": 4, "xl": 2}, ), ], ), ft.ResponsiveRow( [ ft.TextField(label="TextField 1", col={"md": 4}), ft.TextField(label="TextField 2", col={"md": 4}), ft.TextField(label="TextField 3", col={"md": 4}), ], run_spacing={"xs": 10}, ), ) page_resize(None) ft.run(main) ``` -------------------------------- ### Publish Flet App Source: https://github.com/flet-dev/website/blob/main/blog/2023-02-06-standalone-flet-web-apps-with-pyodide.md Use the `flet publish` command to package your Flet application into a standalone static website. The output is placed in the `dist` directory. ```bash flet publish counter.py ``` -------------------------------- ### Create a minimal Flet FastAPI application Source: https://github.com/flet-dev/website/blob/main/blog/2023-08-30-flet-for-fastapi.md Defines an asynchronous Flet main function and wraps it with the flet_fastapi app handler. ```python import flet as ft import flet_fastapi async def main(page: ft.Page): await page.add_async( ft.Text("Hello, Flet!") ) app = flet_fastapi.app(main) ``` -------------------------------- ### List connected devices Source: https://github.com/flet-dev/website/blob/main/blog/2025-12-02-flet-debug-the-new-cli-for-testing-flet-apps-on-mobile-devices.md Displays all connected iOS/Android devices and emulators. ```bash flet devices ```