flet icon indicating copy to clipboard operation
flet copied to clipboard

bug: Making custom controls through the @control decorator does not allow you to set callback functions

Open Qye-Leisure opened this issue 6 months ago • 2 comments

Duplicate Check

  • [x] I have searched the opened issues and there are no duplicates

Describe the bug

When using @control decorator in a flet, when creating a custom control, an incoming event callback will directly report an error

Code sample

Code
import flet as ft
from typing import Optional,Callable

@ft.control
class ceshi(ft.Container):
    expand :bool | int | None=True
    bgcolor:ft.Colors=ft.Colors.BLUE_100
    click_callback: Optional[Callable] =  None

    def init(self):
        # self._click_callback,self.click_callback=self.click_callback,None
        self.on_click=self._on_click

    def _on_click(self,e):
        
        print(f"aaa==={e}")

        if self.click_callback is not None:
            self.click_callback(e)

        # if self._click_callback is not None:
        #     self._click_callback(e)

if __name__ == "__main__":
    def main(page:ft.Page):

        aaa=ceshi(
            click_callback=lambda e :print(f"click_callback_AAA====:{e}")
        )

        page.add( aaa )

    ft.run(main)

To reproduce

Run directly

Expected behavior

I hope that callback functions can be used normally, or that there is a way to use callback functions normally.

Screenshots / Videos

Captures

[Upload media here]

Image

Operating System

Windows

Operating system details

win11

Flet version

0.85

Regression

No, it isn't

Suggestions

No response

Logs

Logs
[Paste your logs here]

Additional details

No response

Qye-Leisure avatar May 10 '26 16:05 Qye-Leisure

import flet as ft
from typing import Any, Optional, Callable

def bind_function(obj: Any, functions: dict[str, Callable]):
    object.__setattr__(obj, '__bind_custom_functions', functions)
    return obj

def custom_function(f):
    _func_name = f.__name__
    def _func(self):
        if hasattr(self, '__bind_custom_functions'):
            functions = object.__getattribute__(self, '__bind_custom_functions')
            if _func_name in functions:
                return functions[_func_name]
        return None
    return _func

@ft.control
class Ceshi(ft.Container):

    @property
    @custom_function
    def click_callback(self) -> Optional[ft.ControlEventHandler[ft.Container]]:
        ...

    def init(self):
        self.bgcolor = ft.Colors.BLUE_300
        self.on_click = lambda e: \
            (self.click_callback if self.click_callback else lambda e: ...)(e)

def click_it(e: ft.Event[ft.Container]):
    print('click_it', e)

def main(page:ft.Page):
    item = bind_function(Ceshi(
        ft.Text("测试"),
    ), {
        'click_callback': click_it,
    })
    page.add( item )

if __name__ == "__main__":
    ft.run(main)

You don't want me to write like that either, right?

Qye-Leisure avatar May 10 '26 20:05 Qye-Leisure

Every widget/controller you create gets serialized and sent to Flutter, and Flutter then parses those params and renders the UI. That's the default behavior. In your case you're running the code purely in Python, so to turn off that default behavior and stop it from being sent to Flutter, you can pass skip_field to the class field. Literally like this:

from flet.controls.base_control import skip_field

@ft.control
class ceshi(ft.Container):
    expand: bool | int | None = True
    bgcolor: ft.Colors = ft.Colors.BLUE_100
    click_callback: Optional[Callable] = skip_field()

Or you can make your own field, same way it's done inside skip_field itself.

7576457 avatar Aug 07 '26 10:08 7576457