inRange method
Checks if a given date is within the range.
By default, this check is inclusive, meaning dates exactly equal to start
or end are considered within the range. Set inclusive to false to
exclude boundary dates.
Args:
date (DateTime): The date to check.
inclusive (bool): If true (default), dates at the start/end boundaries
are considered in range. If false, only dates strictly between start
and end are considered.
Returns:
bool: true if the date is within the range, false otherwise.
Example:
final range = DateTimeRange(
start: DateTime(2024, 1, 1),
end: DateTime(2024, 12, 31),
);
range.inRange(DateTime(2024, 6, 15)); // true
range.inRange(DateTime(2024, 1, 1)); // true (inclusive by default)
range.inRange(DateTime(2024, 1, 1), inclusive: false); // false
Implementation
bool inRange(DateTime date, {bool inclusive = true}) {
if (inclusive) {
return (date.isAtSameMomentAs(start) || date.isAfter(start)) &&
(date.isAtSameMomentAs(end) || date.isBefore(end));
}
return date.isAfter(start) && date.isBefore(end);
}