Challenge
For this intermediate challenge, figure out how to sync the current page of a PageView
with the selected item of a ListView
, as this demo shows:
Open preview in a separate page
If you want to see a real-world application of this challenge, check out the Flutter Tips web app.
Starter Project
You can grab the starter project for this challenge here:
c007_page_view_sync_list_view_starter.zip
v0.1.0, updated: 2025-02-12
Check the README for how to use it.
Primary Goals
Synchronize the PageView
on the left with the ListView
on the right, following these rules:
-
When the selected page changes in the
PageView
:- Update the selected item in the
ListView
- Scroll to the selected item so it is centered vertically in the
ListView
- Update the selected item in the
-
When an item is selected in the
ListView
:- Update the selected page in the
PageView
(no animation) - Scroll to the selected item so it is centered vertically in the
ListView
- Update the selected page in the
Hint about Reactive State
To solve the challenge, you need to share some state between the PageView
and the ListView
, and rebuild/listen to changes reactively. Which built-in Flutter classes can help you with that?
- Use
ValueNotifier
andValueListenableBuilder
🎯
What You Will Learn
- How to share reactive state between widgets
- How to work with
PageView
andPageController
- How to work with
AutoScrollController
from the scroll_to_index package
Useful Links and Resources
Official resources from Flutter.dev: