Dropdown
Examples#
Color selection with filtering#
import flet as ft
def main(page: ft.Page):
def get_options():
colors = [
ft.Colors.RED,
ft.Colors.BLUE,
ft.Colors.YELLOW,
ft.Colors.PURPLE,
ft.Colors.LIME,
]
return [
ft.DropdownOption(
key=color.value,
content=ft.Text(value=color.value, color=color),
)
for color in colors
]
def handle_dropdown_change(e: ft.Event[ft.Dropdown]):
e.control.color = e.control.value
page.update()
page.add(
ft.Dropdown(
editable=True,
label="Color",
options=get_options(),
on_change=handle_dropdown_change,
)
)
ft.run(main)
Icon selection#
import flet as ft
def main(page: ft.Page):
def get_options():
icons = [
{"name": "Smile", "icon": ft.Icons.SENTIMENT_SATISFIED_OUTLINED},
{"name": "Cloud", "icon": ft.Icons.CLOUD_OUTLINED},
{"name": "Brush", "icon": ft.Icons.BRUSH_OUTLINED},
{"name": "Heart", "icon": ft.Icons.FAVORITE},
]
return [
ft.DropdownOption(key=icon["name"], leading_icon=icon["icon"])
for icon in icons
]
page.add(
ft.Dropdown(
border=ft.InputBorder.UNDERLINE,
enable_filter=True,
editable=True,
leading_icon=ft.Icons.SEARCH,
label="Icon",
options=get_options(),
)
)
ft.run(main)
Styled dropdowns#
import flet as ft
def main(page: ft.Page):
page.add(
# 1
ft.Dropdown(
text_size=20,
content_padding=10,
color=ft.Colors.PURPLE_200,
bgcolor=ft.Colors.BLUE_200,
filled=True,
border_radius=30,
border_color=ft.Colors.GREEN_800,
focused_border_color=ft.Colors.GREEN_ACCENT_400,
focused_border_width=5,
options=[
ft.DropdownOption("a", "Item A"),
ft.DropdownOption("b", "Item B"),
ft.DropdownOption("c", "Item C"),
],
),
# 2
ft.Dropdown(
border_radius=30,
filled=True,
fill_color=ft.Colors.RED_400,
border_color=ft.Colors.TRANSPARENT,
bgcolor=ft.Colors.RED_200,
color=ft.Colors.CYAN_400,
focused_border_color=ft.Colors.PINK_300,
focused_border_width=20,
options=[
ft.DropdownOption("a", "Item A"),
ft.DropdownOption("b", "Item B"),
ft.DropdownOption("c", "Item C"),
],
),
# 3
ft.Dropdown(
border_color=ft.Colors.PINK_ACCENT,
focused_border_color=ft.Colors.GREEN_ACCENT_400,
focused_border_width=25,
border_radius=30,
width=150,
border_width=5,
options=[
ft.DropdownOption("a", "Item A"),
ft.DropdownOption("b", "Item B"),
ft.DropdownOption("c", "Item C"),
],
),
# 4
ft.Container(
padding=ft.Padding.only(bottom=20),
content=ft.Dropdown(
text_size=30,
color=ft.Colors.ORANGE_ACCENT,
border_radius=20,
filled=True,
border_width=0,
autofocus=True,
focused_border_color=ft.Colors.GREEN_100,
focused_border_width=10,
width=200,
height=50,
options=[
ft.dropdown.Option("a", "Item A"),
ft.dropdown.Option("b", "Item B"),
ft.dropdown.Option("c", "Item C"),
],
),
),
# 5
ft.Dropdown(
text_size=30,
border_radius=20,
filled=True,
border_width=0,
focused_border_color=ft.Colors.GREEN_100,
focused_border_width=10,
content_padding=20,
width=200,
options=[
ft.DropdownOption(
key="a",
text="Item A",
style=ft.ButtonStyle(
shape=ft.BeveledRectangleBorder(radius=15),
color={
ft.ControlState.HOVERED: ft.Colors.WHITE,
ft.ControlState.FOCUSED: ft.Colors.BLUE,
ft.ControlState.DEFAULT: ft.Colors.BLACK,
},
),
),
ft.DropdownOption(
key="b",
text="Item B",
style=ft.ButtonStyle(
shape=ft.BeveledRectangleBorder(radius=15),
color={
ft.ControlState.HOVERED: ft.Colors.WHITE,
ft.ControlState.FOCUSED: ft.Colors.BLUE,
ft.ControlState.DEFAULT: ft.Colors.BLACK,
},
),
),
ft.DropdownOption(
key="c",
text="Item C",
style=ft.ButtonStyle(
shape=ft.BeveledRectangleBorder(radius=15),
color={
ft.ControlState.HOVERED: ft.Colors.WHITE,
ft.ControlState.FOCUSED: ft.Colors.BLUE,
ft.ControlState.DEFAULT: ft.Colors.BLACK,
},
),
),
],
),
)
ft.run(main)
Dropdown
#
Bases: ConstrainedControl
A dropdown control that allows users to select a single option from a list of options.
animate_offset
#
animate_offset: AnimationValue | None = None
animate_opacity
#
animate_opacity: AnimationValue | None = None
animate_position
#
animate_position: AnimationValue | None = None
animate_rotation
#
animate_rotation: AnimationValue | None = None
animate_scale
#
animate_scale: AnimationValue | None = None
autofocus
#
autofocus: bool = False
Whether 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.
bgcolor
#
bgcolor: ControlStateValue[ColorValue] | None = None
The background color of the dropdown menu
in various ControlState
states.
border_color
#
border_color: ColorValue | None = None
Border color. Could be transparent
to
hide the border.
border_width
#
border_width: Number = 1
The width of the border in virtual pixels. Set to 0
to completely remove border.
bottom
#
bottom: Number | None = None
The distance that the child's bottom edge is inset from the bottom of the stack.
Note
Effective only if this control is a descendant of one of the following:
Stack
control, Page.overlay
list.
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 |
content_padding
#
content_padding: PaddingValue | None = None
The padding for the input decoration's container.
dense
#
dense: bool = False
Whether the TextField is part of a dense form (i.e., uses less vertical space).
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.
elevation
#
elevation: ControlStateValue[Number | None] | None = 8
The dropdown's menu elevation in various ControlState
states.
enable_filter
#
enable_filter: bool = False
Determine if the menu list can be filtered by the text input. Defaults to false.
If set to true, dropdown menu will show a filtered list. The filtered list will contain items that match the text provided by the input field, with a case-insensitive comparison.
enable_search
#
enable_search: bool = True
Determine if the first item that matches the text input can be highlighted.
error_text
#
error_text: str | None = None
Text that appears below the input border.
If non-null, the border's color animates to red and the helper_text
is not shown.
expand
#
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.
fill_color
#
fill_color: ColorValue | None = None
Background color of the dropdown input text field.
Note
Will not be visible if filled=False
.
focused_border_color
#
focused_border_color: ColorValue | None = None
Border color in focused state.
helper_text
#
helper_text: str | None = None
Text that provides context about the input's value, such as how the value will be used.
If non-null, the text is displayed below the input decorator, in the same location
as error_text
. If a non-null error_text
value is specified then the helper text
is not shown.
hint_text
#
hint_text: str | None = None
Text that suggests what sort of input the field accepts.
Displayed on top of the input when it's empty and either (a) label
is null or (b)
the input has the focus.
hover_color
#
hover_color: ColorValue | None = None
The color of the dropdown input text field when hovered.
label
#
label: StrOrControl | None = None
Optional text that describes the input field.
When the input field is empty and unfocused, the label is displayed on top of the input field (i.e., at the same location on the screen where text may be entered in the input field). When the input field receives focus (or if the field is non-empty) the label moves above, either vertically adjacent to, or to the center of the input field.
leading_icon
#
leading_icon: IconValueOrControl | None = None
An optional Icon at the front of the text input field inside the decoration box.
If this is not null, the menu items will have extra paddings to be aligned with the text in the text field.
left
#
left: Number | None = None
The distance that the child's left edge is inset from the left of the stack.
Note
Effective only if this control is a descendant of one of the following:
Stack
control, Page.overlay
list.
menu_height
#
menu_height: Number | None = None
The height of the dropdown menu.
If this is None
, the menu will display as many
items as possible on the screen.
menu_width
#
menu_width: Number | None = None
The width of the dropdown menu.
If this is None
, the menu width will be the same as
input textfield width.
offset
#
offset: OffsetValue | None = None
Applies a translation transformation before painting the control.
The translation is expressed as an Offset
scaled to the control's size.
So, Offset(x=0.25, y=0)
, for example, will result in a horizontal translation
of one quarter the width of this control.
Example
The following example displays container at 0, 0
top left corner of a stack as
transform applies -1 * 100, -1 * 100
(offset * control's size
) horizontal and
vertical translations to the control:
on_animation_end
#
on_animation_end: (
ControlEventHandler[ConstrainedControl] | None
) = None
Called when animation completes.
Can be used to chain multiple animations.
The data
property of the event handler argument contains the name of the animation.
More information here.
on_blur
#
on_blur: ControlEventHandler[Dropdown] | None = None
Called when the control has lost focus.
on_change
#
on_change: ControlEventHandler[Dropdown] | None = None
Called when the selected item of this dropdown has changed.
on_focus
#
on_focus: ControlEventHandler[Dropdown] | None = None
Called when the control has received focus.
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).
options
#
options: list[DropdownOption] = field(default_factory=list)
A list of options to display in the dropdown.
page
#
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
.
right
#
right: Number | None = None
The distance that the child's right edge is inset from the right of the stack.
Note
Effective only if this control is a descendant of one of the following:
Stack
control, Page.overlay
list.
rotate
#
rotate: RotateValue | None = None
Transforms this control using a rotation around its center.
The value of rotate
property could be one of the following types:
number
- a rotation in clockwise radians. Full circle360°
ismath.pi * 2
radians,90°
ispi / 2
,45°
ispi / 4
, etc.Rotate
- allows to specify rotationangle
as well asalignment
- the location of rotation center.
scale
#
scale: ScaleValue | None = None
Scales this control along the 2D plane. Default scale factor is 1.0
, meaning no-scale.
Setting this property to 0.5
, for example, makes this control twice smaller, while 2.0
makes it twice larger.
Different scale multipliers can be specified for x
and y
axis, by setting
Control.scale
property to an instance of Scale
class.
Either scale
or scale_x
and scale_y
could be specified, but not all of them.
selected_trailing_icon
#
selected_trailing_icon: IconValueOrControl = ARROW_DROP_UP
An optional icon at the end of the text field to indicate that the text field is pressed.
tooltip
#
tooltip: TooltipValue | None = None
The tooltip ot show when this control is hovered over.
top
#
top: Number | None = None
The distance that the child's top edge is inset from the top of the stack.
Note
Effective only if this control is a descendant of one of the following:
Stack
control, Page.overlay
list.
trailing_icon
#
trailing_icon: IconValueOrControl = ARROW_DROP_DOWN
An icon to display at the end of the text field.
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.