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:
@@ -221,6 +221,14 @@ class _DateSelectorState extends State<_DateSelector> {
|
|||||||
|
|
||||||
double _offsetForIndex(int index) => index * (_pillWidth + _pillSpacing);
|
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() {
|
void _selectPreviousDay() {
|
||||||
final previousDay = widget.selectedDate.subtract(const Duration(days: 1));
|
final previousDay = widget.selectedDate.subtract(const Duration(days: 1));
|
||||||
final previousDayNormalized =
|
final previousDayNormalized =
|
||||||
@@ -250,7 +258,7 @@ class _DateSelectorState extends State<_DateSelector> {
|
|||||||
widget.onDateSelected(DateTime(today.year, today.month, today.day));
|
widget.onDateSelected(DateTime(today.year, today.month, today.day));
|
||||||
if (_scrollController.hasClients) {
|
if (_scrollController.hasClients) {
|
||||||
_scrollController.animateTo(
|
_scrollController.animateTo(
|
||||||
_offsetForIndex(_futureDays),
|
_centeredOffset(_futureDays),
|
||||||
duration: const Duration(milliseconds: 300),
|
duration: const Duration(milliseconds: 300),
|
||||||
curve: Curves.easeInOut,
|
curve: Curves.easeInOut,
|
||||||
);
|
);
|
||||||
@@ -261,6 +269,11 @@ class _DateSelectorState extends State<_DateSelector> {
|
|||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
_scrollController = ScrollController();
|
_scrollController = ScrollController();
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
if (!mounted) return;
|
||||||
|
final initialIndex = _indexForDate(widget.selectedDate);
|
||||||
|
_scrollController.jumpTo(_centeredOffset(initialIndex));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -270,7 +283,7 @@ class _DateSelectorState extends State<_DateSelector> {
|
|||||||
final newIndex = _indexForDate(widget.selectedDate);
|
final newIndex = _indexForDate(widget.selectedDate);
|
||||||
if (oldIndex != newIndex && _scrollController.hasClients) {
|
if (oldIndex != newIndex && _scrollController.hasClients) {
|
||||||
_scrollController.animateTo(
|
_scrollController.animateTo(
|
||||||
_offsetForIndex(newIndex),
|
_centeredOffset(newIndex),
|
||||||
duration: const Duration(milliseconds: 300),
|
duration: const Duration(milliseconds: 300),
|
||||||
curve: Curves.easeInOut,
|
curve: Curves.easeInOut,
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user