Skip to content

Commit 76c7b21

Browse files
committed
scroll [nfc]: Move scroll-to-bottom logic into our ScrollPosition subclass
This makes a more comfortable home for extending this logic, because it naturally uses a lot of ScrollPosition members. To make the logic fit in better on this class, also loosen its assumptions slightly, allowing maxScrollExtent to be nonzero. It's still expected not to change, though -- fixing that is a more complex job, and will come over the remainder of this commit series.
1 parent ad3d3a6 commit 76c7b21

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

lib/widgets/message_list.dart

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'dart:math';
2-
31
import 'package:collection/collection.dart';
42
// ignore: undefined_hidden_name // anticipates https://github.com/flutter/flutter/pull/164818
53
import 'package:flutter/material.dart' hide SliverPaintOrder;
@@ -697,14 +695,7 @@ class ScrollToBottomButton extends StatelessWidget {
697695
final MessageListScrollController scrollController;
698696

699697
void _scrollToBottom() {
700-
final target = 0.0;
701-
final distance = target - scrollController.position.pixels;
702-
final durationMsAtSpeedLimit = (1000 * distance / 8000).ceil();
703-
final durationMs = max(300, durationMsAtSpeedLimit);
704-
scrollController.animateTo(
705-
target,
706-
duration: Duration(milliseconds: durationMs),
707-
curve: Curves.linear);
698+
scrollController.position.scrollToEnd();
708699
}
709700

710701
@override

lib/widgets/scrolling.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,26 @@ class MessageListScrollPosition extends ScrollPositionWithSingleContext {
373373

374374
return !changed;
375375
}
376+
377+
/// Scroll the position smoothly to the end of the scrollable content.
378+
///
379+
/// This method only works well if [maxScrollExtent] is accurate
380+
/// and does not change during the animation.
381+
/// (For example, this works if there is no content in forward slivers,
382+
/// so that [maxScrollExtent] is always zero.)
383+
/// The animation will attempt to travel to the value [maxScrollExtent] had
384+
/// at the start of the animation, even if that ends up being more or less far
385+
/// than the actual extent of the content.
386+
void scrollToEnd() {
387+
final target = maxScrollExtent;
388+
final distance = target - pixels;
389+
final durationMsAtSpeedLimit = (1000 * distance / 8000).ceil();
390+
final durationMs = math.max(300, durationMsAtSpeedLimit);
391+
animateTo(
392+
target,
393+
duration: Duration(milliseconds: durationMs),
394+
curve: Curves.linear);
395+
}
376396
}
377397

378398
/// A version of [ScrollController] adapted for the Zulip message list.

0 commit comments

Comments
 (0)