Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The isolated/is_isolated() property does not work as intended #4920

Open
1 task done
stur86 opened this issue Feb 19, 2025 · 0 comments
Open
1 task done

The isolated/is_isolated() property does not work as intended #4920

stur86 opened this issue Feb 19, 2025 · 0 comments

Comments

@stur86
Copy link

stur86 commented Feb 19, 2025

Duplicate Check

Describe the bug

The documentation here suggests that controls designated as isolated should ignore their children when updated. This would be potentially useful when e.g. having components that are expensive to update and you want to schedule those updates manually explicitly.

https://flet.dev/docs/getting-started/custom-controls/#isolated-controls

Unfortunately it seems like this is not how it actually works. Here we see how if the isolated argument was passed, it would skip updating the children:

def build_update_commands(
self, index, commands, added_controls, removed_controls, isolated: bool = False
) -> None:
update_cmd = self._build_command(update=True)
if len(update_cmd.attrs) > 0:
update_cmd.name = "set"
commands.append(update_cmd)
if isolated:
return

And here we see how it's never set and thus always defaults to False:

for control in controls:
control.build_update_commands(
self._index, commands, added_controls, removed_controls
)

Also the control itself doesn't really make use of the isolated property:

def is_isolated(self) -> bool:
return False

It's an easy enough fix but I was wondering whether there was a reason for this. I've currently gone around it in my custom component by overriding build_update_commands, but that's obviously hackish.

Code sample

Code
import flet as ft

class UpdateCounter(ft.Text):

    def __init__(self):
        super().__init__()
        self.counter = 0
        self.value = f"Counter: {self.counter}"
        
    def before_update(self):
        self.counter += 1
        self.value = f"Counter: {self.counter}"
        return super().before_update()
    

class Widget(ft.AlertDialog):
    
    def __init__(self):
        super().__init__()
        self.title = ft.Text("Widget")
        close_btn = ft.IconButton(ft.icons.CLOSE, on_click=lambda e: self.hide())
        self.actions = [close_btn]        
        self.content = UpdateCounter()
    
    def is_isolated(self):
        # This actually does nothing
        return True
        
    def show(self):
        self.open = True
        self.update()
    
    def hide(self):
        self.open = False
        self.update()
        
    def build_update_commands(self, index, commands, added_controls, removed_controls, isolated=False):
        # This achieves the desired effect
        return super().build_update_commands(index, commands, added_controls, removed_controls, isolated=True)

def main(page: ft.Page):
    page.title = "Flet App"
    
    widget = Widget()
    btn = ft.FilledButton(text="Open", on_click=lambda e: widget.show())
    page.add(btn)
    page.add(widget)

if __name__ == "__main__":
    ft.app(target=main)

To reproduce

See the above code sample; try commenting out the override of the build_update_commands and note how the content of the widget keeps updating on each open/close.

Expected behavior

Components on which the .isolated property is set to True, or the .is_isolated() method returns True, should not update their children automatically.

Screenshots / Videos

Captures

[Upload media here]

Operating System

Linux

Operating system details

Ubuntu 22.04, on WSL2

Flet version

Observed in 0.22.1, persists as of 0.26.0

Regression

I'm not sure / I don't know

Suggestions

Simply change the code in page.py to:

        for control in controls:
            control.build_update_commands(
                self._index, commands, added_controls, removed_controls, isolated=control.is_isolated()
            )

Logs

Logs
[Paste your logs here]

Additional details

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant