In the code below, i created a Flet app in which the two text controls should update their number based on the amount of clicks on the button. Why is this not happening?
A little bit of background: this is a simplified version of my actual code in which i generate a rather complex layout (a grid) based on the values of a dictionary, depending on the dictionary this layout has more or less rows so i want to update the whole layout and the values within at once.
I tried numerous things and looked at the docs but i can't find any information on updating a layout.
import flet as ft
class MyWidget(ft.UserControl):
def __init__(self):
super().__init__()
self.data = 0
self.layout = []
def build_function(self):
self.layout = ft.Row(
[
ft.Text(f"Button clicked {self.data} time(s)"),
ft.Text(f"Button clicked {self.data} time(s)"),
]
)
print(self.layout)
def button_clicked(self, e):
self.data += 1
self.build_function()
print("data = ", self.data)
self.update()
def build(self):
self.build_function()
return ft.Column(
[
ft.ElevatedButton(
"Button with 'click' event", on_click=self.button_clicked
),
self.layout,
]
)
def main(page: ft.Page):
page.add(MyWidget())
ft.app(target=main)
You cannot update the controls that are already been placed on the layout, but you can rebuild them. If you update the ini and build_function like below, it should work: