Skip to content

Commit

Permalink
Fix an issue with comment scrolling offset
Browse files Browse the repository at this point in the history
  • Loading branch information
micahmo committed Dec 13, 2024
1 parent ad5280d commit bf3e2e4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
3 changes: 3 additions & 0 deletions lib/post/pages/legacy_post_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ class _PostPageState extends State<PostPage> {
final theme = Theme.of(context);
final ThunderState thunderState = context.watch<ThunderBloc>().state;
final AppLocalizations l10n = AppLocalizations.of(context)!;
final double statusBarHeight = MediaQuery.of(context).padding.top;

enableFab = thunderState.enablePostsFab;

bool enableBackToTop = thunderState.postFabEnableBackToTop;
Expand Down Expand Up @@ -267,6 +269,7 @@ class _PostPageState extends State<PostPage> {
maxIndex: state.comments.length,
scrollController: _scrollController,
listController: _listController,
statusBarHeight: thunderState.hideTopBarOnScroll ? statusBarHeight : 0,
),
),
),
Expand Down
2 changes: 2 additions & 0 deletions lib/post/pages/post_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class _PostPageState extends State<PostPage> {
final theme = Theme.of(context);
final l10n = AppLocalizations.of(context)!;
final thunderState = context.read<ThunderBloc>().state;
final double statusBarHeight = MediaQuery.of(context).padding.top;

originalUser ??= context.read<AuthBloc>().state.account;

Expand Down Expand Up @@ -155,6 +156,7 @@ class _PostPageState extends State<PostPage> {
scrollController: scrollController,
listController: listController,
comments: flattenedComments,
statusBarHeight: thunderState.hideTopBarOnScroll ? statusBarHeight : 0,
),
),
),
Expand Down
28 changes: 24 additions & 4 deletions lib/shared/comment_navigator_fab.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ 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,
this.maxIndex = 0,
required this.scrollController,
required this.listController,
this.comments,
required this.statusBarHeight,
});

@override
Expand Down Expand Up @@ -130,10 +134,14 @@ class _CommentNavigatorFabState extends State<CommentNavigatorFab> {

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,
);
Expand Down Expand Up @@ -169,10 +177,14 @@ class _CommentNavigatorFabState extends State<CommentNavigatorFab> {

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,
);
Expand All @@ -186,10 +198,14 @@ class _CommentNavigatorFabState extends State<CommentNavigatorFab> {

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,
);
Expand Down Expand Up @@ -224,10 +240,14 @@ class _CommentNavigatorFabState extends State<CommentNavigatorFab> {

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,
);
Expand Down

0 comments on commit bf3e2e4

Please sign in to comment.