From 5726a19b5a752a9972380e4d3bc34840fc1c0825 Mon Sep 17 00:00:00 2001 From: jon-zuka Date: Sat, 16 Nov 2024 23:30:34 +0100 Subject: [PATCH] fix(flutter_todos): use `ListView.builder` for improved performance (#4275) Co-authored-by: Felix Angelov --- .../view/todos_overview_page.dart | 51 ++++++++++--------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/examples/flutter_todos/lib/todos_overview/view/todos_overview_page.dart b/examples/flutter_todos/lib/todos_overview/view/todos_overview_page.dart index 3d63eea3129..97880af4302 100644 --- a/examples/flutter_todos/lib/todos_overview/view/todos_overview_page.dart +++ b/examples/flutter_todos/lib/todos_overview/view/todos_overview_page.dart @@ -100,31 +100,32 @@ class TodosOverviewView extends StatelessWidget { } return CupertinoScrollbar( - child: ListView( - children: [ - for (final todo in state.filteredTodos) - TodoListTile( - todo: todo, - onToggleCompleted: (isCompleted) { - context.read().add( - TodosOverviewTodoCompletionToggled( - todo: todo, - isCompleted: isCompleted, - ), - ); - }, - onDismissed: (_) { - context - .read() - .add(TodosOverviewTodoDeleted(todo)); - }, - onTap: () { - Navigator.of(context).push( - EditTodoPage.route(initialTodo: todo), - ); - }, - ), - ], + child: ListView.builder( + itemCount: state.filteredTodos.length, + itemBuilder: (_, index) { + final todo = state.filteredTodos.elementAt(index); + return TodoListTile( + todo: todo, + onToggleCompleted: (isCompleted) { + context.read().add( + TodosOverviewTodoCompletionToggled( + todo: todo, + isCompleted: isCompleted, + ), + ); + }, + onDismissed: (_) { + context + .read() + .add(TodosOverviewTodoDeleted(todo)); + }, + onTap: () { + Navigator.of(context).push( + EditTodoPage.route(initialTodo: todo), + ); + }, + ); + }, ), ); },