@@ -489,21 +489,23 @@ class _MessageListState extends State<MessageList> with PerAccountStoreAwareStat
489489 }
490490
491491 void _modelChanged () {
492- if (model! .narrow != widget.narrow) {
493- // A message move event occurred, where propagate mode is
494- // [PropagateMode.changeAll] or [PropagateMode.changeLater].
495- widget.onNarrowChanged (model! .narrow);
496- }
497-
498492 final previousLength = oldItems.length + newItems.length;
499-
500493 setState (() {
494+ // Update both slivers with the new message positions
501495 oldItems = model! .items.sublist (0 , model! .anchorIndex+ 1 );
502496 newItems = model! .items.sublist (model! .anchorIndex+ 1 , model! .items.length);
503497 // The actual state lives in the [MessageListView] model.
504498 // This method was called because that just changed.
505499 });
506500
501+ if (model! .narrow != widget.narrow) {
502+ // A message move event occurred, where propagate mode is
503+ // [PropagateMode.changeAll] or [PropagateMode.changeLater].
504+ widget.onNarrowChanged (model! .narrow);
505+ return ; // Let the parent widget handle the rebuild with new narrow
506+ }
507+
508+
507509
508510 // Auto-scroll when new messages arrive if we're already near the bottom
509511 if (model! .items.length > previousLength && // New messages were added
@@ -534,7 +536,6 @@ class _MessageListState extends State<MessageList> with PerAccountStoreAwareStat
534536 36 , //Scroll 36 px inside bottomSliver.The sizedBox is 36px high. so theres no chance of overscrolling
535537 duration: Duration (milliseconds: durationMs),
536538 curve: Curves .easeIn);
537- await Future <void >.delayed (const Duration (milliseconds: 50 ));
538539 }
539540
540541 // Wait for the layout to settle so scrollController.position.pixels is updated properly
@@ -544,13 +545,12 @@ class _MessageListState extends State<MessageList> with PerAccountStoreAwareStat
544545 // If we go too fast, we'll overscroll.as
545546
546547 // After scroling to the bottom sliver, scroll to the bottom of the bottomSliver if we're not already there
547- while (distanceToBottom > 36 ) {
548+ while (distanceToBottom > 36 && context.mounted ) {
548549 await scrollController.animateTo (
549550 scrollController.position.maxScrollExtent,
550551 duration: Duration (milliseconds: durationMsToBottom),
551552 curve: Curves .ease);
552553 distanceToBottom = scrollController.position.maxScrollExtent - scrollController.position.pixels;
553- await Future <void >.delayed (const Duration (milliseconds: 50 ));
554554 }
555555
556556 }
@@ -781,7 +781,7 @@ class ScrollToBottomButton extends StatelessWidget {
781781 final ValueNotifier <bool > visibleValue;
782782 final ScrollController scrollController;
783783
784- Future <void > _navigateToBottom () async {
784+ Future <void > _navigateToBottom (BuildContext context ) async {
785785 // Calculate initial scroll parameters
786786 final distanceToCenter = scrollController.position.pixels;
787787 final durationMsAtSpeedLimit = (1000 * distanceToCenter / 8000 ).ceil ();
@@ -796,23 +796,18 @@ class ScrollToBottomButton extends StatelessWidget {
796796
797797
798798 // Wait for the layout to settle so scrollController.position.pixels is updated properly
799- await Future <void >.delayed (const Duration (milliseconds: 50 ));
800799
801800 var distanceToBottom = scrollController.position.maxScrollExtent - scrollController.position.pixels;
802801 final durationMsToBottom = math.min (1000 , (1200 * distanceToBottom / 8000 ).ceil ());
803802 // If we go too fast, we'll overscroll.
804803 // After scroling to the bottom sliver, scroll to the bottom of the bottomSliver if we're not already there
805- var count = 0 ;
806- while (distanceToBottom > 36 ) {
804+ while (distanceToBottom > 36 && context.mounted) {
807805 await scrollController.animateTo (
808806 scrollController.position.maxScrollExtent,
809807 duration: Duration (milliseconds: durationMsToBottom),
810808 curve: Curves .easeOut);
811- await Future <void >.delayed (const Duration (milliseconds: 50 ));
812809 distanceToBottom = scrollController.position.maxScrollExtent - scrollController.position.pixels;
813- count++ ;
814810 }
815- print ("count: $count " );
816811 }
817812
818813 @override
@@ -829,7 +824,7 @@ class ScrollToBottomButton extends StatelessWidget {
829824 iconSize: 40 ,
830825 // Web has the same color in light and dark mode.
831826 color: const HSLColor .fromAHSL (0.5 , 240 , 0.96 , 0.68 ).toColor (),
832- onPressed: _navigateToBottom));
827+ onPressed: () => _navigateToBottom (context) ));
833828 }
834829}
835830
0 commit comments