Skip to content

Commit

Permalink
Migrate package to null safety
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksanderwozniak committed Feb 22, 2021
1 parent 354d51d commit 79433e4
Show file tree
Hide file tree
Showing 15 changed files with 211 additions and 225 deletions.
30 changes: 15 additions & 15 deletions lib/src/customization/calendar_builders.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,58 +25,58 @@ typedef HighlightBuilder = Widget Function(
/// Class containing all custom builders for `TableCalendar`.
class CalendarBuilders<T> {
/// Custom builder for day cells, with a priority over any other builder.
final FocusedDayBuilder prioritizedBuilder;
final FocusedDayBuilder? prioritizedBuilder;

/// Custom builder for a day cell that matches `DateTime.now()`.
final FocusedDayBuilder todayBuilder;
final FocusedDayBuilder? todayBuilder;

/// Custom builder for day cells that are currently marked as selected by `selectedDayPredicate`.
final FocusedDayBuilder selectedBuilder;
final FocusedDayBuilder? selectedBuilder;

/// Custom builder for a day cell that is the start of current range selection.
final FocusedDayBuilder rangeStartBuilder;
final FocusedDayBuilder? rangeStartBuilder;

/// Custom builder for a day cell that is the end of current range selection.
final FocusedDayBuilder rangeEndBuilder;
final FocusedDayBuilder? rangeEndBuilder;

/// Custom builder for day cells that fall within the currently selected range.
final FocusedDayBuilder withinRangeBuilder;
final FocusedDayBuilder? withinRangeBuilder;

/// Custom builder for day cells, of which the `day.month` is different than `focusedDay.month`.
/// This will affect day cells that do not match the currently focused month.
final FocusedDayBuilder outsideBuilder;
final FocusedDayBuilder? outsideBuilder;

/// Custom builder for day cells that have been disabled.
///
/// This refers to dates disabled by returning false in `enabledDayPredicate`,
/// as well as dates that are outside of the bounds set up by `firstDay` and `lastDay`.
final FocusedDayBuilder disabledBuilder;
final FocusedDayBuilder? disabledBuilder;

/// Custom builder for day cells that are marked as holidays by `holidayPredicate`.
final FocusedDayBuilder holidayBuilder;
final FocusedDayBuilder? holidayBuilder;

/// Custom builder for day cells that do not match any other builder.
final FocusedDayBuilder defaultBuilder;
final FocusedDayBuilder? defaultBuilder;

/// Custom builder for background highlight of range selection.
/// If `isWithinRange` is true, then `day` is within the selected range.
final HighlightBuilder rangeHighlightBuilder;
final HighlightBuilder? rangeHighlightBuilder;

/// Custom builder for a single event marker. Each of those will be displayed in a `Row` above of the day cell.
/// You can adjust markers' position with `CalendarStyle` properties.
///
/// If `singleMarkerBuilder` is not specified, a default event marker will be displayed (customizable with `CalendarStyle`).
final SingleMarkerBuilder<T> singleMarkerBuilder;
final SingleMarkerBuilder<T>? singleMarkerBuilder;

/// Custom builder for event markers. Use to provide your own marker UI for each day cell.
/// Using `markerBuilder` will override `singleMarkerBuilder` and default event markers.
final MarkerBuilder<T> markerBuilder;
final MarkerBuilder<T>? markerBuilder;

/// Custom builder for days of the week labels (Mon, Tue, Wed, etc.).
final DayBuilder dowBuilder;
final DayBuilder? dowBuilder;

/// Use to customize header's title using different widget
final DayBuilder headerTitleBuilder;
final DayBuilder? headerTitleBuilder;

/// Creates `CalendarBuilders` for `TableCalendar` widget.
const CalendarBuilders({
Expand Down
10 changes: 5 additions & 5 deletions lib/src/customization/calendar_style.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class CalendarStyle {
/// The size of single event marker dot.
///
/// By default `markerSizeScale` is used. To use `markerSize` instead, simply provide a non-null value.
final double markerSize;
final double? markerSize;

/// Proportion of single event marker dot size in relation to day cell size.
///
Expand Down Expand Up @@ -212,16 +212,16 @@ class CalendarStyle {
/// Helper class containing data for internal `Positioned` widget.
class PositionedOffset {
/// Distance from the top edge.
final double top;
final double? top;

/// Distance from the bottom edge.
final double bottom;
final double? bottom;

/// Distance from the leading edge.
final double start;
final double? start;

/// Distance from the trailing edge.
final double end;
final double? end;

/// Creates a `PositionedOffset`. Values are set to `null` by default.
const PositionedOffset({this.top, this.bottom, this.start, this.end});
Expand Down
2 changes: 1 addition & 1 deletion lib/src/customization/days_of_week_style.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class DaysOfWeekStyle {
/// ```dart
/// dowTextFormatter: (date, locale) => DateFormat.E(locale).format(date)[0],
/// ```
final TextFormatter dowTextFormatter;
final TextFormatter? dowTextFormatter;

/// Decoration for the top row of the table
final Decoration decoration;
Expand Down
4 changes: 2 additions & 2 deletions lib/src/customization/header_style.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class HeaderStyle {
/// ```dart
/// titleTextFormatter: (date, locale) => DateFormat.yM(locale).format(date),
/// ```
final TextFormatter titleTextFormatter;
final TextFormatter? titleTextFormatter;

/// Style for title Text (month-year) displayed in header.
final TextStyle titleTextStyle;
Expand Down Expand Up @@ -89,7 +89,7 @@ class HeaderStyle {
border: const Border.fromBorderSide(BorderSide()),
borderRadius: const BorderRadius.all(Radius.circular(12.0)),
),
this.headerMargin,
this.headerMargin = const EdgeInsets.all(0.0),
this.headerPadding = const EdgeInsets.symmetric(vertical: 8.0),
this.formatButtonPadding =
const EdgeInsets.symmetric(horizontal: 10.0, vertical: 4.0),
Expand Down
2 changes: 1 addition & 1 deletion lib/src/shared/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ DateTime normalizeDate(DateTime date) {

/// Checks if two DateTime objects are the same day.
/// Returns `false` if either of them is null.
bool isSameDay(DateTime a, DateTime b) {
bool isSameDay(DateTime? a, DateTime? b) {
if (a == null || b == null) {
return false;
}
Expand Down
99 changes: 46 additions & 53 deletions lib/src/table_calendar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ typedef OnDaySelected = void Function(
/// Signature for `onRangeSelected` callback.
/// Contains start and end of the selected range, as well as currently focused day.
typedef OnRangeSelected = void Function(
DateTime start, DateTime end, DateTime focusedDay);
DateTime? start, DateTime? end, DateTime focusedDay);

/// Modes that range selection can operate in.
enum RangeSelectionMode { disabled, toggledOff, toggledOn, enforced }
Expand All @@ -36,10 +36,10 @@ class TableCalendar<T> extends StatefulWidget {
final dynamic locale;

/// The start of the selected day range.
final DateTime rangeStartDay;
final DateTime? rangeStartDay;

/// The end of the selected day range.
final DateTime rangeEndDay;
final DateTime? rangeEndDay;

/// DateTime that determines which days are currently visible and focused.
final DateTime focusedDay;
Expand Down Expand Up @@ -150,54 +150,54 @@ class TableCalendar<T> extends StatefulWidget {
final RangeSelectionMode rangeSelectionMode;

/// Function that assigns a list of events to a specified day.
final List<T> Function(DateTime day) eventLoader;
final List<T> Function(DateTime day)? eventLoader;

/// Function deciding whether given day should be enabled or not.
/// If `false` is returned, this day will be disabled.
final bool Function(DateTime day) enabledDayPredicate;
final bool Function(DateTime day)? enabledDayPredicate;

/// Function deciding whether given day should be marked as selected.
final bool Function(DateTime day) selectedDayPredicate;
final bool Function(DateTime day)? selectedDayPredicate;

/// Function deciding whether given day is treated as a holiday.
final bool Function(DateTime day) holidayPredicate;
final bool Function(DateTime day)? holidayPredicate;

/// Called whenever a day range gets selected.
final OnRangeSelected onRangeSelected;
final OnRangeSelected? onRangeSelected;

/// Called whenever any day gets tapped.
final OnDaySelected onDaySelected;
final OnDaySelected? onDaySelected;

/// Called whenever any day gets long pressed.
final OnDaySelected onDayLongPressed;
final OnDaySelected? onDayLongPressed;

/// Called whenever any disabled day gets tapped.
final void Function(DateTime day) onDisabledDayTapped;
final void Function(DateTime day)? onDisabledDayTapped;

/// Called whenever any disabled day gets long pressed.
final void Function(DateTime day) onDisabledDayLongPressed;
final void Function(DateTime day)? onDisabledDayLongPressed;

/// Called whenever header gets tapped.
final void Function(DateTime focusedDay) onHeaderTapped;
final void Function(DateTime focusedDay)? onHeaderTapped;

/// Called whenever header gets long pressed.
final void Function(DateTime focusedDay) onHeaderLongPressed;
final void Function(DateTime focusedDay)? onHeaderLongPressed;

/// Called whenever currently visible calendar page is changed.
final void Function(DateTime focusedDay) onPageChanged;
final void Function(DateTime focusedDay)? onPageChanged;

/// Called whenever `calendarFormat` is changed.
final void Function(CalendarFormat format) onFormatChanged;
final void Function(CalendarFormat format)? onFormatChanged;

/// Called when the calendar is created. Exposes its PageController.
final void Function(PageController pageController) onCalendarCreated;
final void Function(PageController pageController)? onCalendarCreated;

/// Creates a `TableCalendar` widget.
TableCalendar({
Key key,
@required DateTime focusedDay,
@required DateTime firstDay,
@required DateTime lastDay,
Key? key,
required DateTime focusedDay,
required DateTime firstDay,
required DateTime lastDay,
this.locale,
this.rangeStartDay,
this.rangeEndDay,
Expand Down Expand Up @@ -230,7 +230,7 @@ class TableCalendar<T> extends StatefulWidget {
this.daysOfWeekStyle = const DaysOfWeekStyle(),
this.calendarStyle = const CalendarStyle(),
this.calendarBuilders = const CalendarBuilders(),
this.rangeSelectionMode,
this.rangeSelectionMode = RangeSelectionMode.toggledOff,
this.eventLoader,
this.enabledDayPredicate,
this.selectedDayPredicate,
Expand All @@ -245,14 +245,8 @@ class TableCalendar<T> extends StatefulWidget {
this.onPageChanged,
this.onFormatChanged,
this.onCalendarCreated,
}) : assert(calendarFormat != null),
assert(rowHeight != null),
assert(firstDay != null),
assert(lastDay != null),
assert(focusedDay != null),
assert(availableCalendarFormats.keys.contains(calendarFormat)),
}) : assert(availableCalendarFormats.keys.contains(calendarFormat)),
assert(availableCalendarFormats.length <= CalendarFormat.values.length),
assert(weekendDays != null),
assert(weekendDays.isNotEmpty
? weekendDays.every(
(day) => day >= DateTime.monday && day <= DateTime.sunday)
Expand All @@ -267,29 +261,27 @@ class TableCalendar<T> extends StatefulWidget {
}

class _TableCalendarState<T> extends State<TableCalendar<T>> {
PageController _pageController;
ValueNotifier<DateTime> _focusedDay;
DateTime _firstSelectedDay;
RangeSelectionMode _rangeSelectionMode;
late final PageController _pageController;
late final ValueNotifier<DateTime> _focusedDay;
late RangeSelectionMode _rangeSelectionMode;
DateTime? _firstSelectedDay;

@override
void initState() {
super.initState();
_focusedDay = ValueNotifier(widget.focusedDay);
_rangeSelectionMode =
widget.rangeSelectionMode ?? RangeSelectionMode.toggledOff;
_rangeSelectionMode = widget.rangeSelectionMode;
}

@override
void didUpdateWidget(TableCalendar oldWidget) {
void didUpdateWidget(TableCalendar<T> oldWidget) {
super.didUpdateWidget(oldWidget);

if (_focusedDay.value != widget.focusedDay) {
_focusedDay.value = widget.focusedDay;
}

if (widget.rangeSelectionMode != null &&
_rangeSelectionMode != widget.rangeSelectionMode) {
if (_rangeSelectionMode != widget.rangeSelectionMode) {
_rangeSelectionMode = widget.rangeSelectionMode;
}

Expand Down Expand Up @@ -331,7 +323,7 @@ class _TableCalendarState<T> extends State<TableCalendar<T>> {
id = max(0, id - 1);
}

widget.onFormatChanged(formats[id]);
widget.onFormatChanged!(formats[id]);
}
}

Expand All @@ -350,13 +342,13 @@ class _TableCalendarState<T> extends State<TableCalendar<T>> {
if (_isRangeSelectionOn && widget.onRangeSelected != null) {
if (_firstSelectedDay == null) {
_firstSelectedDay = day;
widget.onRangeSelected(_firstSelectedDay, null, _focusedDay.value);
widget.onRangeSelected!(_firstSelectedDay, null, _focusedDay.value);
} else {
if (day.isAfter(_firstSelectedDay)) {
widget.onRangeSelected(_firstSelectedDay, day, _focusedDay.value);
if (day.isAfter(_firstSelectedDay!)) {
widget.onRangeSelected!(_firstSelectedDay, day, _focusedDay.value);
_firstSelectedDay = null;
} else if (day.isBefore(_firstSelectedDay)) {
widget.onRangeSelected(day, _firstSelectedDay, _focusedDay.value);
} else if (day.isBefore(_firstSelectedDay!)) {
widget.onRangeSelected!(day, _firstSelectedDay, _focusedDay.value);
_firstSelectedDay = null;
}
}
Expand All @@ -377,7 +369,7 @@ class _TableCalendarState<T> extends State<TableCalendar<T>> {

if (widget.onDayLongPressed != null) {
_updateFocusOnTap(day);
return widget.onDayLongPressed(day, _focusedDay.value);
return widget.onDayLongPressed!(day, _focusedDay.value);
}

if (widget.onRangeSelected != null) {
Expand All @@ -387,7 +379,7 @@ class _TableCalendarState<T> extends State<TableCalendar<T>> {

if (_isRangeSelectionOn) {
_firstSelectedDay = day;
widget.onRangeSelected(_firstSelectedDay, null, _focusedDay.value);
widget.onRangeSelected!(_firstSelectedDay, null, _focusedDay.value);
} else {
_firstSelectedDay = null;
widget.onDaySelected?.call(day, _focusedDay.value);
Expand Down Expand Up @@ -450,8 +442,9 @@ class _TableCalendarState<T> extends State<TableCalendar<T>> {
focusedMonth: value,
onLeftChevronTap: _onLeftChevronTap,
onRightChevronTap: _onRightChevronTap,
onHeaderTap: () => widget.onHeaderTapped(value),
onHeaderLongPress: () => widget.onHeaderLongPressed(value),
onHeaderTap: () => widget.onHeaderTapped?.call(value),
onHeaderLongPress: () =>
widget.onHeaderLongPressed?.call(value),
headerStyle: widget.headerStyle,
availableCalendarFormats: widget.availableCalendarFormats,
calendarFormat: widget.calendarFormat,
Expand Down Expand Up @@ -496,7 +489,7 @@ class _TableCalendarState<T> extends State<TableCalendar<T>> {
widget.onPageChanged?.call(focusedDay);
},
dowBuilder: (BuildContext context, DateTime day) {
Widget dowCell =
Widget? dowCell =
widget.calendarBuilders.dowBuilder?.call(context, day);

if (dowCell == null) {
Expand Down Expand Up @@ -550,12 +543,12 @@ class _TableCalendarState<T> extends State<TableCalendar<T>> {

final isWithinRange = widget.rangeStartDay != null &&
widget.rangeEndDay != null &&
_isWithinRange(day, widget.rangeStartDay, widget.rangeEndDay);
_isWithinRange(day, widget.rangeStartDay!, widget.rangeEndDay!);

final isRangeStart = isSameDay(day, widget.rangeStartDay);
final isRangeEnd = isSameDay(day, widget.rangeEndDay);

Widget rangeHighlight = widget.calendarBuilders.rangeHighlightBuilder
Widget? rangeHighlight = widget.calendarBuilders.rangeHighlightBuilder
?.call(context, day, isWithinRange);

if (rangeHighlight == null) {
Expand Down Expand Up @@ -604,7 +597,7 @@ class _TableCalendarState<T> extends State<TableCalendar<T>> {

if (!isDisabled) {
final events = widget.eventLoader?.call(day) ?? [];
Widget markerWidget =
Widget? markerWidget =
widget.calendarBuilders.markerBuilder?.call(context, day, events);

if (events.isNotEmpty && markerWidget == null) {
Expand Down Expand Up @@ -689,7 +682,7 @@ class _TableCalendarState<T> extends State<TableCalendar<T>> {
bool _isDayAvailable(DateTime day) {
return widget.enabledDayPredicate == null
? true
: widget.enabledDayPredicate(day);
: widget.enabledDayPredicate!(day);
}

DateTime _firstDayOfMonth(DateTime month) {
Expand Down
Loading

0 comments on commit 79433e4

Please sign in to comment.