Flet provides various ways to display and style text in your applications. The Text widget is the primary way to display static text content. In this guide, we'll explore how to use and customize text in Flet.

Basic Text Usage

To display simple text, use the Text widget:

import flet as ft

def main(page: ft.Page):
    text_widget = ft.Text("Hello, Flet!")
    page.add(text_widget)

ft.app(target=main)

Customizing Text

You can customize text properties like size, color, weight, alignment, and more:

def main(page: ft.Page):
    text_widget = ft.Text(
        "Styled Text in Flet",
        size=24,
        color="blue",
        weight=ft.FontWeight.BOLD,
        italic=True,
        text_align=ft.TextAlign.CENTER,
    )
    page.add(text_widget)

Using Rich Text

Flet supports rich text formatting using the RichText widget. It allows you to style different parts of the text individually:

def main(page: ft.Page):
    rich_text = ft.RichText(
        spans=[
            ft.TextSpan("This is "),
            ft.TextSpan("bold", weight=ft.FontWeight.BOLD),
            ft.TextSpan(", and this is "),
            ft.TextSpan("italic", italic=True),
            ft.TextSpan(" text!", color="red"),
        ]
    )
    page.add(rich_text)

Multiline Text

If your text content is long, you can enable multiline support:

def main(page: ft.Page):
    long_text = ft.Text(
        "This is a long text example that wraps into multiple lines.",
        size=18,
        max_lines=3,
        overflow=ft.TextOverflow.ELLIPSIS,
    )
    page.add(long_text)

Text with Markdown

You can render Markdown-formatted text using the Markdown widget:

def main(page: ft.Page):
    md_text = ft.Markdown("""
    # Markdown in Flet
    This is **bold**, *italic*, and `code`.
    
    - Item 1
    - Item 2
    """)
    page.add(md_text)

Wrapping Up

Flet provides powerful tools to work with text, allowing customization, formatting, and dynamic content. By using different widgets and styles, you can enhance your application's UI.

Try out different configurations and make your text elements visually appealing!