Changes to initialDate for CalendarDatePicker should update the state. (#64115)
This commit is contained in:
@@ -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<CalendarDatePicker> {
|
||||
@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<CalendarDatePicker> {
|
||||
}
|
||||
}
|
||||
|
||||
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<CalendarDatePicker> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
assert(debugCheckHasMaterial(context));
|
||||
assert(debugCheckHasMaterialLocalizations(context));
|
||||
assert(debugCheckHasDirectionality(context));
|
||||
return Stack(
|
||||
children: <Widget>[
|
||||
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();
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user