From 9d2f575403e8b1914d323e918d1ddeaba4812c68 Mon Sep 17 00:00:00 2001 From: NikosTsesmelis <100851392+NikosTsesmelis@users.noreply.github.com> Date: Wed, 20 Jul 2022 02:37:35 +0300 Subject: [PATCH] Cupertino date picker fix for minDate, maxDate when using minuteInterval (#105958) --- .../lib/src/cupertino/date_picker.dart | 8 ++-- .../test/cupertino/date_picker_test.dart | 47 +++++++++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/packages/flutter/lib/src/cupertino/date_picker.dart b/packages/flutter/lib/src/cupertino/date_picker.dart index f735b75d33..3aedc8d672 100644 --- a/packages/flutter/lib/src/cupertino/date_picker.dart +++ b/packages/flutter/lib/src/cupertino/date_picker.dart @@ -946,11 +946,11 @@ class _CupertinoDatePickerDateTimeState extends State { if (minCheck || maxCheck) { // We have minCheck === !maxCheck. final DateTime targetDate = minCheck ? widget.minimumDate! : widget.maximumDate!; - _scrollToDate(targetDate, selectedDate); + _scrollToDate(targetDate, selectedDate, minCheck); } } - void _scrollToDate(DateTime newDate, DateTime fromDate) { + void _scrollToDate(DateTime newDate, DateTime fromDate, bool minCheck) { assert(newDate != null); SchedulerBinding.instance.addPostFrameCallback((Duration timestamp) { if (fromDate.year != newDate.year || fromDate.month != newDate.month || fromDate.day != newDate.day) { @@ -977,7 +977,9 @@ class _CupertinoDatePickerDateTimeState extends State { } if (fromDate.minute != newDate.minute) { - _animateColumnControllerToItem(minuteController, newDate.minute); + final double positionDouble = newDate.minute / widget.minuteInterval; + final int position = minCheck ? positionDouble.ceil() : positionDouble.floor(); + _animateColumnControllerToItem(minuteController, position); } }); } diff --git a/packages/flutter/test/cupertino/date_picker_test.dart b/packages/flutter/test/cupertino/date_picker_test.dart index 73ce7200bf..5e0326b1cd 100644 --- a/packages/flutter/test/cupertino/date_picker_test.dart +++ b/packages/flutter/test/cupertino/date_picker_test.dart @@ -1601,6 +1601,53 @@ void main() { // Text style should not return unresolved color. expect(paragraph.text.style!.color.toString().contains('UNRESOLVED'), isFalse); }); + + testWidgets('TimerPicker minDate - maxDate with minuteInterval', (WidgetTester tester) async { + late DateTime date; + final DateTime minimum = DateTime(2022, 6, 14, 3, 31); + final DateTime initial = DateTime(2022, 6, 14, 3, 40); + final DateTime maximum = DateTime(2022, 6, 14, 3, 49); + await tester.pumpWidget( + CupertinoApp( + home: Center( + child: SizedBox( + height: 400.0, + width: 400.0, + child: CupertinoDatePicker( + initialDateTime: initial, + minimumDate: minimum, + maximumDate: maximum, + minuteInterval: 5, + use24hFormat: true, + onDateTimeChanged: (DateTime newDate) { + date = newDate; + }, + ), + ), + ), + ), + ); + + // Drag picker minutes to min date + await tester.drag(find.text('40'), const Offset(0.0, 32.0), touchSlopY: 0.0, warnIfMissed: false); + await tester.pumpAndSettle(); + + // Returns to min date. + expect( + date, + DateTime(2022, 6, 14, 3, 35), + ); + + // Drag picker minutes to max date + await tester.drag(find.text('50'), const Offset(0.0, -64.0), touchSlopY: 0.0, warnIfMissed: false); + await tester.pumpAndSettle(); + + // Returns to max date. + expect( + date, + DateTime(2022, 6, 14, 3, 45), + ); + }); } Widget _buildPicker({