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 <noreply@anthropic.com>
This commit is contained in:
dbastrikin
2026-03-23 14:11:41 +02:00
parent 83594ed911
commit 7994355971

View File

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