From 79943559717ab9f2c162209fe1052afda3731de3 Mon Sep 17 00:00:00 2001 From: dbastrikin Date: Mon, 23 Mar 2026 14:11:41 +0200 Subject: [PATCH] fix: center selected date pill in home screen date strip On first open the ListView started at offset 0 (future end), leaving today's pill out of view. Also, navigation always scrolled the pill to the left edge instead of centering it. Added _centeredOffset() helper and a post-frame jumpTo in initState() so the initially selected date is centered immediately. Updated _jumpToToday() and didUpdateWidget() to use the same centering formula. Co-Authored-By: Claude Sonnet 4.6 --- client/lib/features/home/home_screen.dart | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/client/lib/features/home/home_screen.dart b/client/lib/features/home/home_screen.dart index 0a122c5..074dc81 100644 --- a/client/lib/features/home/home_screen.dart +++ b/client/lib/features/home/home_screen.dart @@ -221,6 +221,14 @@ class _DateSelectorState extends State<_DateSelector> { double _offsetForIndex(int index) => index * (_pillWidth + _pillSpacing); + double _centeredOffset(int index) { + final rawOffset = _offsetForIndex(index); + if (!_scrollController.hasClients) return rawOffset; + final viewportWidth = _scrollController.position.viewportDimension; + final centeredOffset = rawOffset - viewportWidth / 2 + _pillWidth / 2; + return centeredOffset < 0 ? 0 : centeredOffset; + } + void _selectPreviousDay() { final previousDay = widget.selectedDate.subtract(const Duration(days: 1)); final previousDayNormalized = @@ -250,7 +258,7 @@ class _DateSelectorState extends State<_DateSelector> { widget.onDateSelected(DateTime(today.year, today.month, today.day)); if (_scrollController.hasClients) { _scrollController.animateTo( - _offsetForIndex(_futureDays), + _centeredOffset(_futureDays), duration: const Duration(milliseconds: 300), curve: Curves.easeInOut, ); @@ -261,6 +269,11 @@ class _DateSelectorState extends State<_DateSelector> { void initState() { super.initState(); _scrollController = ScrollController(); + WidgetsBinding.instance.addPostFrameCallback((_) { + if (!mounted) return; + final initialIndex = _indexForDate(widget.selectedDate); + _scrollController.jumpTo(_centeredOffset(initialIndex)); + }); } @override @@ -270,7 +283,7 @@ class _DateSelectorState extends State<_DateSelector> { final newIndex = _indexForDate(widget.selectedDate); if (oldIndex != newIndex && _scrollController.hasClients) { _scrollController.animateTo( - _offsetForIndex(newIndex), + _centeredOffset(newIndex), duration: const Duration(milliseconds: 300), curve: Curves.easeInOut, );