Scroll to top of list when changing pages

The notifications and devices lists kept their scroll offset when paging,
so a new page would open partway down. Attach a ScrollController to each
CustomScrollView and animate back to the top whenever the page changes
via the pagination bar.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
rzuasti
2026-06-04 10:10:13 -04:00
co-authored by Claude Opus 4.8
parent aa5a29271b
commit aa5f65de2b
5 changed files with 131 additions and 4 deletions
+17 -1
View File
@@ -52,6 +52,7 @@ class _NotificationsListState extends State<NotificationsList>
bool _hasNextPage = false;
String? _error;
CancelToken? _fetchToken;
final ScrollController _scrollController = ScrollController();
@override
void initState() {
@@ -107,9 +108,23 @@ class _NotificationsListState extends State<NotificationsList>
routeObserver.unsubscribe(this);
_notificationTimer?.cancel();
_fetchToken?.cancel();
_scrollController.dispose();
super.dispose();
}
/// Fetches [page] and scrolls back to the top of the list, so changing pages
/// always starts the new page from its first item.
void _goToPage(int page) {
if (_scrollController.hasClients) {
_scrollController.animateTo(
0,
duration: const Duration(milliseconds: 300),
curve: Curves.easeOut,
);
}
_fetchPage(page);
}
Future<void> _fetchPage(int page) async {
_fetchToken?.cancel();
final token = CancelToken();
@@ -207,6 +222,7 @@ class _NotificationsListState extends State<NotificationsList>
_buildNotificationsHeader(context),
Expanded(
child: CustomScrollView(
controller: _scrollController,
slivers: [
..._buildNotificationSlivers(context),
...widget.trailingSlivers,
@@ -296,7 +312,7 @@ class _NotificationsListState extends State<NotificationsList>
currentPage: _currentPage,
hasNextPage: _hasNextPage,
isLoading: _isLoading,
onPageChanged: _fetchPage,
onPageChanged: _goToPage,
),
),
];