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

fix(flutter_todos): fix scroll freeze by using ListView.builder instead. #4270

Closed
jon-zuka opened this issue Oct 29, 2024 · 4 comments · Fixed by #4275
Closed

fix(flutter_todos): fix scroll freeze by using ListView.builder instead. #4270

jon-zuka opened this issue Oct 29, 2024 · 4 comments · Fixed by #4275
Assignees
Labels
example Example application good first issue Good for newcomers

Comments

@jon-zuka
Copy link
Contributor

I fixed scroll freeze when I switched to using ListView.builder instead.

child: ListView(
children: [
for (final todo in state.filteredTodos)
TodoListTile(
todo: todo,
onToggleCompleted: (isCompleted) {
context.read<TodosOverviewBloc>().add(
TodosOverviewTodoCompletionToggled(
todo: todo,
isCompleted: isCompleted,
),
);
},
onDismissed: (_) {
context
.read<TodosOverviewBloc>()
.add(TodosOverviewTodoDeleted(todo));
},
onTap: () {
Navigator.of(context).push(
EditTodoPage.route(initialTodo: todo),
);
},
),
],
),

@felangel felangel added good first issue Good for newcomers example Example application labels Nov 4, 2024
@felangel
Copy link
Owner

felangel commented Nov 4, 2024

Thanks for catching that! Would you like to open a PR to fix that?

@jon-zuka
Copy link
Contributor Author

jon-zuka commented Nov 5, 2024

Hello felangel! My changes are minimal and should not break anything. However, I only tested it on my NixOS machine, which is of course Linux.

Keep up the good work! 👍🏻

@xoliq0v
Copy link

xoliq0v commented Nov 13, 2024

The scroll freeze issue has been resolved by replacing ListView with ListView.builder. The builder constructor optimizes performance by only building the visible items in the list, which is particularly effective for large datasets.

Updated code snippet:

child: ListView.builder(
  itemCount: state.filteredTodos.length,
  itemBuilder: (context, index) {
    final todo = state.filteredTodos[index];
    return TodoListTile(
      todo: todo,
      onToggleCompleted: (isCompleted) {
        context.read<TodosOverviewBloc>().add(
              TodosOverviewTodoCompletionToggled(
                todo: todo,
                isCompleted: isCompleted,
              ),
            );
      },
      onDismissed: (_) {
        context.read<TodosOverviewBloc>().add(
              TodosOverviewTodoDeleted(todo),
            );
      },
      onTap: () {
        Navigator.of(context).push(
          EditTodoPage.route(initialTodo: todo),
        );
      },
    );
  },
),

Explanation:

•	Performance Improvement: ListView.builder only renders visible items, reducing memory usage and preventing freezes during scrolling.
•	Code Adjustments: The itemCount and itemBuilder properties allow ListView.builder to iterate over state.filteredTodos, creating each TodoListTile dynamically.

This change should help maintain smooth scrolling and improve the overall performance of the TodosOverviewPage. Let me know if there are any further issues!

@jon-zuka
Copy link
Contributor Author

@xoliq0v Thank you, can you please review my pull request?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
example Example application good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants