Unverified Commit 53bda97c authored by Darren Austin's avatar Darren Austin Committed by GitHub

Expose current day as a parameter to showDatePicker. (#54978)

parent 3ccd7bbd
...@@ -77,6 +77,10 @@ class CalendarDatePicker extends StatefulWidget { ...@@ -77,6 +77,10 @@ class CalendarDatePicker extends StatefulWidget {
/// [initialDate] must be between [firstDate] and [lastDate] or equal to /// [initialDate] must be between [firstDate] and [lastDate] or equal to
/// one of them. /// one of them.
/// ///
/// [currentDate] represents the current day (i.e. today). This
/// date will be highlighted in the day grid. If null, the date of
/// `DateTime.now()` will be used.
///
/// If [selectableDayPredicate] is non-null, it must return `true` for the /// If [selectableDayPredicate] is non-null, it must return `true` for the
/// [initialDate]. /// [initialDate].
CalendarDatePicker({ CalendarDatePicker({
...@@ -84,6 +88,7 @@ class CalendarDatePicker extends StatefulWidget { ...@@ -84,6 +88,7 @@ class CalendarDatePicker extends StatefulWidget {
@required DateTime initialDate, @required DateTime initialDate,
@required DateTime firstDate, @required DateTime firstDate,
@required DateTime lastDate, @required DateTime lastDate,
DateTime currentDate,
@required this.onDateChanged, @required this.onDateChanged,
this.onDisplayedMonthChanged, this.onDisplayedMonthChanged,
this.initialCalendarMode = DatePickerMode.day, this.initialCalendarMode = DatePickerMode.day,
...@@ -94,6 +99,7 @@ class CalendarDatePicker extends StatefulWidget { ...@@ -94,6 +99,7 @@ class CalendarDatePicker extends StatefulWidget {
initialDate = utils.dateOnly(initialDate), initialDate = utils.dateOnly(initialDate),
firstDate = utils.dateOnly(firstDate), firstDate = utils.dateOnly(firstDate),
lastDate = utils.dateOnly(lastDate), lastDate = utils.dateOnly(lastDate),
currentDate = utils.dateOnly(currentDate ?? DateTime.now()),
assert(onDateChanged != null), assert(onDateChanged != null),
assert(initialCalendarMode != null), assert(initialCalendarMode != null),
super(key: key) { super(key: key) {
...@@ -124,6 +130,9 @@ class CalendarDatePicker extends StatefulWidget { ...@@ -124,6 +130,9 @@ class CalendarDatePicker extends StatefulWidget {
/// The latest allowable [DateTime] that the user can select. /// The latest allowable [DateTime] that the user can select.
final DateTime lastDate; final DateTime lastDate;
/// The [DateTime] representing today. It will be highlighted in the day grid.
final DateTime currentDate;
/// Called when the user selects a date in the picker. /// Called when the user selects a date in the picker.
final ValueChanged<DateTime> onDateChanged; final ValueChanged<DateTime> onDateChanged;
...@@ -243,7 +252,7 @@ class _CalendarDatePickerState extends State<CalendarDatePicker> { ...@@ -243,7 +252,7 @@ class _CalendarDatePickerState extends State<CalendarDatePicker> {
return _MonthPicker( return _MonthPicker(
key: _monthPickerKey, key: _monthPickerKey,
initialMonth: _currentDisplayedMonthDate, initialMonth: _currentDisplayedMonthDate,
currentDate: DateTime.now(), currentDate: widget.currentDate,
firstDate: widget.firstDate, firstDate: widget.firstDate,
lastDate: widget.lastDate, lastDate: widget.lastDate,
selectedDate: _selectedDate, selectedDate: _selectedDate,
...@@ -256,7 +265,7 @@ class _CalendarDatePickerState extends State<CalendarDatePicker> { ...@@ -256,7 +265,7 @@ class _CalendarDatePickerState extends State<CalendarDatePicker> {
padding: const EdgeInsets.only(top: _subHeaderHeight), padding: const EdgeInsets.only(top: _subHeaderHeight),
child: _YearPicker( child: _YearPicker(
key: _yearPickerKey, key: _yearPickerKey,
currentDate: DateTime.now(), currentDate: widget.currentDate,
firstDate: widget.firstDate, firstDate: widget.firstDate,
lastDate: widget.lastDate, lastDate: widget.lastDate,
initialDate: _currentDisplayedMonthDate, initialDate: _currentDisplayedMonthDate,
......
...@@ -45,6 +45,10 @@ const Duration _dialogSizeAnimationDuration = Duration(milliseconds: 200); ...@@ -45,6 +45,10 @@ const Duration _dialogSizeAnimationDuration = Duration(milliseconds: 200);
/// their dates are considered. Their time fields are ignored. They must all /// their dates are considered. Their time fields are ignored. They must all
/// be non-null. /// be non-null.
/// ///
/// The [currentDate] represents the current day (i.e. today). This
/// date will be highlighted in the day grid. If null, the date of
/// `DateTime.now()` will be used.
///
/// An optional [initialEntryMode] argument can be used to display the date /// An optional [initialEntryMode] argument can be used to display the date
/// picker in the [DatePickerEntryMode.calendar] (a calendar month grid) /// picker in the [DatePickerEntryMode.calendar] (a calendar month grid)
/// or [DatePickerEntryMode.input] (a text input field) mode. /// or [DatePickerEntryMode.input] (a text input field) mode.
...@@ -93,6 +97,7 @@ Future<DateTime> showDatePicker({ ...@@ -93,6 +97,7 @@ Future<DateTime> showDatePicker({
@required DateTime initialDate, @required DateTime initialDate,
@required DateTime firstDate, @required DateTime firstDate,
@required DateTime lastDate, @required DateTime lastDate,
DateTime currentDate,
DatePickerEntryMode initialEntryMode = DatePickerEntryMode.calendar, DatePickerEntryMode initialEntryMode = DatePickerEntryMode.calendar,
SelectableDayPredicate selectableDayPredicate, SelectableDayPredicate selectableDayPredicate,
String helpText, String helpText,
...@@ -141,6 +146,7 @@ Future<DateTime> showDatePicker({ ...@@ -141,6 +146,7 @@ Future<DateTime> showDatePicker({
initialDate: initialDate, initialDate: initialDate,
firstDate: firstDate, firstDate: firstDate,
lastDate: lastDate, lastDate: lastDate,
currentDate: currentDate,
initialEntryMode: initialEntryMode, initialEntryMode: initialEntryMode,
selectableDayPredicate: selectableDayPredicate, selectableDayPredicate: selectableDayPredicate,
helpText: helpText, helpText: helpText,
...@@ -184,6 +190,7 @@ class _DatePickerDialog extends StatefulWidget { ...@@ -184,6 +190,7 @@ class _DatePickerDialog extends StatefulWidget {
@required DateTime initialDate, @required DateTime initialDate,
@required DateTime firstDate, @required DateTime firstDate,
@required DateTime lastDate, @required DateTime lastDate,
DateTime currentDate,
this.initialEntryMode = DatePickerEntryMode.calendar, this.initialEntryMode = DatePickerEntryMode.calendar,
this.selectableDayPredicate, this.selectableDayPredicate,
this.cancelText, this.cancelText,
...@@ -200,6 +207,7 @@ class _DatePickerDialog extends StatefulWidget { ...@@ -200,6 +207,7 @@ class _DatePickerDialog extends StatefulWidget {
initialDate = utils.dateOnly(initialDate), initialDate = utils.dateOnly(initialDate),
firstDate = utils.dateOnly(firstDate), firstDate = utils.dateOnly(firstDate),
lastDate = utils.dateOnly(lastDate), lastDate = utils.dateOnly(lastDate),
currentDate = utils.dateOnly(currentDate ?? DateTime.now()),
assert(initialEntryMode != null), assert(initialEntryMode != null),
assert(initialCalendarMode != null), assert(initialCalendarMode != null),
super(key: key) { super(key: key) {
...@@ -230,6 +238,9 @@ class _DatePickerDialog extends StatefulWidget { ...@@ -230,6 +238,9 @@ class _DatePickerDialog extends StatefulWidget {
/// The latest allowable [DateTime] that the user can select. /// The latest allowable [DateTime] that the user can select.
final DateTime lastDate; final DateTime lastDate;
/// The [DateTime] representing today. It will be highlighted in the day grid.
final DateTime currentDate;
final DatePickerEntryMode initialEntryMode; final DatePickerEntryMode initialEntryMode;
/// Function to provide full control over which [DateTime] can be selected. /// Function to provide full control over which [DateTime] can be selected.
...@@ -382,6 +393,7 @@ class _DatePickerDialogState extends State<_DatePickerDialog> { ...@@ -382,6 +393,7 @@ class _DatePickerDialogState extends State<_DatePickerDialog> {
initialDate: _selectedDate, initialDate: _selectedDate,
firstDate: widget.firstDate, firstDate: widget.firstDate,
lastDate: widget.lastDate, lastDate: widget.lastDate,
currentDate: widget.currentDate,
onDateChanged: _handleDateChanged, onDateChanged: _handleDateChanged,
selectableDayPredicate: widget.selectableDayPredicate, selectableDayPredicate: widget.selectableDayPredicate,
initialCalendarMode: widget.initialCalendarMode, initialCalendarMode: widget.initialCalendarMode,
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import '../rendering/mock_canvas.dart';
import 'feedback_tester.dart'; import 'feedback_tester.dart';
void main() { void main() {
...@@ -12,6 +13,7 @@ void main() { ...@@ -12,6 +13,7 @@ void main() {
DateTime firstDate; DateTime firstDate;
DateTime lastDate; DateTime lastDate;
DateTime initialDate; DateTime initialDate;
DateTime today;
SelectableDayPredicate selectableDayPredicate; SelectableDayPredicate selectableDayPredicate;
DatePickerEntryMode initialEntryMode; DatePickerEntryMode initialEntryMode;
DatePickerMode initialCalendarMode; DatePickerMode initialCalendarMode;
...@@ -37,6 +39,7 @@ void main() { ...@@ -37,6 +39,7 @@ void main() {
firstDate = DateTime(2001, DateTime.january, 1); firstDate = DateTime(2001, DateTime.january, 1);
lastDate = DateTime(2031, DateTime.december, 31); lastDate = DateTime(2031, DateTime.december, 31);
initialDate = DateTime(2016, DateTime.january, 15); initialDate = DateTime(2016, DateTime.january, 15);
today = DateTime(2016, DateTime.january, 3);
selectableDayPredicate = null; selectableDayPredicate = null;
initialEntryMode = DatePickerEntryMode.calendar; initialEntryMode = DatePickerEntryMode.calendar;
initialCalendarMode = DatePickerMode.day; initialCalendarMode = DatePickerMode.day;
...@@ -75,6 +78,7 @@ void main() { ...@@ -75,6 +78,7 @@ void main() {
initialDate: initialDate, initialDate: initialDate,
firstDate: firstDate, firstDate: firstDate,
lastDate: lastDate, lastDate: lastDate,
currentDate: today,
selectableDayPredicate: selectableDayPredicate, selectableDayPredicate: selectableDayPredicate,
initialDatePickerMode: initialCalendarMode, initialDatePickerMode: initialCalendarMode,
initialEntryMode: initialEntryMode, initialEntryMode: initialEntryMode,
...@@ -504,7 +508,7 @@ void main() { ...@@ -504,7 +508,7 @@ void main() {
}); });
}); });
testWidgets('Can select initial date picker mode', (WidgetTester tester) async { testWidgets('Can select initial calendar picker mode', (WidgetTester tester) async {
initialDate = DateTime(2014, DateTime.january, 15); initialDate = DateTime(2014, DateTime.january, 15);
initialCalendarMode = DatePickerMode.year; initialCalendarMode = DatePickerMode.year;
await prepareDatePicker(tester, (Future<DateTime> date) async { await prepareDatePicker(tester, (Future<DateTime> date) async {
...@@ -517,6 +521,18 @@ void main() { ...@@ -517,6 +521,18 @@ void main() {
}); });
}); });
testWidgets('currentDate is highlighted', (WidgetTester tester) async {
today = DateTime(2016, 1, 2);
await prepareDatePicker(tester, (Future<DateTime> date) async {
await tester.pump();
const Color todayColor = Color(0xff2196f3); // default primary color
expect(
Material.of(tester.element(find.text('2'))),
// The current day should be painted with a circle outline
paints..circle(color: todayColor, style: PaintingStyle.stroke, strokeWidth: 1.0)
);
});
});
}); });
group('Input mode', () { group('Input mode', () {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment