@@ -60,11 +60,6 @@ const _landingHighlightDuration = Duration(seconds: 3);
6060const _landingHighlightDelay = Duration (milliseconds: 50 );
6161const _landingHighlightTransitionDuration = Duration (milliseconds: 300 );
6262const _landingHighlightOpacity = 0.12 ;
63- const _threadTailScrollTolerance = 0.5 ;
64-
65- // Keep the direct-position correction finite in case the viewport cannot
66- // expose its tail (for example, continuously changing media dimensions).
67- const _latestTailCorrectionLimit = 8 ;
6863
6964/// Full-screen thread detail page.
7065///
@@ -79,6 +74,10 @@ class ThreadDetailPage extends HookConsumerWidget {
7974 final bool isArchived;
8075 final String ? initialMessageId;
8176
77+ /// Overrides the tail jump only in deterministic lazy-layout tests.
78+ @visibleForTesting
79+ final bool Function ()? jumpThreadTailForTesting;
80+
8281 const ThreadDetailPage ({
8382 super .key,
8483 required this .threadHead,
@@ -88,6 +87,7 @@ class ThreadDetailPage extends HookConsumerWidget {
8887 required this .isMember,
8988 required this .isArchived,
9089 this .initialMessageId,
90+ this .jumpThreadTailForTesting,
9191 });
9292
9393 @override
@@ -252,9 +252,15 @@ class ThreadDetailPage extends HookConsumerWidget {
252252 final initialTailSettle = useMemoized (InitialThreadTailSettle .new );
253253 final isAtThreadTail = useState (true );
254254 final isNavigatingToThreadTail = useState (false );
255+ // Ordinary thread entry owns a tail-follow intent before lazy item
256+ // positions settle. Keep Latest suppressed through those initial layout
257+ // frames; a deep-link entry must expose it for its older target instead.
258+ final hidesLatestForInitialTailSettle = useState (initialMessageId == null );
259+ final hidesLatestForComposerTailCorrection = useState (false );
255260 final tailCorrectionInProgress = useRef (false );
256261 final tailCorrectionGeneration = useRef (0 );
257262 final activeThreadScrollPosition = useRef <ScrollPosition ?>(null );
263+ final composerHasFocus = useListenable (composerFocusNode).hasFocus;
258264 final viewportHeight = useListenable (listViewport.height).value;
259265 final previousViewportHeight = useRef (viewportHeight);
260266 final settledImeLift = usesFixedAndroidImeViewport
@@ -321,51 +327,34 @@ class ThreadDetailPage extends HookConsumerWidget {
321327 final trailingBoundary =
322328 1 - ((Grid .xs + timelineBottomInset) / viewportHeight) + 0.001 ;
323329 final lastMessageIndex = _threadTailIndex (replies.length);
324- return itemPositionsListener.itemPositions.value.any (
325- (position) =>
326- position.index == lastMessageIndex &&
327- position.itemTrailingEdge <= trailingBoundary,
330+ var tailItemIsLaidOut = false ;
331+ var tailItemIsVisible = false ;
332+ for (final item in itemPositionsListener.itemPositions.value) {
333+ if (item.index != lastMessageIndex) continue ;
334+ tailItemIsLaidOut = true ;
335+ tailItemIsVisible = item.itemTrailingEdge <= trailingBoundary;
336+ break ;
337+ }
338+ final position = activeThreadScrollPosition.value;
339+ return threadTailIsAtEffectiveEnd (
340+ tailIsLaidOut: tailItemIsLaidOut,
341+ tailIsVisible: tailItemIsVisible,
342+ extentAfter: position != null && position.hasContentDimensions
343+ ? position.extentAfter
344+ : null ,
328345 );
329346 }
330347
331- Widget trackActiveScrollPosition (Widget child) {
332- return Builder (
333- builder: (itemContext) {
334- activeThreadScrollPosition.value = Scrollable .of (
335- itemContext,
336- ).position;
337- return child;
338- },
339- );
340- }
348+ Widget trackActiveScrollPosition (Widget child) =>
349+ _trackActiveThreadScrollPosition (child, activeThreadScrollPosition);
341350
342- bool jumpActiveScrollPositionToTail () {
343- final position = activeThreadScrollPosition.value;
344- if (position == null || ! position.hasContentDimensions) return false ;
345- // Move the one active viewport to its exact end. Unlike indexed
346- // jumpTo/scrollTo, this does not reset or cross-fade through a second
347- // list, so iOS never exposes the intermediate top-of-thread frame.
348- position.jumpTo (position.maxScrollExtent);
349- return true ;
350- }
351+ bool jumpActiveScrollPositionToTail () => _jumpActiveThreadScrollToTail (
352+ activeThreadScrollPosition,
353+ jumpThreadTailForTesting,
354+ );
351355
352- Future <bool > animateActiveScrollPositionToTail () async {
353- final position = activeThreadScrollPosition.value;
354- if (position == null || ! position.hasContentDimensions) return false ;
355- if (MediaQuery .disableAnimationsOf (context)) {
356- position.jumpTo (position.maxScrollExtent);
357- return true ;
358- }
359- // Match the channel's visible Latest glide while moving only the active
360- // thread viewport. The indexed-list animation path can create a temporary
361- // second list for distant targets, which caused the old top-frame bounce.
362- await position.animateTo (
363- position.maxScrollExtent,
364- duration: jumpToLatestScrollDuration,
365- curve: jumpToLatestScrollCurve,
366- );
367- return true ;
368- }
356+ Future <bool > animateActiveScrollPositionToTail () =>
357+ _animateActiveThreadScrollToTail (context, activeThreadScrollPosition);
369358
370359 void finishThreadTailCorrection ({
371360 required bool revealViewport,
@@ -380,14 +369,14 @@ class ThreadDetailPage extends HookConsumerWidget {
380369 tailIntent.isDragging ||
381370 userOptedOutOfTailFollow.value) {
382371 tailCorrectionInProgress.value = false ;
372+ hidesLatestForComposerTailCorrection.value = false ;
383373 isNavigatingToThreadTail.value = false ;
384374 isAtThreadTail.value = threadTailIsVisible ();
385375 return ;
386376 }
387377 final reachedTail = threadTailIsVisible ();
388378 // Lazy children can revise maxScrollExtent for several frames. Keep
389379 // moving the same active position until the measured tail is visible;
390- // the cap only guards pathological layouts that never stabilize.
391380 if (! reachedTail && corrections < _latestTailCorrectionLimit) {
392381 jumpActiveScrollPositionToTail ();
393382 WidgetsBinding .instance.addPostFrameCallback (
@@ -401,19 +390,10 @@ class ThreadDetailPage extends HookConsumerWidget {
401390 return ;
402391 }
403392 tailCorrectionInProgress.value = false ;
393+ hidesLatestForComposerTailCorrection.value = false ;
404394 isNavigatingToThreadTail.value = false ;
405395 if (revealViewport) initialViewportReady.value = true ;
406- // Item positions can trail the ScrollPosition by a frame after an
407- // animated jump. Once the bounded lazy-layout correction is exhausted,
408- // trust an exact end-of-scroll position too: there is nowhere further
409- // for Latest to navigate, so leaving the control visible is misleading.
410- final position = activeThreadScrollPosition.value;
411- isAtThreadTail.value = threadTailCorrectionReachedEnd (
412- tailIsVisible: reachedTail,
413- extentAfter: position != null && position.hasContentDimensions
414- ? position.extentAfter
415- : null ,
416- );
396+ isAtThreadTail.value = reachedTail;
417397 }
418398
419399 void correctThreadTailInstantly () {
@@ -432,6 +412,7 @@ class ThreadDetailPage extends HookConsumerWidget {
432412
433413 void followThreadTailFromComposer () {
434414 if (userDragDetachedTailFollow.value) return ;
415+ hidesLatestForComposerTailCorrection.value = true ;
435416 initialTailSettle.abandon ();
436417 initialViewportReady.value = true ;
437418 tailIntent.endDrag ();
@@ -440,9 +421,20 @@ class ThreadDetailPage extends HookConsumerWidget {
440421 followsThreadTail.value = true ;
441422 final reachedTail = threadTailIsVisible ();
442423 isAtThreadTail.value = reachedTail;
443- if (! reachedTail) correctThreadTailInstantly ();
424+ if (reachedTail) {
425+ hidesLatestForComposerTailCorrection.value = false ;
426+ } else {
427+ correctThreadTailInstantly ();
428+ }
444429 }
445430
431+ useEffect (() {
432+ if (! composerHasFocus) {
433+ hidesLatestForComposerTailCorrection.value = false ;
434+ }
435+ return null ;
436+ }, [composerHasFocus]);
437+
446438 useEffect (
447439 () {
448440 void onPositionsChanged () {
@@ -863,6 +855,8 @@ class ThreadDetailPage extends HookConsumerWidget {
863855 child: _ThreadMessageList (
864856 viewport: listViewport,
865857 onUserScrollStart: () {
858+ hidesLatestForInitialTailSettle.value = false ;
859+ hidesLatestForComposerTailCorrection.value = false ;
866860 initialTailSettle.abandon ();
867861 initialViewportReady.value = true ;
868862 tailCorrectionInProgress.value = false ;
@@ -981,6 +975,9 @@ class ThreadDetailPage extends HookConsumerWidget {
981975 threadViewportVisible &&
982976 hasFetchedReplies &&
983977 ! isNavigatingToThreadTail.value &&
978+ ! hidesLatestForInitialTailSettle.value &&
979+ ! hidesLatestForComposerTailCorrection.value &&
980+ ! (composerHasFocus && ! userDragDetachedTailFollow.value) &&
984981 ! isAtThreadTail.value,
985982 onPressed: scrollToThreadLatest,
986983 ),
0 commit comments