Skip to content
This repository has been archived by the owner on Jun 13, 2024. It is now read-only.

Commit

Permalink
Fix pick Date/Time/Range Exception. (#1042)
Browse files Browse the repository at this point in the history
## Description

Fix pick Date/Time/Range Exception.

## Issues


[https://github.com/flutter/gallery/issues/1041](https://github.com/flutter/gallery/issues/1041)

---

Co-authored-by: Pierre-Louis <6655696+guidezpl@users.noreply.github.com>
  • Loading branch information
xiaochenghua and guidezpl authored Jan 10, 2024
1 parent 74017f8 commit f153d93
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 25 deletions.
48 changes: 38 additions & 10 deletions lib/codeviewer/code_segments.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34288,7 +34288,7 @@ class CodeSegments {
TextSpan(style: codeStyle.classStyle, text: 'RestorableRouteFuture'),
TextSpan(style: codeStyle.punctuationStyle, text: '<'),
TextSpan(style: codeStyle.classStyle, text: 'DateTime'),
TextSpan(style: codeStyle.punctuationStyle, text: '>'),
TextSpan(style: codeStyle.punctuationStyle, text: '?>'),
TextSpan(
style: codeStyle.baseStyle,
text: ' _restorableDatePickerRouteFuture'),
Expand All @@ -34297,7 +34297,7 @@ class CodeSegments {
TextSpan(style: codeStyle.classStyle, text: 'RestorableRouteFuture'),
TextSpan(style: codeStyle.punctuationStyle, text: '<'),
TextSpan(style: codeStyle.classStyle, text: 'DateTimeRange'),
TextSpan(style: codeStyle.punctuationStyle, text: '>'),
TextSpan(style: codeStyle.punctuationStyle, text: '?>'),
TextSpan(
style: codeStyle.baseStyle,
text: '\u000a _restorableDateRangePickerRouteFuture'),
Expand All @@ -34306,7 +34306,7 @@ class CodeSegments {
TextSpan(style: codeStyle.classStyle, text: 'RestorableRouteFuture'),
TextSpan(style: codeStyle.punctuationStyle, text: '<'),
TextSpan(style: codeStyle.classStyle, text: 'TimeOfDay'),
TextSpan(style: codeStyle.punctuationStyle, text: '>'),
TextSpan(style: codeStyle.punctuationStyle, text: '?>'),
TextSpan(
style: codeStyle.baseStyle,
text: ' _restorableTimePickerRouteFuture'),
Expand All @@ -34316,6 +34316,7 @@ class CodeSegments {
TextSpan(style: codeStyle.baseStyle, text: ' _selectDate'),
TextSpan(style: codeStyle.punctuationStyle, text: '('),
TextSpan(style: codeStyle.classStyle, text: 'DateTime'),
TextSpan(style: codeStyle.punctuationStyle, text: '?'),
TextSpan(style: codeStyle.baseStyle, text: ' selectedDate'),
TextSpan(style: codeStyle.punctuationStyle, text: ')'),
TextSpan(style: codeStyle.baseStyle, text: ' '),
Expand All @@ -34326,6 +34327,12 @@ class CodeSegments {
TextSpan(style: codeStyle.punctuationStyle, text: '('),
TextSpan(style: codeStyle.baseStyle, text: 'selectedDate '),
TextSpan(style: codeStyle.punctuationStyle, text: '!='),
TextSpan(style: codeStyle.baseStyle, text: ' '),
TextSpan(style: codeStyle.keywordStyle, text: 'null'),
TextSpan(style: codeStyle.baseStyle, text: ' '),
TextSpan(style: codeStyle.punctuationStyle, text: '&&'),
TextSpan(style: codeStyle.baseStyle, text: ' selectedDate '),
TextSpan(style: codeStyle.punctuationStyle, text: '!='),
TextSpan(style: codeStyle.baseStyle, text: ' _fromDate'),
TextSpan(style: codeStyle.punctuationStyle, text: '.'),
TextSpan(style: codeStyle.baseStyle, text: 'value'),
Expand Down Expand Up @@ -34353,39 +34360,54 @@ class CodeSegments {
TextSpan(style: codeStyle.baseStyle, text: ' _selectDateRange'),
TextSpan(style: codeStyle.punctuationStyle, text: '('),
TextSpan(style: codeStyle.classStyle, text: 'DateTimeRange'),
TextSpan(style: codeStyle.punctuationStyle, text: '?'),
TextSpan(style: codeStyle.baseStyle, text: ' newSelectedDate'),
TextSpan(style: codeStyle.punctuationStyle, text: ')'),
TextSpan(style: codeStyle.baseStyle, text: ' '),
TextSpan(style: codeStyle.punctuationStyle, text: '{'),
TextSpan(style: codeStyle.baseStyle, text: '\u000a setState'),
TextSpan(style: codeStyle.baseStyle, text: '\u000a '),
TextSpan(style: codeStyle.keywordStyle, text: 'if'),
TextSpan(style: codeStyle.baseStyle, text: ' '),
TextSpan(style: codeStyle.punctuationStyle, text: '('),
TextSpan(style: codeStyle.baseStyle, text: 'newSelectedDate '),
TextSpan(style: codeStyle.punctuationStyle, text: '!='),
TextSpan(style: codeStyle.baseStyle, text: ' '),
TextSpan(style: codeStyle.keywordStyle, text: 'null'),
TextSpan(style: codeStyle.punctuationStyle, text: ')'),
TextSpan(style: codeStyle.baseStyle, text: ' '),
TextSpan(style: codeStyle.punctuationStyle, text: '{'),
TextSpan(style: codeStyle.baseStyle, text: '\u000a setState'),
TextSpan(style: codeStyle.punctuationStyle, text: '(()'),
TextSpan(style: codeStyle.baseStyle, text: ' '),
TextSpan(style: codeStyle.punctuationStyle, text: '{'),
TextSpan(style: codeStyle.baseStyle, text: '\u000a _startDate'),
TextSpan(style: codeStyle.baseStyle, text: '\u000a _startDate'),
TextSpan(style: codeStyle.punctuationStyle, text: '.'),
TextSpan(style: codeStyle.baseStyle, text: 'value '),
TextSpan(style: codeStyle.punctuationStyle, text: '='),
TextSpan(style: codeStyle.baseStyle, text: ' newSelectedDate'),
TextSpan(style: codeStyle.punctuationStyle, text: '.'),
TextSpan(style: codeStyle.baseStyle, text: 'start'),
TextSpan(style: codeStyle.punctuationStyle, text: ';'),
TextSpan(style: codeStyle.baseStyle, text: '\u000a _endDate'),
TextSpan(style: codeStyle.baseStyle, text: '\u000a _endDate'),
TextSpan(style: codeStyle.punctuationStyle, text: '.'),
TextSpan(style: codeStyle.baseStyle, text: 'value '),
TextSpan(style: codeStyle.punctuationStyle, text: '='),
TextSpan(style: codeStyle.baseStyle, text: ' newSelectedDate'),
TextSpan(style: codeStyle.punctuationStyle, text: '.'),
TextSpan(style: codeStyle.baseStyle, text: 'end'),
TextSpan(style: codeStyle.punctuationStyle, text: ';'),
TextSpan(style: codeStyle.baseStyle, text: '\u000a '),
TextSpan(style: codeStyle.baseStyle, text: '\u000a '),
TextSpan(style: codeStyle.punctuationStyle, text: '});'),
TextSpan(style: codeStyle.baseStyle, text: '\u000a '),
TextSpan(style: codeStyle.punctuationStyle, text: '}'),
TextSpan(style: codeStyle.baseStyle, text: '\u000a '),
TextSpan(style: codeStyle.punctuationStyle, text: '}'),
TextSpan(style: codeStyle.baseStyle, text: '\u000a\u000a '),
TextSpan(style: codeStyle.keywordStyle, text: 'void'),
TextSpan(style: codeStyle.baseStyle, text: ' _selectTime'),
TextSpan(style: codeStyle.punctuationStyle, text: '('),
TextSpan(style: codeStyle.classStyle, text: 'TimeOfDay'),
TextSpan(style: codeStyle.punctuationStyle, text: '?'),
TextSpan(style: codeStyle.baseStyle, text: ' selectedTime'),
TextSpan(style: codeStyle.punctuationStyle, text: ')'),
TextSpan(style: codeStyle.baseStyle, text: ' '),
Expand All @@ -34396,6 +34418,12 @@ class CodeSegments {
TextSpan(style: codeStyle.punctuationStyle, text: '('),
TextSpan(style: codeStyle.baseStyle, text: 'selectedTime '),
TextSpan(style: codeStyle.punctuationStyle, text: '!='),
TextSpan(style: codeStyle.baseStyle, text: ' '),
TextSpan(style: codeStyle.keywordStyle, text: 'null'),
TextSpan(style: codeStyle.baseStyle, text: ' '),
TextSpan(style: codeStyle.punctuationStyle, text: '&&'),
TextSpan(style: codeStyle.baseStyle, text: ' selectedTime '),
TextSpan(style: codeStyle.punctuationStyle, text: '!='),
TextSpan(style: codeStyle.baseStyle, text: ' _fromTime'),
TextSpan(style: codeStyle.punctuationStyle, text: '.'),
TextSpan(style: codeStyle.baseStyle, text: 'value'),
Expand Down Expand Up @@ -34726,7 +34754,7 @@ class CodeSegments {
TextSpan(style: codeStyle.classStyle, text: 'RestorableRouteFuture'),
TextSpan(style: codeStyle.punctuationStyle, text: '<'),
TextSpan(style: codeStyle.classStyle, text: 'DateTime'),
TextSpan(style: codeStyle.punctuationStyle, text: '>('),
TextSpan(style: codeStyle.punctuationStyle, text: '?>('),
TextSpan(style: codeStyle.baseStyle, text: '\u000a onComplete'),
TextSpan(style: codeStyle.punctuationStyle, text: ':'),
TextSpan(style: codeStyle.baseStyle, text: ' _selectDate'),
Expand Down Expand Up @@ -34772,7 +34800,7 @@ class CodeSegments {
TextSpan(style: codeStyle.classStyle, text: 'RestorableRouteFuture'),
TextSpan(style: codeStyle.punctuationStyle, text: '<'),
TextSpan(style: codeStyle.classStyle, text: 'DateTimeRange'),
TextSpan(style: codeStyle.punctuationStyle, text: '>('),
TextSpan(style: codeStyle.punctuationStyle, text: '?>('),
TextSpan(style: codeStyle.baseStyle, text: '\u000a onComplete'),
TextSpan(style: codeStyle.punctuationStyle, text: ':'),
TextSpan(style: codeStyle.baseStyle, text: ' _selectDateRange'),
Expand Down Expand Up @@ -34803,7 +34831,7 @@ class CodeSegments {
TextSpan(style: codeStyle.classStyle, text: 'RestorableRouteFuture'),
TextSpan(style: codeStyle.punctuationStyle, text: '<'),
TextSpan(style: codeStyle.classStyle, text: 'TimeOfDay'),
TextSpan(style: codeStyle.punctuationStyle, text: '>('),
TextSpan(style: codeStyle.punctuationStyle, text: '?>('),
TextSpan(style: codeStyle.baseStyle, text: '\u000a onComplete'),
TextSpan(style: codeStyle.punctuationStyle, text: ':'),
TextSpan(style: codeStyle.baseStyle, text: ' _selectTime'),
Expand Down
32 changes: 17 additions & 15 deletions lib/demos/material/picker_demo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,30 @@ class _PickerDemoState extends State<PickerDemo> with RestorationMixin {
final RestorableDateTime _startDate = RestorableDateTime(DateTime.now());
final RestorableDateTime _endDate = RestorableDateTime(DateTime.now());

late RestorableRouteFuture<DateTime> _restorableDatePickerRouteFuture;
late RestorableRouteFuture<DateTimeRange>
late RestorableRouteFuture<DateTime?> _restorableDatePickerRouteFuture;
late RestorableRouteFuture<DateTimeRange?>
_restorableDateRangePickerRouteFuture;
late RestorableRouteFuture<TimeOfDay> _restorableTimePickerRouteFuture;
late RestorableRouteFuture<TimeOfDay?> _restorableTimePickerRouteFuture;

void _selectDate(DateTime selectedDate) {
if (selectedDate != _fromDate.value) {
void _selectDate(DateTime? selectedDate) {
if (selectedDate != null && selectedDate != _fromDate.value) {
setState(() {
_fromDate.value = selectedDate;
});
}
}

void _selectDateRange(DateTimeRange newSelectedDate) {
setState(() {
_startDate.value = newSelectedDate.start;
_endDate.value = newSelectedDate.end;
});
void _selectDateRange(DateTimeRange? newSelectedDate) {
if (newSelectedDate != null) {
setState(() {
_startDate.value = newSelectedDate.start;
_endDate.value = newSelectedDate.end;
});
}
}

void _selectTime(TimeOfDay selectedTime) {
if (selectedTime != _fromTime.value) {
void _selectTime(TimeOfDay? selectedTime) {
if (selectedTime != null && selectedTime != _fromTime.value) {
setState(() {
_fromTime.value = selectedTime;
});
Expand Down Expand Up @@ -111,7 +113,7 @@ class _PickerDemoState extends State<PickerDemo> with RestorationMixin {
@override
void initState() {
super.initState();
_restorableDatePickerRouteFuture = RestorableRouteFuture<DateTime>(
_restorableDatePickerRouteFuture = RestorableRouteFuture<DateTime?>(
onComplete: _selectDate,
onPresent: (navigator, arguments) {
return navigator.restorablePush(
Expand All @@ -121,13 +123,13 @@ class _PickerDemoState extends State<PickerDemo> with RestorationMixin {
},
);
_restorableDateRangePickerRouteFuture =
RestorableRouteFuture<DateTimeRange>(
RestorableRouteFuture<DateTimeRange?>(
onComplete: _selectDateRange,
onPresent: (navigator, arguments) =>
navigator.restorablePush(_dateRangePickerRoute),
);

_restorableTimePickerRouteFuture = RestorableRouteFuture<TimeOfDay>(
_restorableTimePickerRouteFuture = RestorableRouteFuture<TimeOfDay?>(
onComplete: _selectTime,
onPresent: (navigator, arguments) => navigator.restorablePush(
_timePickerRoute,
Expand Down

0 comments on commit f153d93

Please sign in to comment.