Skip to content

Commit a0f3a29

Browse files
gnpricechrisbobbe
authored andcommitted
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 17602d1 commit a0f3a29

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
import 'package:flutter/material.dart';
53
import 'package:flutter_color_models/flutter_color_models.dart';
@@ -696,14 +694,7 @@ class ScrollToBottomButton extends StatelessWidget {
696694
final MessageListScrollController scrollController;
697695

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

709700
@override

lib/widgets/scrolling.dart

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

168168
return !changed;
169169
}
170+
171+
/// Scroll the position smoothly to the end of the scrollable content.
172+
///
173+
/// This method only works well if [maxScrollExtent] is accurate
174+
/// and does not change during the animation.
175+
/// (For example, this works if there is no content in forward slivers,
176+
/// so that [maxScrollExtent] is always zero.)
177+
/// The animation will attempt to travel to the value [maxScrollExtent] had
178+
/// at the start of the animation, even if that ends up being more or less far
179+
/// than the actual extent of the content.
180+
void scrollToEnd() {
181+
final target = maxScrollExtent;
182+
final distance = target - pixels;
183+
final durationMsAtSpeedLimit = (1000 * distance / 8000).ceil();
184+
final durationMs = math.max(300, durationMsAtSpeedLimit);
185+
animateTo(
186+
target,
187+
duration: Duration(milliseconds: durationMs),
188+
curve: Curves.linear);
189+
}
170190
}
171191

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

0 commit comments

Comments
 (0)