on_click event in ft.Container not works (on_hover works fine) #4061
-
Duplicate Check
Describe the bugWhen i use `# event: on click - works fine
on_click=lambda e: print('on_click_event') event not works. Similar lambda on event Code sampleComplete code, where problem will be shownimport flet as ft
def main(page: ft.Page):
page.vertical_alignment = ft.MainAxisAlignment.CENTER
page.horizontal_alignment = ft.CrossAxisAlignment.CENTER
_main_row = ft.Container(
expand=True,
content=ft.Row(
alignment=ft.MainAxisAlignment.START,
controls=[
],
),
)
_icons_list = [
ft.icons.FAVORITE_ROUNDED,
ft.icons.NOTIFICATION_ADD_ROUNDED,
]
for icon in _icons_list:
_main_row.content.controls.append(
ft.Container(
# event: on hover - works fine
on_hover=lambda e: print('on_hover_event'),
# event: on click - works fine
on_click=lambda e: print('on_click_event'),
content=ft.IconButton(
icon=icon,
icon_size=22,
style=ft.ButtonStyle(
color={"": "white"}),
)
)
)
_main_container = ft.Container(
width=280,
height=590,
bgcolor='black',
alignment=ft.alignment.bottom_center,
border_radius=35,
padding=20,
content=_main_row
)
page.add(_main_container)
page.update()
ft.app(main) To reproduce
Expected behaviorAccording to the documentation, the event should be executed Screenshots / VideosCaptures[Upload media here] Operating SystemLinux Operating system detailsXubuntu 24.04 Flet versionflet==0.24.1 RegressionNo, it isn't SuggestionsNo response LogsNo response Additional detailsNo response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
on_click doesn't work for those two containers because the content of each container is IconButton and when you click on it, it would trigger on_click event for IconButton, not Container. You can specify on_click for the IconButton:
If you want to make sure that on_click event works for Container, try it for the
|
Beta Was this translation helpful? Give feedback.
-
@LukasSliacky I understood your problem. See when you place a widget in a container, that child widget acquires entire width and height of parent widget. Same happened in this case, when you placed an import flet as ft
def main(page: ft.Page):
page.vertical_alignment = ft.MainAxisAlignment.CENTER
page.horizontal_alignment = ft.CrossAxisAlignment.CENTER
_main_row = ft.Container(
expand=True,
content=ft.Row(
alignment=ft.MainAxisAlignment.START,
controls=[],
),
)
_icons_list = [
ft.icons.FAVORITE_ROUNDED,
ft.icons.NOTIFICATION_ADD_ROUNDED,
]
for icon in _icons_list:
_main_row.content.controls.append(
ft.Container(
width=100,
height=100,
bgcolor="red",
padding=20, #gave the padding to lower icon size
# event: on hover - works fine
on_hover=lambda e: print("on_hover_event"),
# event: on click - works fine
on_click=lambda e: print("on_click_event"),
content=ft.IconButton(
width=25,
height=25,
icon=icon,
icon_size=10,
style=ft.ButtonStyle(color={"": "white"}),
bgcolor="purple",
),
)
)
_main_container = ft.Container(
width=280,
height=590,
bgcolor="black",
alignment=ft.alignment.bottom_center,
border_radius=35,
padding=20,
content=_main_row,
on_click=lambda e: print("on_click_event"),
)
page.add(_main_container)
page.update()
ft.app(main) One tip: Don't use for loops for appending any control if each of them going to have different functionality. |
Beta Was this translation helpful? Give feedback.
on_click doesn't work for those two containers because the content of each container is IconButton and when you click on it, it would trigger on_click event for IconButton, not Container.
You can specify on_click for the IconButton:
If you want to make sure that on_click event works for Container, try it for the
_main_container
: