From 8b889d3551f9d401a787925f2443f008cbd269e5 Mon Sep 17 00:00:00 2001 From: Michael Debertol Date: Fri, 12 Mar 2021 00:45:02 +0100 Subject: [PATCH] [DatePicker] Don't jump to the current month (#77739) --- .../src/material/calendar_date_picker.dart | 2 +- .../material/calendar_date_picker_test.dart | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/material/calendar_date_picker.dart b/packages/flutter/lib/src/material/calendar_date_picker.dart index ed10221a94..ef5d08e8a4 100644 --- a/packages/flutter/lib/src/material/calendar_date_picker.dart +++ b/packages/flutter/lib/src/material/calendar_date_picker.dart @@ -531,7 +531,7 @@ class _MonthPickerState extends State<_MonthPicker> { @override void didUpdateWidget(_MonthPicker oldWidget) { super.didUpdateWidget(oldWidget); - if (widget.initialMonth != oldWidget.initialMonth) { + if (widget.initialMonth != oldWidget.initialMonth && widget.initialMonth != _currentMonth) { // We can't interrupt this widget build with a scroll, so do it next frame WidgetsBinding.instance!.addPostFrameCallback( (Duration timeStamp) => _showMonth(widget.initialMonth, jump: true) diff --git a/packages/flutter/test/material/calendar_date_picker_test.dart b/packages/flutter/test/material/calendar_date_picker_test.dart index 4c2fdaffc7..df65060974 100644 --- a/packages/flutter/test/material/calendar_date_picker_test.dart +++ b/packages/flutter/test/material/calendar_date_picker_test.dart @@ -431,6 +431,26 @@ void main() { expect(find.text('2016'), findsNothing); // 2016 in year grid }); + testWidgets('Dragging more than half the width should not cause a jump', ( + WidgetTester tester) async { + await tester.pumpWidget(calendarDatePicker()); + await tester.pumpAndSettle(); + final TestGesture gesture = await tester.startGesture(tester.getCenter(find.byType(PageView))); + // This initial drag is required for the PageView to recognize the gesture, as it uses DragStartBehavior.start. + // It does not count towards the drag distance. + await gesture.moveBy(const Offset(100, 0)); + // Dragging for a bit less than half the width should reveal the previous month. + await gesture.moveBy(const Offset(800 / 2 - 1, 0)); + await tester.pumpAndSettle(); + expect(find.text('January 2016'), findsOneWidget); + expect(find.text('1'), findsNWidgets(2)); + // Dragging a bit over the half should still show both. + await gesture.moveBy(const Offset(2, 0)); + await tester.pumpAndSettle(); + expect(find.text('December 2015'), findsOneWidget); + expect(find.text('1'), findsNWidgets(2)); + }); + group('Keyboard navigation', () { testWidgets('Can toggle to year mode', (WidgetTester tester) async { await tester.pumpWidget(calendarDatePicker());