mirror of
https://github.com/nisrulz/flutter-examples.git
synced 2025-11-08 20:50:04 +00:00
New Example - Calendar (#91)
This commit is contained in:
152
flutter_date_pickers-master/test/date_time_utils.dart
Normal file
152
flutter_date_pickers-master/test/date_time_utils.dart
Normal file
@@ -0,0 +1,152 @@
|
||||
/// Bunch of useful functions for date pickers.
|
||||
class DateTimeUtils {
|
||||
/// Returns if two objects have same year, month and day.
|
||||
/// Time doesn't matter.
|
||||
static bool sameDate(DateTime dateTimeOne, DateTime dateTimeTwo) =>
|
||||
dateTimeOne.year == dateTimeTwo.year &&
|
||||
dateTimeOne.month == dateTimeTwo.month &&
|
||||
dateTimeOne.day == dateTimeTwo.day;
|
||||
|
||||
/// Returns if two objects have same year and month.
|
||||
/// Day and time don't matter/
|
||||
static bool sameMonth(DateTime dateTimeOne, DateTime dateTimeTwo) =>
|
||||
dateTimeOne.year == dateTimeTwo.year
|
||||
&& dateTimeOne.month == dateTimeTwo.month;
|
||||
|
||||
|
||||
|
||||
/// Returns number of months between [startDate] and [endDate]
|
||||
static int monthDelta(DateTime startDate, DateTime endDate) =>
|
||||
(endDate.year - startDate.year) * 12 +
|
||||
endDate.month -
|
||||
startDate.month;
|
||||
|
||||
|
||||
/// Add months to a month truncated date.
|
||||
static DateTime addMonthsToMonthDate(DateTime monthDate, int monthsToAdd) =>
|
||||
// year is switched automatically if new month > 12
|
||||
DateTime(monthDate.year, monthDate.month + monthsToAdd);
|
||||
|
||||
|
||||
/// Returns number of years between [startDate] and [endDate]
|
||||
static int yearDelta(DateTime startDate, DateTime endDate) =>
|
||||
endDate.year - startDate.year;
|
||||
|
||||
|
||||
/// Returns start of the first day of the week with given day.
|
||||
///
|
||||
/// Start of the week calculated using firstDayIndex which is int from 0 to 6
|
||||
/// where 0 points to Sunday and 6 points to Saturday.
|
||||
/// (according to MaterialLocalization.firstDayIfWeekIndex)
|
||||
static DateTime getFirstDayOfWeek(DateTime day, int firstDayIndex) {
|
||||
// from 1 to 7 where 1 points to Monday and 7 points to Sunday
|
||||
int weekday = day.weekday;
|
||||
|
||||
// to match weekdays where Sunday is 7 not 0
|
||||
if (firstDayIndex == 0) firstDayIndex = 7;
|
||||
|
||||
int diff = weekday - firstDayIndex;
|
||||
if (diff < 0) diff = 7 + diff;
|
||||
|
||||
DateTime firstDayOfWeek = day.subtract(Duration(days: diff));
|
||||
firstDayOfWeek = startOfTheDay(firstDayOfWeek);
|
||||
return firstDayOfWeek;
|
||||
}
|
||||
|
||||
/// Returns end of the last day of the week with given day.
|
||||
///
|
||||
/// Start of the week calculated using firstDayIndex which is int from 0 to 6
|
||||
/// where 0 points to Sunday and 6 points to Saturday.
|
||||
/// (according to MaterialLocalization.firstDayIfWeekIndex)
|
||||
static DateTime getLastDayOfWeek(DateTime day, int firstDayIndex) {
|
||||
// from 1 to 7 where 1 points to Monday and 7 points to Sunday
|
||||
int weekday = day.weekday;
|
||||
|
||||
// to match weekdays where Sunday is 7 not 0
|
||||
if (firstDayIndex == 0) firstDayIndex = 7;
|
||||
|
||||
int lastDayIndex = firstDayIndex - 1;
|
||||
if (lastDayIndex == 0) lastDayIndex = 7;
|
||||
|
||||
int diff = lastDayIndex - weekday;
|
||||
if (diff < 0) diff = 7 + diff;
|
||||
|
||||
DateTime lastDayOfWeek = day.add(Duration(days: diff));
|
||||
lastDayOfWeek = endOfTheDay(lastDayOfWeek);
|
||||
return lastDayOfWeek;
|
||||
}
|
||||
|
||||
/// Returns end of the given day.
|
||||
///
|
||||
/// End time is 1 millisecond before start of the next day.
|
||||
static DateTime endOfTheDay(DateTime date) =>
|
||||
DateTime(date.year, date.month, date.day)
|
||||
.add(const Duration(days: 1))
|
||||
.subtract(const Duration(milliseconds: 1));
|
||||
|
||||
/// Returns start of the given day.
|
||||
///
|
||||
/// Start time is 00:00:00.
|
||||
static DateTime startOfTheDay(DateTime date) =>
|
||||
DateTime(date.year, date.month, date.day);
|
||||
|
||||
|
||||
/// Computes the offset from the first day of week that the first day of the
|
||||
/// [month] falls on.
|
||||
///
|
||||
/// For example, September 1, 2017 falls on a Friday, which in the calendar
|
||||
/// localized for United States English appears as:
|
||||
///
|
||||
/// ```
|
||||
/// S M T W T F S
|
||||
/// _ _ _ _ _ 1 2
|
||||
/// ```
|
||||
///
|
||||
/// The offset for the first day of the months is the number of leading blanks
|
||||
/// in the calendar, i.e. 5.
|
||||
///
|
||||
/// The same date localized for the Russian calendar has a different offset,
|
||||
/// because the first day of week is Monday rather than Sunday:
|
||||
///
|
||||
/// ```
|
||||
/// M T W T F S S
|
||||
/// _ _ _ _ 1 2 3
|
||||
/// ```
|
||||
///
|
||||
/// So the offset is 4, rather than 5.
|
||||
///
|
||||
/// This code consolidates the following:
|
||||
///
|
||||
/// - [DateTime.weekday] provides a 1-based index into days of week, with 1
|
||||
/// falling on Monday.
|
||||
/// - MaterialLocalizations.firstDayOfWeekIndex provides a 0-based index
|
||||
/// into the MaterialLocalizations.narrowWeekdays list.
|
||||
/// - MaterialLocalizations.narrowWeekdays list provides localized names of
|
||||
/// days of week, always starting with Sunday and ending with Saturday.
|
||||
static int computeFirstDayOffset(
|
||||
int year, int month, int firstDayOfWeekFromSunday) {
|
||||
// 0-based day of week, with 0 representing Monday.
|
||||
final int weekdayFromMonday = DateTime(year, month).weekday - 1;
|
||||
// firstDayOfWeekFromSunday recomputed to be Monday-based
|
||||
final int firstDayOfWeekFromMonday = (firstDayOfWeekFromSunday - 1) % 7;
|
||||
// Number of days between the first day of week appearing on the calendar,
|
||||
// and the day corresponding to the 1-st of the month.
|
||||
return (weekdayFromMonday - firstDayOfWeekFromMonday) % 7;
|
||||
}
|
||||
|
||||
/// Returns earliest [DateTime] from the list.
|
||||
///
|
||||
/// [dates] must not be null.
|
||||
/// In case it is null, [ArgumentError] will be thrown.
|
||||
static DateTime getEarliestFromList(List<DateTime> dates) {
|
||||
ArgumentError.checkNotNull(dates, "dates");
|
||||
|
||||
return dates.fold(dates[0], getEarliest);
|
||||
}
|
||||
|
||||
/// Returns earliest [DateTime] from two.
|
||||
///
|
||||
/// If two [DateTime]s is the same moment first ([a]) will be return.
|
||||
static DateTime getEarliest(DateTime a, DateTime b)
|
||||
=> a.isBefore(b) ? a : b;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import 'package:flutter_date_pickers/src/day_type.dart';
|
||||
import 'package:flutter_date_pickers/src/i_selectable_picker.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
import 'date_time_utils.dart';
|
||||
|
||||
void main() {
|
||||
group("DayMultiSelectable test.", () {
|
||||
test("getDayType() returns correct type for different dates", () {
|
||||
final now = DateTime.now();
|
||||
final selectedDates = [
|
||||
now.subtract(const Duration(days: 1)),
|
||||
now,
|
||||
now.add(const Duration(days: 1)),
|
||||
];
|
||||
|
||||
final firstDate = now.subtract(const Duration(days: 10));
|
||||
final lastDate = now.add(const Duration(days: 10));
|
||||
final disabledDate = now.subtract(const Duration(days: 5));
|
||||
|
||||
// ignore: prefer_function_declarations_over_variables
|
||||
final selectablePredicate = (DateTime d)
|
||||
=> !DateTimeUtils.sameDate(d, disabledDate);
|
||||
|
||||
final selectableLogic = DayMultiSelectable(
|
||||
selectedDates, firstDate, lastDate,
|
||||
selectableDayPredicate: selectablePredicate);
|
||||
|
||||
final notSelectedEnabledDateType = selectableLogic.getDayType(firstDate);
|
||||
expect(notSelectedEnabledDateType, DayType.notSelected);
|
||||
|
||||
final disabledDateType = selectableLogic.getDayType(disabledDate);
|
||||
expect(disabledDateType, DayType.disabled);
|
||||
|
||||
for (DateTime d in selectedDates) {
|
||||
final selectedDateType = selectableLogic.getDayType(d);
|
||||
expect(selectedDateType, DayType.single,
|
||||
reason: "Incorrect DayType for the date ${d.toString()}");
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
31
flutter_date_pickers-master/test/day_selectable_test.dart
Normal file
31
flutter_date_pickers-master/test/day_selectable_test.dart
Normal file
@@ -0,0 +1,31 @@
|
||||
import 'package:flutter_date_pickers/src/day_type.dart';
|
||||
import 'package:flutter_date_pickers/src/i_selectable_picker.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
import 'date_time_utils.dart';
|
||||
|
||||
void main() {
|
||||
group("DaySelectable test.", () {
|
||||
test("getDayType() returns correct type for different dates", () {
|
||||
final selectedDate = DateTime.now();
|
||||
final firstDate = selectedDate.subtract(const Duration(days: 10));
|
||||
final lastDate = selectedDate.add(const Duration(days: 10));
|
||||
final disabledDate = selectedDate.subtract(const Duration(days: 1));
|
||||
|
||||
// ignore: prefer_function_declarations_over_variables
|
||||
final selectablePredicate = (DateTime d)
|
||||
=> !DateTimeUtils.sameDate(d, disabledDate);
|
||||
|
||||
final selectableLogic = DaySelectable(
|
||||
selectedDate, firstDate, lastDate,
|
||||
selectableDayPredicate: selectablePredicate);
|
||||
final selectedDateType = selectableLogic.getDayType(selectedDate);
|
||||
final notSelectedEnabledDateType = selectableLogic.getDayType(firstDate);
|
||||
final disabledDateType = selectableLogic.getDayType(disabledDate);
|
||||
|
||||
expect(selectedDateType, DayType.single);
|
||||
expect(notSelectedEnabledDateType, DayType.notSelected);
|
||||
expect(disabledDateType, DayType.disabled);
|
||||
});
|
||||
});
|
||||
}
|
||||
55
flutter_date_pickers-master/test/range_selectable_test.dart
Normal file
55
flutter_date_pickers-master/test/range_selectable_test.dart
Normal file
@@ -0,0 +1,55 @@
|
||||
import 'package:flutter_date_pickers/flutter_date_pickers.dart';
|
||||
import 'package:flutter_date_pickers/src/day_type.dart';
|
||||
import 'package:flutter_date_pickers/src/i_selectable_picker.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
import 'date_time_utils.dart';
|
||||
|
||||
void main() {
|
||||
group("RangeSelectable test.", () {
|
||||
test("getDayType() returns correct type for different dates", () {
|
||||
final now = DateTime.now();
|
||||
final startPeriod = now.subtract(const Duration(days: 3));
|
||||
final endPeriod = now.add(const Duration(days: 3));
|
||||
final selectedPeriod = DatePeriod(startPeriod, endPeriod);
|
||||
|
||||
final firstDate = now.subtract(const Duration(days: 10));
|
||||
final lastDate = now.add(const Duration(days: 10));
|
||||
final disabledDate = now.subtract(const Duration(days: 5));
|
||||
|
||||
// ignore: prefer_function_declarations_over_variables
|
||||
final selectablePredicate = (DateTime d)
|
||||
=> !DateTimeUtils.sameDate(d, disabledDate);
|
||||
|
||||
final selectableLogic = RangeSelectable(
|
||||
selectedPeriod, firstDate, lastDate,
|
||||
selectableDayPredicate: selectablePredicate);
|
||||
|
||||
final notSelectedEnabledDateType = selectableLogic.getDayType(firstDate);
|
||||
expect(notSelectedEnabledDateType, DayType.notSelected);
|
||||
|
||||
final disabledDateType = selectableLogic.getDayType(disabledDate);
|
||||
expect(disabledDateType, DayType.disabled);
|
||||
|
||||
final startPeriodDateType = selectableLogic.getDayType(startPeriod);
|
||||
expect(startPeriodDateType, DayType.start);
|
||||
|
||||
final endPeriodDateType = selectableLogic.getDayType(endPeriod);
|
||||
expect(endPeriodDateType, DayType.end);
|
||||
|
||||
final periodDays = endPeriod.difference(startPeriod).inDays;
|
||||
|
||||
// Count of the day period which is not start and not end.
|
||||
final middleDaysCount = periodDays - 2;
|
||||
final middleDates = List.generate(middleDaysCount,
|
||||
(i) => startPeriod.add(Duration(days: i + 1)));
|
||||
|
||||
for (DateTime date in middleDates) {
|
||||
final middlePeriodDateType = selectableLogic.getDayType(date);
|
||||
expect(middlePeriodDateType, DayType.middle,
|
||||
reason: "Incorrect DayType for the date ${date.toString()} "
|
||||
"in period ${startPeriod.toString()} - ${endPeriod.toString()}");
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
56
flutter_date_pickers-master/test/week_selectabl_test.dart
Normal file
56
flutter_date_pickers-master/test/week_selectabl_test.dart
Normal file
@@ -0,0 +1,56 @@
|
||||
import 'package:flutter_date_pickers/src/day_type.dart';
|
||||
import 'package:flutter_date_pickers/src/i_selectable_picker.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
import 'date_time_utils.dart';
|
||||
|
||||
void main() {
|
||||
group("WeekSelectable test.", () {
|
||||
test("getDayType() returns correct type for different dates", () {
|
||||
final selectedDate = DateTime(2020, 10, 5); // Monday
|
||||
final firstDayOfWeekIndex = 1; // Week starts from Monday
|
||||
|
||||
final firstDate = selectedDate.subtract(const Duration(days: 10));
|
||||
final lastDate = selectedDate.add(const Duration(days: 10));
|
||||
final disabledDate = selectedDate.subtract(const Duration(days: 5));
|
||||
|
||||
// ignore: prefer_function_declarations_over_variables
|
||||
final selectablePredicate = (DateTime d)
|
||||
=> !DateTimeUtils.sameDate(d, disabledDate);
|
||||
|
||||
final selectableLogic = WeekSelectable(
|
||||
selectedDate, firstDayOfWeekIndex, firstDate, lastDate,
|
||||
selectableDayPredicate: selectablePredicate);
|
||||
|
||||
final notSelectedEnabledDateType = selectableLogic.getDayType(firstDate);
|
||||
expect(notSelectedEnabledDateType, DayType.notSelected);
|
||||
|
||||
final disabledDateType = selectableLogic.getDayType(disabledDate);
|
||||
expect(disabledDateType, DayType.disabled);
|
||||
|
||||
final weekStart = DateTimeUtils
|
||||
.getFirstDayOfWeek(selectedDate, firstDayOfWeekIndex);
|
||||
|
||||
final weekEnd = DateTimeUtils
|
||||
.getLastDayOfWeek(selectedDate, firstDayOfWeekIndex);
|
||||
|
||||
final startPeriodDateType = selectableLogic.getDayType(weekStart);
|
||||
expect(startPeriodDateType, DayType.start);
|
||||
|
||||
final endPeriodDateType = selectableLogic.getDayType(weekEnd);
|
||||
expect(endPeriodDateType, DayType.end);
|
||||
|
||||
// Count of the week days which is not start and not end (7 - 2).
|
||||
final middleDaysCount = 5;
|
||||
final middleDates = List.generate(middleDaysCount,
|
||||
(i) => weekStart.add(Duration(days: i + 1)));
|
||||
|
||||
for (DateTime date in middleDates) {
|
||||
final middlePeriodDateType = selectableLogic.getDayType(date);
|
||||
expect(middlePeriodDateType, DayType.middle,
|
||||
reason: "Incorrect DayType for the date ${date.toString()} "
|
||||
"in week ${weekStart.toString()} - ${weekEnd.toString()}");
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user