Checkbox
Checkbox allows to select one or more items from a group, or switch between two mutually exclusive options (checked or unchecked, on or off).
Basic checkboxes
Inherits: LayoutControl
, AdaptiveControl
Properties
-
active_color
(ColorValue | None
) –The color to use when this checkbox is checked.
-
autofocus
(bool
) –True if the control will be selected as the initial focus. If there is more than
-
border_side
(ControlStateValue[BorderSide] | None
) –The color and width of this checkbox's border in all or specific
-
check_color
(ColorValue | None
) –The color to use for the check icon when this checkbox is checked.
-
error
(bool
) –Whether this checkbox wants to show an error state.
-
fill_color
(ControlStateValue[ColorValue] | None
) –The color that fills this checkbox in all or specific
ControlState
s. -
focus_color
(ColorValue | None
) –The color for the checkbox's Material when it has the input focus.
-
hover_color
(ColorValue | None
) –The color to use when this checkbox is hovered.
-
label
(StrOrControl | None
) –The clickable label to display on the right of a checkbox.
-
label_position
(LabelPosition
) –Defines on which side of the checkbox the
label
should be shown. -
label_style
(TextStyle | None
) –The
label
's text style. -
mouse_cursor
(MouseCursor | None
) –The cursor to be displayed when a mouse pointer enters or is hovering over this
-
overlay_color
(ControlStateValue[ColorValue] | None
) –The color of this checkbox's overlay in various
ControlState
states. -
semantics_label
(str | None
) –The semantic label for the checkbox that is not shown in the UI, but will be
-
shape
(OutlinedBorder | None
) –The shape of the checkbox.
-
splash_radius
(Number | None
) –The radius of the circular Material ink response (ripple) in logical pixels.
-
tristate
(bool
) –If
True
the checkbox'svalue
can beTrue
,False
, orNone
. -
value
(bool | None
) –The value of this checkbox.
-
visual_density
(VisualDensity | None
) –Defines how compact the checkbox's layout will be.
Events
-
on_blur
(ControlEventHandler[Checkbox] | None
) –Called when this checkbox has lost focus.
-
on_change
(ControlEventHandler[Checkbox] | None
) –Called when the state of this checkbox is changed.
-
on_focus
(ControlEventHandler[Checkbox] | None
) –Called when this checkbox has received focus.
Examples#
Basic Example#
import flet as ft
def main(page: ft.Page):
def handle_button_click(e: ft.Event[ft.Button]):
message.value = f"Checkboxes values are: {c1.value}, {c2.value}, {c3.value}, "
f"{c4.value}, {c5.value}."
page.update()
page.add(
c1 := ft.Checkbox(label="Unchecked by default checkbox", value=False),
c2 := ft.Checkbox(
label="Undefined by default tristate checkbox", tristate=True
),
c3 := ft.Checkbox(label="Checked by default checkbox", value=True),
c4 := ft.Checkbox(label="Disabled checkbox", disabled=True),
c5 := ft.Checkbox(
label="Checkbox with LEFT label_position",
label_position=ft.LabelPosition.LEFT,
),
ft.Button(content="Submit", on_click=handle_button_click),
message := ft.Text(),
)
if __name__ == "__main__":
ft.run(main)
Handling events#
import flet as ft
def main(page: ft.Page):
def handle_checkbox_change(e: ft.Event[ft.Checkbox]):
page.add(ft.Text(f"Checkbox value changed to {e.control.value}"))
page.update()
page.add(
ft.Checkbox(
label="Checkbox with 'change' event",
on_change=handle_checkbox_change,
)
)
if __name__ == "__main__":
ft.run(main)
Styled checkboxes#
import flet as ft
def main(page: ft.Page):
page.add(
ft.Checkbox(label="Checkbox with default style"),
ft.Checkbox(
label="Checkbox with constant fill color",
fill_color=ft.Colors.RED,
check_color=ft.Colors.YELLOW,
),
ft.Checkbox(
label="Checkbox with dynamic fill color",
fill_color={
ft.ControlState.HOVERED: ft.Colors.BLUE,
ft.ControlState.SELECTED: ft.Colors.GREEN,
ft.ControlState.DEFAULT: ft.Colors.RED,
},
# border_side={ft.ControlState.HOVERED: ft.BorderSide(width=1.0)},
),
)
if __name__ == "__main__":
ft.run(main)
Properties#
active_color
#
active_color: ColorValue | None = None
The color to use when this checkbox is checked.
autofocus
#
autofocus: bool = False
True if the control will be selected as the initial focus. If there is more than one control on a page with autofocus set, then the first one added to the page will get focus.
border_side
#
border_side: ControlStateValue[BorderSide] | None = None
The color and width of this checkbox's border in all or specific
ControlState
s.
Defaults to CheckboxTheme.border_side
, or if that is None
,
falls back to BorderSide
with a width of 2.0
.
Note
Supported states: ControlState.SELECTED
,
ControlState.HOVERED
, ControlState.DISABLED
,
ControlState.FOCUSED
, ControlState.PRESSED
,
ControlState.ERROR
, and ControlState.DEFAULT
.
check_color
#
check_color: ColorValue | None = None
The color to use for the check icon when this checkbox is checked.
error
#
error: bool = False
Whether this checkbox wants to show an error state.
If True
this checkbox will
have a different default container color and check color.
fill_color
#
fill_color: ControlStateValue[ColorValue] | None = None
The color that fills this checkbox in all or specific ControlState
s.
Note
Supported states: ControlState.SELECTED
,
ControlState.HOVERED
, ControlState.DISABLED
,
ControlState.FOCUSED
, and ControlState.DEFAULT
.
focus_color
#
focus_color: ColorValue | None = None
The color for the checkbox's Material when it has the input focus.
If overlay_color
returns a non-None color in the
ControlState.FOCUSED
state, it will be used instead.
Defaults to CheckboxTheme.overlay_color
in the
ControlState.FOCUSED
state, or if that is None
,
falls back to Theme.focus_color
.
label
#
label: StrOrControl | None = None
The clickable label to display on the right of a checkbox.
label_position
#
label_position: LabelPosition = RIGHT
Defines on which side of the checkbox the label
should be shown.
mouse_cursor
#
mouse_cursor: MouseCursor | None = None
The cursor to be displayed when a mouse pointer enters or is hovering over this checkbox.
Defaults to CheckboxTheme.mouse_cursor
,
or if that is None
, falls back to MouseCursor.CLICK
.
overlay_color
#
overlay_color: ControlStateValue[ColorValue] | None = None
The color of this checkbox's overlay in various ControlState
states.
Note
Supported states: ControlState.PRESSED
,
ControlState.SELECTED
, ControlState.HOVERED
,
ControlState.FOCUSED
, and ControlState.DEFAULT
.
semantics_label
#
semantics_label: str | None = None
The semantic label for the checkbox that is not shown in the UI, but will be announced by screen readers in accessibility modes (e.g TalkBack/VoiceOver).
shape
#
shape: OutlinedBorder | None = None
The shape of the checkbox.
Defaults to CheckboxTheme.shape
, or if that is None
,
falls back to RoundedRectangleBorder(radius=2)
.
splash_radius
#
splash_radius: Number | None = None
The radius of the circular Material ink response (ripple) in logical pixels.
Defaults to CheckboxTheme.splash_radius
,
or if that is None
, falls back to 20.0
.
value
#
value: bool | None = False
The value of this checkbox.
- If
True
, this checkbox is checked. - If
False
, this checkbox is unchecked. - If
None
andtristate
isTrue
, this checkbox is indeterminate (displayed as a dash).
visual_density
#
visual_density: VisualDensity | None = None
Defines how compact the checkbox's layout will be.
Events#
on_blur
#
on_blur: ControlEventHandler[Checkbox] | None = None
Called when this checkbox has lost focus.
on_change
#
on_change: ControlEventHandler[Checkbox] | None = None
Called when the state of this checkbox is changed.
on_focus
#
on_focus: ControlEventHandler[Checkbox] | None = None
Called when this checkbox has received focus.