Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
dbaa4c23
Unverified
Commit
dbaa4c23
authored
Jan 29, 2020
by
LongCatIsLooong
Committed by
GitHub
Jan 29, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix date picker range validation (#49637)
parent
5f868fc9
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
56 additions
and
15 deletions
+56
-15
date_picker.dart
packages/flutter/lib/src/cupertino/date_picker.dart
+23
-15
date_picker_test.dart
packages/flutter/test/cupertino/date_picker_test.dart
+33
-0
No files found.
packages/flutter/lib/src/cupertino/date_picker.dart
View file @
dbaa4c23
...
...
@@ -180,8 +180,8 @@ enum _PickerColumnType {
///
/// Example of the picker in date mode:
///
/// * US-English: `
July | 13 | 2012
`
/// * Vietnamese: `
13 | Tháng 7 | 2012
`
/// * US-English: `
| July | 13 | 2012 |
`
/// * Vietnamese: `
| 13 | Tháng 7 | 2012 |
`
///
/// Can be used with [showCupertinoModalPopup] to display the picker modally at
/// the bottom of the screen.
...
...
@@ -202,7 +202,9 @@ class CupertinoDatePicker extends StatefulWidget {
///
/// [onDateTimeChanged] is the callback called when the selected date or time
/// changes and must not be null. When in [CupertinoDatePickerMode.time] mode,
/// the year, month and day will be the same as [initialDateTime].
/// the year, month and day will be the same as [initialDateTime]. When in
/// [CupertinoDatePickerMode.date] mode, this callback will always report the
/// start time of the currently selected day.
///
/// [initialDateTime] is the initial date time of the picker. Defaults to the
/// present date and time and must not be null. The present must conform to
...
...
@@ -317,8 +319,11 @@ class CupertinoDatePicker extends StatefulWidget {
/// Whether to use 24 hour format. Defaults to false.
final
bool
use24hFormat
;
/// Callback called when the selected date and/or time changes. Must not be
/// null.
/// Callback called when the selected date and/or time changes. If the new selected
/// [DateTime] is not valid, or is not in the [minimumDate] through [maximumDate]
/// range, this callback will not be called.
///
/// Must not be null.
final
ValueChanged
<
DateTime
>
onDateTimeChanged
;
/// Background color of date picker.
...
...
@@ -1215,12 +1220,14 @@ class _CupertinoDatePickerDateState extends State<CupertinoDatePicker> {
}
bool
get
_isCurrentDateValid
{
final
DateTime
selectedDate
=
DateTime
(
selectedYear
,
selectedMonth
,
selectedDay
);
// The current date selection represents a range [minSelectedData, maxSelectDate].
final
DateTime
minSelectedDate
=
DateTime
(
selectedYear
,
selectedMonth
,
selectedDay
);
final
DateTime
maxSelectedDate
=
DateTime
(
selectedYear
,
selectedMonth
,
selectedDay
+
1
);
final
bool
minCheck
=
widget
.
minimumDate
?.
is
After
(
selectedDate
)
??
fals
e
;
final
bool
maxCheck
=
widget
.
maximumDate
?.
isBefore
(
s
electedDate
)
??
false
;
final
bool
minCheck
=
widget
.
minimumDate
?.
is
Before
(
maxSelectedDate
)
??
tru
e
;
final
bool
maxCheck
=
widget
.
maximumDate
?.
isBefore
(
minS
electedDate
)
??
false
;
return
!
minCheck
&&
!
maxCheck
&&
s
electedDate
.
day
==
selectedDay
;
return
minCheck
&&
!
maxCheck
&&
minS
electedDate
.
day
==
selectedDay
;
}
// One or more pickers have just stopped scrolling.
...
...
@@ -1235,21 +1242,22 @@ class _CupertinoDatePickerDateState extends State<CupertinoDatePicker> {
// Whenever scrolling lands on an invalid entry, the picker
// automatically scrolls to a valid one.
final
DateTime
selectedDate
=
DateTime
(
selectedYear
,
selectedMonth
,
selectedDay
);
final
DateTime
minSelectDate
=
DateTime
(
selectedYear
,
selectedMonth
,
selectedDay
);
final
DateTime
maxSelectDate
=
DateTime
(
selectedYear
,
selectedMonth
,
selectedDay
+
1
);
final
bool
minCheck
=
widget
.
minimumDate
?.
is
After
(
selectedDate
)
??
fals
e
;
final
bool
maxCheck
=
widget
.
maximumDate
?.
isBefore
(
selected
Date
)
??
false
;
final
bool
minCheck
=
widget
.
minimumDate
?.
is
Before
(
maxSelectDate
)
??
tru
e
;
final
bool
maxCheck
=
widget
.
maximumDate
?.
isBefore
(
minSelect
Date
)
??
false
;
if
(
minCheck
||
maxCheck
)
{
if
(
!
minCheck
||
maxCheck
)
{
// We have minCheck === !maxCheck.
final
DateTime
targetDate
=
minCheck
?
widget
.
m
inimumDate
:
widget
.
max
imumDate
;
final
DateTime
targetDate
=
minCheck
?
widget
.
m
aximumDate
:
widget
.
min
imumDate
;
_scrollToDate
(
targetDate
);
return
;
}
// Some months have less days (e.g. February). Go to the last day of that month
// if the selectedDay exceeds the maximum.
if
(
selected
Date
.
day
!=
selectedDay
)
{
if
(
minSelect
Date
.
day
!=
selectedDay
)
{
final
DateTime
lastDay
=
_lastDayInMonth
(
selectedYear
,
selectedMonth
);
_scrollToDate
(
lastDay
);
}
...
...
packages/flutter/test/cupertino/date_picker_test.dart
View file @
dbaa4c23
...
...
@@ -935,6 +935,39 @@ void main() {
);
});
testWidgets
(
'date picker should only take into account the date part of minimumDate and maximumDate'
,
(
WidgetTester
tester
)
async
{
// Regression test for https://github.com/flutter/flutter/issues/49606.
DateTime
date
;
final
DateTime
minDate
=
DateTime
(
2020
,
1
,
1
,
12
);
await
tester
.
pumpWidget
(
CupertinoApp
(
home:
Center
(
child:
SizedBox
(
height:
400.0
,
width:
400.0
,
child:
CupertinoDatePicker
(
mode:
CupertinoDatePickerMode
.
date
,
minimumDate:
minDate
,
onDateTimeChanged:
(
DateTime
newDate
)
{
date
=
newDate
;
},
initialDateTime:
DateTime
(
2020
,
1
,
12
),
),
),
),
),
);
// Scroll to 2019.
await
tester
.
drag
(
find
.
text
(
'2020'
),
const
Offset
(
0.0
,
32.0
),
touchSlopY:
0.0
);
await
tester
.
pump
();
await
tester
.
pumpAndSettle
();
expect
(
date
.
year
,
minDate
.
year
);
expect
(
date
.
month
,
minDate
.
month
);
expect
(
date
.
day
,
minDate
.
day
);
});
group
(
'Picker handles initial noon/midnight times'
,
()
{
testWidgets
(
'midnight'
,
(
WidgetTester
tester
)
async
{
DateTime
date
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment