From bf3e2e4b46e807bf0d994e9ea64ccf81a376275e Mon Sep 17 00:00:00 2001 From: Micah Morrison Date: Fri, 13 Dec 2024 11:01:44 -0500 Subject: [PATCH] Fix an issue with comment scrolling offset --- lib/post/pages/legacy_post_page.dart | 3 +++ lib/post/pages/post_page.dart | 2 ++ lib/shared/comment_navigator_fab.dart | 28 +++++++++++++++++++++++---- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/lib/post/pages/legacy_post_page.dart b/lib/post/pages/legacy_post_page.dart index 0e0e2dc8e..705464787 100644 --- a/lib/post/pages/legacy_post_page.dart +++ b/lib/post/pages/legacy_post_page.dart @@ -99,6 +99,8 @@ class _PostPageState extends State { final theme = Theme.of(context); final ThunderState thunderState = context.watch().state; final AppLocalizations l10n = AppLocalizations.of(context)!; + final double statusBarHeight = MediaQuery.of(context).padding.top; + enableFab = thunderState.enablePostsFab; bool enableBackToTop = thunderState.postFabEnableBackToTop; @@ -267,6 +269,7 @@ class _PostPageState extends State { maxIndex: state.comments.length, scrollController: _scrollController, listController: _listController, + statusBarHeight: thunderState.hideTopBarOnScroll ? statusBarHeight : 0, ), ), ), diff --git a/lib/post/pages/post_page.dart b/lib/post/pages/post_page.dart index 11f62c99f..a01f78f86 100644 --- a/lib/post/pages/post_page.dart +++ b/lib/post/pages/post_page.dart @@ -113,6 +113,7 @@ class _PostPageState extends State { final theme = Theme.of(context); final l10n = AppLocalizations.of(context)!; final thunderState = context.read().state; + final double statusBarHeight = MediaQuery.of(context).padding.top; originalUser ??= context.read().state.account; @@ -155,6 +156,7 @@ class _PostPageState extends State { scrollController: scrollController, listController: listController, comments: flattenedComments, + statusBarHeight: thunderState.hideTopBarOnScroll ? statusBarHeight : 0, ), ), ), diff --git a/lib/shared/comment_navigator_fab.dart b/lib/shared/comment_navigator_fab.dart index dddbafb83..13340ecd7 100644 --- a/lib/shared/comment_navigator_fab.dart +++ b/lib/shared/comment_navigator_fab.dart @@ -21,6 +21,9 @@ class CommentNavigatorFab extends StatefulWidget { /// The maximum index that can be scrolled to final int maxIndex; + /// The height of the OS status bar, needed to calculate an offset for scrolling comments to the top + final double statusBarHeight; + const CommentNavigatorFab({ super.key, this.initialIndex = 0, @@ -28,6 +31,7 @@ class CommentNavigatorFab extends StatefulWidget { required this.scrollController, required this.listController, this.comments, + required this.statusBarHeight, }); @override @@ -130,10 +134,14 @@ class _CommentNavigatorFabState extends State { setState(() => currentIndex = previousIndex); + // Calculate alignment + final double screenHeight = MediaQuery.of(context).size.height; + final double alignmentOffset = widget.statusBarHeight / screenHeight; + widget.listController.animateToItem( index: previousIndex, scrollController: widget.scrollController, - alignment: 0, + alignment: alignmentOffset, duration: (estimatedDistance) => const Duration(milliseconds: 450), curve: (estimatedDistance) => Curves.easeInOutCubicEmphasized, ); @@ -169,10 +177,14 @@ class _CommentNavigatorFabState extends State { setState(() => currentIndex = parentCommentIndex); + // Calculate alignment + final double screenHeight = MediaQuery.of(context).size.height; + final double alignmentOffset = widget.statusBarHeight / screenHeight; + widget.listController.animateToItem( index: parentCommentIndex, scrollController: widget.scrollController, - alignment: 0, + alignment: alignmentOffset, duration: (estimatedDistance) => const Duration(milliseconds: 450), curve: (estimatedDistance) => Curves.easeInOutCubicEmphasized, ); @@ -186,10 +198,14 @@ class _CommentNavigatorFabState extends State { setState(() => currentIndex = nextIndex); + // Calculate alignment + final double screenHeight = MediaQuery.of(context).size.height; + final double alignmentOffset = widget.statusBarHeight / screenHeight; + widget.listController.animateToItem( index: nextIndex, scrollController: widget.scrollController, - alignment: 0, + alignment: alignmentOffset, duration: (estimatedDistance) => const Duration(milliseconds: 450), curve: (estimatedDistance) => Curves.easeInOutCubicEmphasized, ); @@ -224,10 +240,14 @@ class _CommentNavigatorFabState extends State { setState(() => currentIndex = parentCommentIndex); + // Calculate alignment + final double screenHeight = MediaQuery.of(context).size.height; + final double alignmentOffset = widget.statusBarHeight / screenHeight; + widget.listController.animateToItem( index: parentCommentIndex, scrollController: widget.scrollController, - alignment: 0, + alignment: alignmentOffset, duration: (estimatedDistance) => const Duration(milliseconds: 450), curve: (estimatedDistance) => Curves.easeInOutCubicEmphasized, );