day_picker_selection.dart 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import 'date_period.dart';
  2. import 'utils.dart';
  3. /// Base class for day based pickers selection.
  4. abstract class DayPickerSelection {
  5. /// If this is before [dateTime].
  6. bool isBefore(DateTime dateTime);
  7. /// If this is after [dateTime].
  8. bool isAfter(DateTime dateTime);
  9. /// Returns earliest [DateTime] in this selection.
  10. DateTime get earliest;
  11. /// If this selection is empty.
  12. bool get isEmpty;
  13. /// If this selection is not empty.
  14. bool get isNotEmpty;
  15. /// Constructor to allow children to have constant constructor.
  16. const DayPickerSelection();
  17. }
  18. /// Selection with only one selected date.
  19. ///
  20. /// See also:
  21. /// * [DayPickerMultiSelection] - selection with one or many single dates.
  22. /// * [DayPickerRangeSelection] - date period selection.
  23. class DayPickerSingleSelection extends DayPickerSelection {
  24. /// Selected date.
  25. final DateTime selectedDate;
  26. /// Creates selection with only one selected date.
  27. const DayPickerSingleSelection(this.selectedDate);
  28. @override
  29. bool isAfter(DateTime dateTime) => selectedDate.isAfter(dateTime);
  30. @override
  31. bool isBefore(DateTime dateTime) => selectedDate.isBefore(dateTime);
  32. @override
  33. DateTime get earliest => selectedDate;
  34. @override
  35. bool get isEmpty => false;
  36. @override
  37. bool get isNotEmpty => true;
  38. }
  39. /// Selection with one or many single dates.
  40. ///
  41. /// See also:
  42. /// * [DayPickerSingleSelection] - selection with only one selected date.
  43. /// * [DayPickerRangeSelection] - date period selection.
  44. class DayPickerMultiSelection extends DayPickerSelection {
  45. /// List of the selected dates.
  46. final List<DateTime> selectedDates;
  47. /// Selection with one or many single dates.
  48. DayPickerMultiSelection(this.selectedDates);
  49. @override
  50. bool isAfter(DateTime dateTime) =>
  51. selectedDates.every((d) => d.isAfter(dateTime));
  52. @override
  53. bool isBefore(DateTime dateTime) =>
  54. selectedDates.every((d) => d.isBefore(dateTime));
  55. @override
  56. DateTime get earliest => DatePickerUtils.getEarliestFromList(selectedDates);
  57. @override
  58. bool get isEmpty => selectedDates.isEmpty;
  59. @override
  60. bool get isNotEmpty => selectedDates.isNotEmpty;
  61. }
  62. /// Date period selection.
  63. ///
  64. /// See also:
  65. /// * [DayPickerSingleSelection] - selection with only one selected date.
  66. /// * [DayPickerMultiSelection] - selection with one or many single dates.
  67. class DayPickerRangeSelection extends DayPickerSelection {
  68. /// Selected period.
  69. final DatePeriod selectedRange;
  70. /// Date period selection.
  71. const DayPickerRangeSelection(this.selectedRange);
  72. @override
  73. DateTime get earliest => selectedRange.start;
  74. @override
  75. bool isAfter(DateTime dateTime) => selectedRange.start.isAfter(dateTime);
  76. @override
  77. bool isBefore(DateTime dateTime) => selectedRange.end.isBefore(dateTime);
  78. @override
  79. bool get isEmpty => false;
  80. @override
  81. bool get isNotEmpty => true;
  82. }