From 2735a661f98585c643caa17f3bd7ea672640cbe9 Mon Sep 17 00:00:00 2001 From: Darren Austin Date: Wed, 19 Aug 2020 13:36:06 -0700 Subject: [PATCH] Changes to initialDate for CalendarDatePicker should update the state. (#64115) --- .../pickers/calendar_date_picker.dart | 31 +++++++++- .../test/material/date_picker_test.dart | 56 +++++++++++++++++++ 2 files changed, 84 insertions(+), 3 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 ca61a2a8a9..faa253f46a 100644 --- a/packages/flutter/lib/src/material/pickers/calendar_date_picker.dart +++ b/packages/flutter/lib/src/material/pickers/calendar_date_picker.dart @@ -12,6 +12,7 @@ import 'package:flutter/services.dart'; import 'package:flutter/widgets.dart'; import '../color_scheme.dart'; +import '../debug.dart'; import '../divider.dart'; import '../icon_button.dart'; import '../icons.dart'; @@ -164,14 +165,21 @@ class _CalendarDatePickerState extends State { @override void initState() { super.initState(); - _mode = widget.initialCalendarMode; - _currentDisplayedMonthDate = DateTime(widget.initialDate.year, widget.initialDate.month); - _selectedDate = widget.initialDate; + _initWidgetState(); + } + + @override + void didUpdateWidget(CalendarDatePicker oldWidget) { + super.didUpdateWidget(oldWidget); + _initWidgetState(); } @override void didChangeDependencies() { super.didChangeDependencies(); + assert(debugCheckHasMaterial(context)); + assert(debugCheckHasMaterialLocalizations(context)); + assert(debugCheckHasDirectionality(context)); _localizations = MaterialLocalizations.of(context); _textDirection = Directionality.of(context); if (!_announcedInitialDate) { @@ -183,6 +191,12 @@ class _CalendarDatePickerState extends State { } } + void _initWidgetState() { + _mode = widget.initialCalendarMode; + _currentDisplayedMonthDate = DateTime(widget.initialDate.year, widget.initialDate.month); + _selectedDate = widget.initialDate; + } + void _vibrate() { switch (Theme.of(context).platform) { case TargetPlatform.android: @@ -281,6 +295,9 @@ class _CalendarDatePickerState extends State { @override Widget build(BuildContext context) { + assert(debugCheckHasMaterial(context)); + assert(debugCheckHasMaterialLocalizations(context)); + assert(debugCheckHasDirectionality(context)); return Stack( children: [ SizedBox( @@ -514,6 +531,14 @@ class _MonthPickerState extends State<_MonthPicker> { _textDirection = Directionality.of(context); } + @override + void didUpdateWidget(_MonthPicker oldWidget) { + super.didUpdateWidget(oldWidget); + if (widget.initialMonth != oldWidget.initialMonth) { + _showMonth(widget.initialMonth); + } + } + @override void dispose() { _pageController?.dispose(); diff --git a/packages/flutter/test/material/date_picker_test.dart b/packages/flutter/test/material/date_picker_test.dart index 2241bc8a9e..f94d7afcab 100644 --- a/packages/flutter/test/material/date_picker_test.dart +++ b/packages/flutter/test/material/date_picker_test.dart @@ -825,6 +825,62 @@ void main() { }); }); + group('CalendarDatePicker', () { + // Tests for the standalone CalendarDatePicker class + testWidgets('Updates to initialDate parameter is reflected in the state', (WidgetTester tester) async { + final Key pickerKey = UniqueKey(); + final DateTime initialDate = DateTime(2020, 1, 21); + final DateTime updatedDate = DateTime(1976, 2, 23); + const Color selectedColor = Color(0xff2196f3); // default primary color + + await tester.pumpWidget(MaterialApp( + home: Material( + child: CalendarDatePicker( + key: pickerKey, + initialDate: initialDate, + firstDate: DateTime(1970, 1, 1), + lastDate: DateTime(2099, 31, 12), + onDateChanged: (DateTime value) {}, + ), + ), + )); + await tester.pumpAndSettle(); + + // Month should show as January 2020 + expect(find.text('January 2020'), findsOneWidget); + // Selected date should be painted with a colored circle + expect( + Material.of(tester.element(find.text('21'))), + paints..circle(color: selectedColor, style: PaintingStyle.fill) + ); + + // Change to the updated initialDate + await tester.pumpWidget(MaterialApp( + home: Material( + child: CalendarDatePicker( + key: pickerKey, + initialDate: updatedDate, + firstDate: DateTime(1970, 1, 1), + lastDate: DateTime(2099, 31, 12), + onDateChanged: (DateTime value) {}, + ), + ), + )); + // Wait for the page scroll animation to finish + await tester.pumpAndSettle(const Duration(milliseconds: 200)); + + // Month should show as February 1976 + expect(find.text('January 2020'), findsNothing); + expect(find.text('February 1976'), findsOneWidget); + // Selected date should be painted with a colored circle + expect( + Material.of(tester.element(find.text('23'))), + paints..circle(color: selectedColor, style: PaintingStyle.fill) + ); + + }); + }); + group('Haptic feedback', () { const Duration hapticFeedbackInterval = Duration(milliseconds: 10); FeedbackTester feedback;