.update() for children causes ReorderableListView to forget item order
Duplicate Check
- [x] I have searched the opened issues and there are no duplicates
Describe the bug
When using ReorderableListView alongside Custom Controls as items, if any child controls within the custom control are updated, or the custom control itself is updated, the list view forgets the current index of the item, and refreshes back to the original order.
Code sample
import flet as ft
class Task(ft.Row):
def __init__(self, text):
super().__init__()
self.text_view = ft.Text(text)
self.text_edit = ft.TextField(text, visible=False)
self.edit_button = ft.IconButton(icon=ft.Icons.EDIT, on_click=self.edit)
self.save_button = ft.IconButton(
visible=False, icon=ft.Icons.SAVE, on_click=self.save
)
self.controls = [
ft.Checkbox(),
self.text_view,
self.text_edit,
self.edit_button,
self.save_button,
]
def edit(self, e):
self.edit_button.visible = False
self.save_button.visible = True
self.text_view.visible = False
self.text_edit.visible = True
self.update()
def save(self, e):
self.edit_button.visible = True
self.save_button.visible = False
self.text_view.visible = True
self.text_edit.visible = False
self.text_view.value = self.text_edit.value
self.update()
def main(page: ft.Page):
page.add(
ft.ReorderableListView(
[
Task('test 1'),
Task('test 2')
]
)
)
ft.app(main)
To reproduce
- Define a custom control class, a composite one. [Refer to docs]. Ensure that the custom control involves triggering the
.update()method in some way. - Use this custom control as a List Item in the ReorderableListView.
- Run the program.
- Reorder the list items.
- Try to trigger the
.update()method, in whatever way you've added the trigger to your code. In case of my code sample, editing the task should trigger the.update()method. - Notice the unintentional reordering of the list view, back to it's original order.
Expected behavior
The order of the list view should have been maintained. Only the item itself should have been updated, but it's position within the list view should've remained the same.
Screenshots / Videos
Videos
Operating System
Windows
Operating system details
Windows 11
Flet version
0.27.6
Regression
I'm not sure / I don't know
Suggestions
Perhaps add an internal list to the class, that tracks the changes made after each reorder, and preserve the order whenever .update() is called using the list as a reference.
Logs
Additional details
This bug only happens when .update() is called on a list item.
You can make a "crutch" to solve the problem
Details
def main(page: ft.Page):
def reorder(e):
rlv = page.controls[0] # ReorderableListView
rlv.controls.insert(e.new_index, rlv.controls.pop(e.old_index)) # reorder ReorderableListView controls
rlv.update()
page.add(
ft.ReorderableListView(
[
Task('test 1'),
Task('test 2')
],
on_reorder = reorder
)
)
You can make a "crutch" to solve the problem
Details
def main(page: ft.Page):
def reorder(e): rlv = page.controls[0] # ReorderableListView rlv.controls.insert(e.new_index, rlv.controls.pop(e.old_index)) # reorder ReorderableListView controls rlv.update() page.add( ft.ReorderableListView( [ Task('test 1'), Task('test 2') ], on_reorder = reorder ) )
I am aware that there are possible manual workarounds for this, but my motive with posting this issue, is for the hopeful removal of the very issue itself, so that such workarounds won't be needed anymore. But thanks so much for the help <33
The issue is fixed in the latest pre-release. Give it a try and let know.