Skip to content

Semantics

Examples#

Basic Example#

import flet as ft


def main(page: ft.Page):
    def handle_gain_accessibility_focus(e: ft.Event[ft.Semantics]):
        print("focus gained")

    def handle_lose_accessibility_focus(e: ft.Event[ft.Semantics]):
        print("focus lost")

    page.add(
        ft.Column(
            controls=[
                ft.Semantics(
                    label="Input your occupation",
                    on_did_gain_accessibility_focus=handle_gain_accessibility_focus,
                    on_did_lose_accessibility_focus=handle_lose_accessibility_focus,
                    content=ft.TextField(
                        label="Occupation",
                        hint_text="Use 20 words or less",
                        value="What is your occupation?",
                    ),
                ),
                ft.Icon(name="settings", color="#c1c1c1"),
            ]
        )
    )


ft.run(main)

Semantics #

Bases: Control

Provides semantic annotations for the control tree, describing the meaning and purpose of controls.

These annotations are utilized by accessibility tools, search engines, and semantic analysis software to better understand the structure and functionality of the application.

badge #

badge: BadgeValue | None = None

TBD

button #

button: bool | None = None

Whether this subtree represents a button.

checked #

checked: bool | None = None

Whether this subtree represents a checkbox or similar widget with a "checked" state, and what its current state is.

col #

col: ResponsiveNumber = 12

If a parent of this control is a ResponsiveRow, this property is used to determine how many virtual columns of a screen this control will span.

Can be a number or a dictionary configured to have a different value for specific breakpoints, for example col={"sm": 6}.

This control spans the 12 virtual columns by default.

Dimensions
Breakpoint Dimension
xs <576px
sm ≥576px
md ≥768px
lg ≥992px
xl ≥1200px
xxl ≥1400px

container #

container: bool | None = None

TBD

content #

content: Control | None = None

The Control to annotate.

current_value_length #

current_value_length: int | None = None

The current number of characters that have been entered into an editable text field.

data #

data: Any = skip_field()

Arbitrary data of any type.

decreased_value #

decreased_value: str | None = None

The value that the semantics node represents when it is decreased.

disabled #

disabled: bool = False

Every control has disabled property which is False by default - control and all its children are enabled.

Note

The value of this property will be propagated down to all children controls recursively.

Example

For example, if you have a form with multiple entry controls you can disable them all together by disabling container:

ft.Column(
    disabled = True,
    controls=[
        ft.TextField(),
        ft.TextField()
    ]
)

exclude_semantics #

exclude_semantics: bool = False

TBD

expand #

expand: bool | int | None = None

Specifies whether/how this control should expand to fill available space in its parent layout.

More information here.

Note

Has effect only if the direct parent of this control is one of the following controls, or their subclasses: Column, Row, View, Page.

expand_loose #

expand_loose: bool = False

Allows the control to expand along the main axis if space is available, but does not require it to fill all available space.

More information here.

Note

If expand_loose is True, it will have effect only if:

  • expand is not None and
  • the direct parent of this control is one of the following controls, or their subclasses: Column, Row, View, Page.

expanded #

expanded: bool | None = None

Whether this subtree represents something that can be in an "expanded" or "collapsed" state.

focus #

focus: bool | None = None

Whether the node currently holds input focus.

focusable #

focusable: bool | None = None

Whether the node is able to hold input focus.

header #

header: bool | None = None

Whether this subtree represents a header.

heading_level #

heading_level: int | None = None

The heading level in the DOM document structure.

hidden #

hidden: bool | None = None

Whether this subtree is currently hidden.

hint_text #

hint_text: str | None = None

A brief textual description of the result of an action performed on the content control.

image #

image: bool | None = None

Whether the node represents an image.

increased_value #

increased_value: str | None = None

The value that the semantics node represents when it is increased.

key #

key: (
    str | int | float | bool | ValueKey | ScrollKey | None
) = None

label #

label: str | None = None

A textual description of the content.

link: bool | None = None

Whether this subtree represents a link.

live_region #

live_region: bool | None = None

Whether this subtree should be considered a live region.

max_value_length #

max_value_length: Number | None = None

The maximum number of characters that can be entered into an editable text field.

mixed #

mixed: bool | None = None

Whether this subtree represents a checkbox or similar control with a "half-checked" state or similar, and whether it is currently in this half-checked state.

multiline #

multiline: bool | None = None

Whether the value is coming from a field that supports multiline text editing.

obscured #

obscured: bool | None = None

Whether value should be obscured.

on_copy #

on_copy: ControlEventHandler[Semantics] | None = None

Called when the current selection is copied to the clipboard.

on_cut #

on_cut: ControlEventHandler[Semantics] | None = None

Called when the current selection is cut to the clipboard.

on_decrease #

on_decrease: ControlEventHandler[Semantics] | None = None

Called when the value represented by the semantics node is decreased.

on_did_gain_accessibility_focus #

on_did_gain_accessibility_focus: (
    ControlEventHandler[Semantics] | None
) = None

Called when the node has gained accessibility focus.

on_did_lose_accessibility_focus #

on_did_lose_accessibility_focus: (
    ControlEventHandler[Semantics] | None
) = None

Called when the node has lost accessibility focus.

on_dismiss #

on_dismiss: ControlEventHandler[Semantics] | None = None

Called when the node is dismissed.

on_double_tap #

on_double_tap: ControlEventHandler[Semantics] | None = None

TBD

on_increase #

on_increase: ControlEventHandler[Semantics] | None = None

Called when the value represented by the semantics node is increased.

on_long_press #

on_long_press: ControlEventHandler[Semantics] | None = None

Called when the node is long-pressed (pressing and holding the screen with the finger for a few seconds without moving it).

on_long_press_hint_text #

on_long_press_hint_text: str | None = None

TBD

on_move_cursor_backward_by_character #

on_move_cursor_backward_by_character: (
    ControlEventHandler[Semantics] | None
) = None

Called when the cursor is moved backward by one character.

on_move_cursor_forward_by_character #

on_move_cursor_forward_by_character: (
    ControlEventHandler[Semantics] | None
) = None

Called when the cursor is moved forward by one character.

on_paste #

on_paste: ControlEventHandler[Semantics] | None = None

Called when the current content of the clipboard is pasted.

on_scroll_down #

on_scroll_down: ControlEventHandler[Semantics] | None = None

Called when a user moves their finger across the screen from top to bottom.

on_scroll_left #

on_scroll_left: ControlEventHandler[Semantics] | None = None

Called when a user moves their finger across the screen from right to left.

on_scroll_right #

on_scroll_right: ControlEventHandler[Semantics] | None = (
    None
)

Called when a user moves their finger across the screen from left to right.

on_scroll_up #

on_scroll_up: ControlEventHandler[Semantics] | None = None

Called when a user moves their finger across the screen from bottom to top.

on_set_text #

on_set_text: ControlEventHandler[Semantics] | None = None

Called when a user wants to replace the current text in the text field with a new text.

Voice access users can trigger this handler by speaking type <text> to their Android devices.

on_tap #

on_tap: ControlEventHandler[Semantics] | None = None

Called when this control is tapped.

on_tap_hint_text #

on_tap_hint_text: str | None = None

TBD

opacity #

opacity: Number = 1.0

Defines the transparency of the control.

Value ranges from 0.0 (completely transparent) to 1.0 (completely opaque without any transparency).

page #

page: Page | PageView | None

The page (of type Page or PageView) to which this control belongs to.

parent #

parent: BaseControl | None

The direct ancestor(parent) of this control.

It defaults to None and will only have a value when this control is mounted (added to the page tree).

The Page control (which is the root of the tree) is an exception - it always has parent=None.

read_only #

read_only: bool | None = None

Whether this subtree is read only.

rtl #

rtl: bool = False

Whether the text direction of the control should be right-to-left (RTL).

selected #

selected: bool | None = None

Whether this subtree represents something that can be in a selected or unselected state, and what its current state is.

slider #

slider: bool | None = None

Whether this subtree represents a slider.

textfield #

textfield: bool | None = None

Whether this subtree represents a text field.

toggled #

toggled: bool | None = None

Whether this subtree represents a toggle switch or similar widget with an "on" state, and what its current state is.

tooltip #

tooltip: str | None = None

A textual description of the widget's tooltip.

value #

value: str | None = None

A textual description of the value of the content control.

visible #

visible: bool = True

Every control has visible property which is True by default - control is rendered on the page. Setting visible to False completely prevents control (and all its children if any) from rendering on a page canvas. Hidden controls cannot be focused or selected with a keyboard or mouse and they do not emit any events.

before_event #

before_event(e: ControlEvent)

before_update #

before_update()

clean #

clean() -> None

did_mount #

did_mount()

init #

init()

is_isolated #

is_isolated()

update #

update() -> None

will_unmount #

will_unmount()