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
@@ -41,6 +41,7 @@ class _DeviceListState extends State<DeviceList> with RouteAware {
int _currentPage = 0;
bool _hasNextPage = false;
CancelToken? _fetchToken;
final ScrollController _scrollController = ScrollController();
@override
void initState() {
@@ -69,9 +70,23 @@ class _DeviceListState extends State<DeviceList> with RouteAware {
_ownerDebounce?.cancel();
_fetchToken?.cancel();
_ownerController.dispose();
_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 row.
void _goToPage(int page) {
if (_scrollController.hasClients) {
_scrollController.animateTo(
0,
duration: const Duration(milliseconds: 300),
curve: Curves.easeOut,
);
}
_fetchPage(page);
}
void _onOwnerChanged() {
_ownerDebounce?.cancel();
_ownerDebounce = Timer(
@@ -286,6 +301,7 @@ class _DeviceListState extends State<DeviceList> with RouteAware {
return RefreshIndicator(
onRefresh: () => _fetchPage(_currentPage),
child: CustomScrollView(
controller: _scrollController,
physics: const AlwaysScrollableScrollPhysics(),
slivers: [
if (isWide)
@@ -323,7 +339,7 @@ class _DeviceListState extends State<DeviceList> with RouteAware {
currentPage: _currentPage,
hasNextPage: _hasNextPage,
isLoading: _isLoading,
onPageChanged: _fetchPage,
onPageChanged: _goToPage,
),
),
],