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, );