From 812c189de8a2d09cb6f52dc1f5186b33f451f837 Mon Sep 17 00:00:00 2001 From: Darren Austin Date: Mon, 29 Jun 2020 10:03:02 -0700 Subject: [PATCH] Fixed a problem with date calculations that caused a test to fail in a non-US time zone. (#60396) --- .../material/pickers/calendar_date_picker.dart | 16 ++++++++-------- .../lib/src/material/pickers/date_utils.dart | 5 +++++ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/packages/flutter/lib/src/material/pickers/calendar_date_picker.dart b/packages/flutter/lib/src/material/pickers/calendar_date_picker.dart index b92eaf87ad..428a79f9ae 100644 --- a/packages/flutter/lib/src/material/pickers/calendar_date_picker.dart +++ b/packages/flutter/lib/src/material/pickers/calendar_date_picker.dart @@ -668,14 +668,14 @@ class _MonthPickerState extends State<_MonthPicker> { }); } - static const Map _directionOffset = { - TraversalDirection.up: Duration(days: -DateTime.daysPerWeek), - TraversalDirection.right: Duration(days: 1), - TraversalDirection.down: Duration(days: DateTime.daysPerWeek), - TraversalDirection.left: Duration(days: -1), + static const Map _directionOffset = { + TraversalDirection.up: -DateTime.daysPerWeek, + TraversalDirection.right: 1, + TraversalDirection.down: DateTime.daysPerWeek, + TraversalDirection.left: -1, }; - Duration _dayDirectionOffset(TraversalDirection traversalDirection, TextDirection textDirection) { + int _dayDirectionOffset(TraversalDirection traversalDirection, TextDirection textDirection) { // Swap left and right if the text direction if RTL if (textDirection == TextDirection.rtl) { if (traversalDirection == TraversalDirection.left) @@ -688,12 +688,12 @@ class _MonthPickerState extends State<_MonthPicker> { DateTime _nextDateInDirection(DateTime date, TraversalDirection direction) { final TextDirection textDirection = Directionality.of(context); - DateTime nextDate = date.toUtc().add(_dayDirectionOffset(direction, textDirection)); + DateTime nextDate = utils.addDaysToDate(date, _dayDirectionOffset(direction, textDirection)); while (!nextDate.isBefore(widget.firstDate) && !nextDate.isAfter(widget.lastDate)) { if (_isSelectable(nextDate)) { return nextDate; } - nextDate = nextDate.add(_dayDirectionOffset(direction, textDirection)); + nextDate = utils.addDaysToDate(nextDate, _dayDirectionOffset(direction, textDirection)); } return null; } diff --git a/packages/flutter/lib/src/material/pickers/date_utils.dart b/packages/flutter/lib/src/material/pickers/date_utils.dart index 44acaedd80..95d83aeeb1 100644 --- a/packages/flutter/lib/src/material/pickers/date_utils.dart +++ b/packages/flutter/lib/src/material/pickers/date_utils.dart @@ -72,6 +72,11 @@ DateTime addMonthsToMonthDate(DateTime monthDate, int monthsToAdd) { return DateTime(monthDate.year, monthDate.month + monthsToAdd); } +/// Returns a [DateTime] with the added number of days and no time set. +DateTime addDaysToDate(DateTime date, int days) { + return DateTime(date.year, date.month, date.day + days); +} + /// Computes the offset from the first day of the week that the first day of /// the [month] falls on. ///