flet-dev / flet

Flet enables developers to easily build realtime web, mobile and desktop apps in Python. No frontend experience required.
https://flet.dev
Apache License 2.0
10.89k stars 419 forks source link

FAB on_click function triggering without click when code runs. #3964

Closed Dzheremi2 closed 1 week ago

Dzheremi2 commented 1 week ago

Duplicate Check

Describe the bug

I created a FAB and set its on_click method to my function.

    page.floating_action_button = ft.FloatingActionButton(
        icon=ft.icons.CHECK_CIRCLE_OUTLINE_OUTLINED,
        text="Start Sync",
        enable_feedback=True,
        on_click=change_to_sync(page)
    )

But every time I run my code, this function triggers without pressing the FAB

Code sample

Code ```python import flet as ft def main(page: ft.Page): def test(): for i in range(10): page.controls.append(ft.Text("Test Text", color=ft.colors.WHITE, size=100)) page.floating_action_button = ft.FloatingActionButton( icon=ft.icons.CHECK_CIRCLE_OUTLINE_OUTLINED, text="Test", enable_feedback=True, on_click=test() ) page.update() ft.app(main) ```

To reproduce

Just run this code with flet run [name].py

Expected behavior

test() function executes only when FAB pressed.

Screenshots / Videos

Captures https://github.com/user-attachments/assets/3fc6c25c-74b1-4b35-af66-7393ac7525d3

Operating System

Linux

Operating system details

Linux fedora 6.10.7-200.fc40.x86_64

Flet version

0.24.1

Regression

I'm not sure / I don't know

lk-itween commented 1 week ago

hey, bro. This is an incorrect usage. on_click may be set function name or using lambda instead of calling it example:

import flet as ft

def main(page: ft.Page):
    def test(e):
        for i in range(10):
            page.controls.append(ft.Text("Test Text", color=ft.colors.WHITE, size=100))
        page.update()

    page.floating_action_button = ft.FloatingActionButton(
        icon=ft.icons.CHECK_CIRCLE_OUTLINE_OUTLINED,
        text="Test",
        enable_feedback=True,
        on_click=test   # You shouldn't call it
    )
    page.update()

ft.app(main)
Dzheremi2 commented 1 week ago

hey, bro. This is an incorrect usage. on_click may be set function name or using lambda instead of calling it example:

import flet as ft

def main(page: ft.Page):
    def test(e):
        for i in range(10):
            page.controls.append(ft.Text("Test Text", color=ft.colors.WHITE, size=100))
        page.update()

    page.floating_action_button = ft.FloatingActionButton(
        icon=ft.icons.CHECK_CIRCLE_OUTLINE_OUTLINED,
        text="Test",
        enable_feedback=True,
        on_click=test   # You shouldn't call it
    )
    page.update()

ft.app(main)

Oh god, thank you so much. I've almost gave up.