Skip to content

Commit f013da3

Browse files
All test cases pass now
1 parent 0725cfe commit f013da3

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

lib/widgets/message_list.dart

+13-18
Original file line numberDiff line numberDiff line change
@@ -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

pubspec.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -1044,10 +1044,10 @@ packages:
10441044
dependency: transitive
10451045
description:
10461046
name: stream_channel
1047-
sha256: "4ac0537115a24d772c408a2520ecd0abb99bca2ea9c4e634ccbdbfae64fe17ec"
1047+
sha256: "969e04c80b8bcdf826f8f16579c7b14d780458bd97f56d107d3950fdbeef059d"
10481048
url: "https://pub.dev"
10491049
source: hosted
1050-
version: "2.1.3"
1050+
version: "2.1.4"
10511051
stream_transform:
10521052
dependency: transitive
10531053
description:

0 commit comments

Comments
 (0)