utils.dart
1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright 2019 Aleksander Woźniak
// SPDX-License-Identifier: Apache-2.0
import 'package:flutter/widgets.dart';
/// Signature for a function that creates a widget for a given `day`.
typedef DayBuilder = Widget? Function(BuildContext context, DateTime day);
/// Signature for a function that creates a widget for a given `day`.
/// Additionally, contains the currently focused day.
typedef FocusedDayBuilder = Widget? Function(
BuildContext context,
DateTime day,
DateTime focusedDay,
);
/// Signature for a function returning text that can be localized and formatted with `DateFormat`.
typedef TextFormatter = String Function(DateTime date, dynamic locale);
/// Gestures available for the calendar.
enum AvailableGestures { none, verticalSwipe, horizontalSwipe, all }
/// Formats that the calendar can display.
enum CalendarFormat { month, twoWeeks, week }
/// Days of the week that the calendar can start with.
enum StartingDayOfWeek {
monday,
tuesday,
wednesday,
thursday,
friday,
saturday,
sunday,
}
/// Returns a numerical value associated with given `weekday`.
///
/// Returns 1 for `StartingDayOfWeek.monday`, all the way to 7 for `StartingDayOfWeek.sunday`.
int getWeekdayNumber(StartingDayOfWeek weekday) {
return StartingDayOfWeek.values.indexOf(weekday) + 1;
}
/// Returns `date` in UTC format, without its time part.
DateTime normalizeDate(DateTime date) {
return DateTime.utc(date.year, date.month, date.day);
}
/// Checks if two DateTime objects are the same day.
/// Returns `false` if either of them is null.
bool isSameDay(DateTime? a, DateTime? b) {
if (a == null || b == null) {
return false;
}
return a.year == b.year && a.month == b.month && a.day == b.day;
}