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
2933e895
Unverified
Commit
2933e895
authored
Jun 19, 2020
by
Andrew Chen
Committed by
GitHub
Jun 19, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement Comparable<TimeOfDay> (#59317)
parent
5cfb16b1
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
83 additions
and
6 deletions
+83
-6
time.dart
packages/flutter/lib/src/material/time.dart
+18
-6
time_test.dart
packages/flutter/test/material/time_test.dart
+65
-0
No files found.
packages/flutter/lib/src/material/time.dart
View file @
2933e895
...
...
@@ -48,20 +48,21 @@ enum DayPeriod {
/// * [DateTime], which represents date and time, and is subject to eras and
/// time zones.
@immutable
class
TimeOfDay
{
class
TimeOfDay
implements
Comparable
<
TimeOfDay
>
{
/// Creates a time of day.
///
/// The [hour] argument must be between 0 and 23, inclusive. The [minute]
/// argument must be between 0 and 59, inclusive.
const
TimeOfDay
({
@required
this
.
hour
,
@required
this
.
minute
});
const
TimeOfDay
({
@required
this
.
hour
,
@required
this
.
minute
})
:
assert
(
hour
!=
null
&&
hour
>=
0
&&
hour
<
hoursPerDay
),
assert
(
minute
!=
null
&&
minute
>=
0
&&
minute
<
minutesPerHour
);
/// Creates a time of day based on the given time.
///
/// The [hour] is set to the time's hour and the [minute] is set to the time's
/// minute in the timezone of the given [DateTime].
TimeOfDay
.
fromDateTime
(
DateTime
time
)
:
hour
=
time
.
hour
,
minute
=
time
.
minute
;
:
this
(
hour:
time
.
hour
,
minute:
time
.
minute
);
/// Creates a time of day based on the current time.
///
...
...
@@ -80,8 +81,6 @@ class TimeOfDay {
/// Returns a new TimeOfDay with the hour and/or minute replaced.
TimeOfDay
replacing
({
int
hour
,
int
minute
})
{
assert
(
hour
==
null
||
(
hour
>=
0
&&
hour
<
hoursPerDay
));
assert
(
minute
==
null
||
(
minute
>=
0
&&
minute
<
minutesPerHour
));
return
TimeOfDay
(
hour:
hour
??
this
.
hour
,
minute:
minute
??
this
.
minute
);
}
...
...
@@ -136,6 +135,19 @@ class TimeOfDay {
return
'
$TimeOfDay
(
$hourLabel
:
$minuteLabel
)'
;
}
static
int
_inMinutesOf
(
TimeOfDay
time
)
=>
minutesPerHour
*
time
.
hour
+
time
.
minute
;
@override
int
compareTo
(
TimeOfDay
other
)
{
if
(
other
==
null
)
{
return
1
;
}
if
(
other
==
this
)
{
return
0
;
}
return
_inMinutesOf
(
this
).
compareTo
(
_inMinutesOf
(
other
));
}
}
/// Determines how the time picker invoked using [showTimePicker] formats and
...
...
packages/flutter/test/material/time_test.dart
View file @
2933e895
...
...
@@ -28,4 +28,69 @@ void main() {
expect
(
await
pumpTest
(
true
),
'07:00'
);
});
});
group
(
'TimeOfDay'
,
()
{
test
(
'assertions'
,
()
{
expect
(()
=>
TimeOfDay
(
hour:
null
,
minute:
null
),
throwsAssertionError
);
expect
(()
=>
TimeOfDay
(
hour:
null
,
minute:
0
),
throwsAssertionError
);
expect
(()
=>
TimeOfDay
(
hour:
0
,
minute:
null
),
throwsAssertionError
);
expect
(()
=>
TimeOfDay
(
hour:
-
1
,
minute:
-
1
),
throwsAssertionError
);
expect
(()
=>
TimeOfDay
(
hour:
0
,
minute:
-
1
),
throwsAssertionError
);
expect
(()
=>
TimeOfDay
(
hour:
-
1
,
minute:
0
),
throwsAssertionError
);
expect
(()
=>
TimeOfDay
(
hour:
24
,
minute:
0
),
throwsAssertionError
);
expect
(()
=>
TimeOfDay
(
hour:
0
,
minute:
60
),
throwsAssertionError
);
});
test
(
'.=='
,
()
{
expect
(
const
TimeOfDay
(
hour:
0
,
minute:
0
)
==
(
const
TimeOfDay
(
hour:
0
,
minute:
0
)),
true
);
});
test
(
'.replacing'
,
()
{
expect
(
const
TimeOfDay
(
hour:
23
,
minute:
59
).
replacing
(
hour:
0
),
const
TimeOfDay
(
hour:
0
,
minute:
59
),
);
expect
(
const
TimeOfDay
(
hour:
23
,
minute:
59
).
replacing
(
minute:
0
),
const
TimeOfDay
(
hour:
23
,
minute:
0
),
);
});
test
(
'.compareTo'
,
()
{
expect
(
<
TimeOfDay
>[
const
TimeOfDay
(
hour:
12
,
minute:
0
),
const
TimeOfDay
(
hour:
23
,
minute:
59
),
const
TimeOfDay
(
hour:
0
,
minute:
0
),
]..
sort
(),
<
TimeOfDay
>[
const
TimeOfDay
(
hour:
0
,
minute:
0
),
const
TimeOfDay
(
hour:
12
,
minute:
0
),
const
TimeOfDay
(
hour:
23
,
minute:
59
),
]
);
expect
(
const
TimeOfDay
(
hour:
0
,
minute:
0
).
compareTo
(
null
)
>
0
,
true
);
const
TimeOfDay
zero
=
TimeOfDay
(
hour:
0
,
minute:
0
);
expect
(
zero
.
compareTo
(
zero
),
0
);
expect
(
const
TimeOfDay
(
hour:
0
,
minute:
0
)
.
compareTo
(
const
TimeOfDay
(
hour:
0
,
minute:
0
)),
0
);
expect
(
<
TimeOfDay
>[
const
TimeOfDay
(
hour:
0
,
minute:
0
),
const
TimeOfDay
(
hour:
23
,
minute:
59
),
const
TimeOfDay
(
hour:
12
,
minute:
0
),
]..
sort
(),
<
TimeOfDay
>[
const
TimeOfDay
(
hour:
0
,
minute:
0
),
const
TimeOfDay
(
hour:
12
,
minute:
0
),
const
TimeOfDay
(
hour:
23
,
minute:
59
),
]
);
});
});
}
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