diff --git a/lib/calendarro.dart b/lib/calendarro.dart index 07eff6d..4c1e77d 100644 --- a/lib/calendarro.dart +++ b/lib/calendarro.dart @@ -3,7 +3,7 @@ library calendarro; import 'package:calendarro/calendarro_page.dart'; import 'package:calendarro/date_range.dart'; import 'package:calendarro/default_weekday_labels_row.dart'; -import 'package:calendarro/date_utils.dart'; +import 'package:calendarro/date_utils.dart' as dateUtil; import 'package:calendarro/default_day_tile_builder.dart'; import 'package:flutter/material.dart'; @@ -50,14 +50,14 @@ class Calendarro extends StatefulWidget { this.weekdayLabelsRow, }) : super(key: key) { if (startDate == null) { - startDate = DateUtils.getFirstDayOfCurrentMonth(); + startDate = dateUtil.DateUtils.getFirstDayOfCurrentMonth(); } - startDate = DateUtils.toMidnight(startDate); + startDate = dateUtil.DateUtils.toMidnight(startDate); if (endDate == null) { - endDate = DateUtils.getLastDayOfCurrentMonth(); + endDate = dateUtil.DateUtils.getLastDayOfCurrentMonth(); } - endDate = DateUtils.toMidnight(endDate); + endDate = dateUtil.DateUtils.toMidnight(endDate); if (startDate.isAfter(endDate)) { throw new ArgumentError("Calendarro: startDate is after the endDate"); @@ -77,14 +77,11 @@ class Calendarro extends StatefulWidget { } } - static CalendarroState of(BuildContext context) => - context.ancestorStateOfType(const TypeMatcher()); - + static CalendarroState of(BuildContext context) => context.findAncestorStateOfType(); + @override CalendarroState createState() { - state = CalendarroState( - selectedSingleDate: selectedSingleDate, - selectedDates: selectedDates); + state = CalendarroState(selectedSingleDate: selectedSingleDate, selectedDates: selectedDates); return state; } @@ -102,9 +99,7 @@ class Calendarro extends StatefulWidget { int getPositionOfDate(DateTime date) { int daysDifference = - date - .difference(DateUtils.toMidnight(startDate)) - .inDays; + date.difference(dateUtil.DateUtils.toMidnight(startDate)).inDays; int weekendsDifference = ((daysDifference + startDate.weekday) / 7).toInt(); var position = daysDifference - weekendsDifference * 2; return position; @@ -112,9 +107,7 @@ class Calendarro extends StatefulWidget { int getPageForDate(DateTime date) { if (displayMode == DisplayMode.WEEKS) { - int daysDifferenceFromStartDate = date - .difference(startDate) - .inDays; + int daysDifferenceFromStartDate = date.difference(startDate).inDays; int page = (daysDifferenceFromStartDate + startDayOffset) ~/ 7; return page; } else { @@ -132,10 +125,7 @@ class CalendarroState extends State { int pagesCount; PageView pageView; - CalendarroState({ - this.selectedSingleDate, - this.selectedDates - }); + CalendarroState({this.selectedSingleDate, this.selectedDates}); @override void initState() { @@ -175,17 +165,18 @@ class CalendarroState extends State { int lastPage = widget.getPageForDate(widget.endDate); pagesCount = lastPage + 1; } else { - pagesCount = DateUtils.calculateMonthsDifference( - widget.startDate, - widget.endDate) + 1; + pagesCount = dateUtil.DateUtils.calculateMonthsDifference( + widget.startDate, widget.endDate) + + 1; } pageView = PageView.builder( itemBuilder: (context, position) => _buildCalendarPage(position), itemCount: pagesCount, controller: PageController( - initialPage: - selectedSingleDate != null ? widget.getPageForDate(selectedSingleDate) : 0), + initialPage: selectedSingleDate != null + ? widget.getPageForDate(selectedSingleDate) + : 0), onPageChanged: (page) { if (widget.onPageSelected != null) { DateRange pageDateRange = _calculatePageDateRange(page); @@ -198,16 +189,13 @@ class CalendarroState extends State { if (widget.displayMode == DisplayMode.WEEKS) { widgetHeight = widget.dayLabelHeight + widget.dayTileHeight; } else { - var maxWeeksNumber = DateUtils.calculateMaxWeeksNumberMonthly( - widget.startDate, - widget.endDate); - widgetHeight = widget.dayLabelHeight - + maxWeeksNumber * widget.dayTileHeight; + var maxWeeksNumber = dateUtil.DateUtils.calculateMaxWeeksNumberMonthly( + widget.startDate, widget.endDate); + widgetHeight = + widget.dayLabelHeight + maxWeeksNumber * widget.dayTileHeight; } - return Container( - height: widgetHeight, - child: pageView); + return Container(height: widgetHeight, child: pageView); } bool isDateSelected(DateTime date) { @@ -216,10 +204,9 @@ class CalendarroState extends State { return DateUtils.isSameDay(selectedSingleDate, date); break; case SelectionMode.MULTI: - final matchedSelectedDate = selectedDates.firstWhere((currentDate) => - DateUtils.isSameDay(currentDate, date), - orElse: () => null - ); + final matchedSelectedDate = selectedDates.firstWhere( + (currentDate) => DateUtils.isSameDay(currentDate, date), + orElse: () => null); return matchedSelectedDate != null; break; @@ -230,11 +217,11 @@ class CalendarroState extends State { case 1: return DateUtils.isSameDay(selectedDates[0], date); default: - var dateBetweenDatesRange = (date.isAfter(selectedDates[0]) - && date.isBefore(selectedDates[1])); - return DateUtils.isSameDay(date, selectedDates[0]) - || DateUtils.isSameDay(date, selectedDates[1]) - || dateBetweenDatesRange; + var dateBetweenDatesRange = (date.isAfter(selectedDates[0]) && + date.isBefore(selectedDates[1])); + return DateUtils.isSameDay(date, selectedDates[0]) || + DateUtils.isSameDay(date, selectedDates[1]) || + dateBetweenDatesRange; } break; } @@ -301,18 +288,19 @@ class CalendarroState extends State { if (pagesCount <= 1) { pageEndDate = widget.endDate; } else { - var lastDayOfMonth = DateUtils.getLastDayOfMonth(widget.startDate); + var lastDayOfMonth = + dateUtil.DateUtils.getLastDayOfMonth(widget.startDate); pageEndDate = lastDayOfMonth; } } else if (pagePosition == pagesCount - 1) { - pageStartDate = DateUtils.getFirstDayOfMonth(widget.endDate); + pageStartDate = dateUtil.DateUtils.getFirstDayOfMonth(widget.endDate); pageEndDate = widget.endDate; } else { - DateTime firstDateOfCurrentMonth = DateUtils.addMonths( - widget.startDate, - pagePosition); + DateTime firstDateOfCurrentMonth = + dateUtil.DateUtils.addMonths(widget.startDate, pagePosition); pageStartDate = firstDateOfCurrentMonth; - pageEndDate = DateUtils.getLastDayOfMonth(firstDateOfCurrentMonth); + pageEndDate = + dateUtil.DateUtils.getLastDayOfMonth(firstDateOfCurrentMonth); } return DateRange(pageStartDate, pageEndDate); @@ -324,16 +312,16 @@ class CalendarroState extends State { if (pagePosition == 0) { pageStartDate = widget.startDate; - pageEndDate = - DateUtils.addDaysToDate(widget.startDate, 6 - widget.startDayOffset); + pageEndDate = dateUtil.DateUtils.addDaysToDate( + widget.startDate, 6 - widget.startDayOffset); } else if (pagePosition == pagesCount - 1) { - pageStartDate = DateUtils.addDaysToDate( + pageStartDate = dateUtil.DateUtils.addDaysToDate( widget.startDate, 7 * pagePosition - widget.startDayOffset); pageEndDate = widget.endDate; } else { - pageStartDate = DateUtils.addDaysToDate( + pageStartDate = dateUtil.DateUtils.addDaysToDate( widget.startDate, 7 * pagePosition - widget.startDayOffset); - pageEndDate = DateUtils.addDaysToDate( + pageEndDate = dateUtil.DateUtils.addDaysToDate( widget.startDate, 7 * pagePosition + 6 - widget.startDayOffset); } @@ -363,10 +351,9 @@ class CalendarroState extends State { } void _setMultiSelectedDate(DateTime date) { - final alreadyExistingDate = selectedDates.firstWhere((currentDate) => - DateUtils.isSameDay(currentDate, date), - orElse: () => null - ); + final alreadyExistingDate = selectedDates.firstWhere( + (currentDate) => dateUtil.DateUtils.isSameDay(currentDate, date), + orElse: () => null); if (alreadyExistingDate != null) { selectedDates.remove(alreadyExistingDate); diff --git a/lib/calendarro_page.dart b/lib/calendarro_page.dart index 9dce19e..bcddb5b 100644 --- a/lib/calendarro_page.dart +++ b/lib/calendarro_page.dart @@ -1,10 +1,9 @@ import 'package:calendarro/calendarro.dart'; -import 'package:calendarro/date_utils.dart'; +import 'package:calendarro/date_utils.dart' as dateUtil; import 'package:calendarro/default_weekday_labels_row.dart'; import 'package:flutter/material.dart'; class CalendarroPage extends StatelessWidget { - static final MAX_ROWS_COUNT = 6; DateTime pageStartDate; @@ -13,11 +12,8 @@ class CalendarroPage extends StatelessWidget { int startDayOffset; - CalendarroPage({ - this.pageStartDate, - this.pageEndDate, - this.weekdayLabelsRow - }) { + CalendarroPage( + {this.pageStartDate, this.pageEndDate, this.weekdayLabelsRow}) { startDayOffset = pageStartDate.weekday - DateTime.monday; } @@ -25,32 +21,30 @@ class CalendarroPage extends StatelessWidget { Widget build(BuildContext context) { return Container( child: Column( - children: buildRows(context), - mainAxisSize: MainAxisSize.min - ) - ); + children: buildRows(context), mainAxisSize: MainAxisSize.min)); } List buildRows(BuildContext context) { List rows = []; rows.add(weekdayLabelsRow); - DateTime rowLastDayDate = DateUtils.addDaysToDate(pageStartDate, 6 - startDayOffset); + DateTime rowLastDayDate = + dateUtil.DateUtils.addDaysToDate(pageStartDate, 6 - startDayOffset); if (pageEndDate.isAfter(rowLastDayDate)) { rows.add(Row( - children: buildCalendarRow(context, pageStartDate, rowLastDayDate)) - ); + children: buildCalendarRow(context, pageStartDate, rowLastDayDate))); for (var i = 1; i < MAX_ROWS_COUNT; i++) { - DateTime nextRowFirstDayDate = DateUtils.addDaysToDate(pageStartDate, 7 * i - startDayOffset); + DateTime nextRowFirstDayDate = dateUtil.DateUtils.addDaysToDate( + pageStartDate, 7 * i - startDayOffset); if (nextRowFirstDayDate.isAfter(pageEndDate)) { break; } - DateTime nextRowLastDayDate = DateUtils.addDaysToDate(pageStartDate, 7 * i - startDayOffset + 6); - + DateTime nextRowLastDayDate = dateUtil.DateUtils.addDaysToDate( + pageStartDate, 7 * i - startDayOffset + 6); if (nextRowLastDayDate.isAfter(pageEndDate)) { nextRowLastDayDate = pageEndDate; @@ -61,9 +55,8 @@ class CalendarroPage extends StatelessWidget { context, nextRowFirstDayDate, nextRowLastDayDate))); } } else { - rows.add(Row( - children: buildCalendarRow(context, pageStartDate, pageEndDate)) - ); + rows.add( + Row(children: buildCalendarRow(context, pageStartDate, pageEndDate))); } return rows; @@ -77,9 +70,9 @@ class CalendarroPage extends StatelessWidget { for (int i = 0; i < 7; i++) { if (i + 1 >= rowStartDate.weekday && i + 1 <= rowEndDate.weekday) { CalendarroState calendarroState = Calendarro.of(context); - Widget dayTile = calendarroState.widget.dayTileBuilder - .build(context, currentDate, calendarroState.widget.onTap); - items.add(dayTile); + Widget dayTile = calendarroState.widget.dayTileBuilder + .build(context, currentDate, calendarroState.widget.onTap); + items.add(dayTile); currentDate = currentDate.add(Duration(days: 1)); } else { items.add(Expanded( diff --git a/lib/default_day_tile.dart b/lib/default_day_tile.dart index 4f303cc..7b6a1c2 100644 --- a/lib/default_day_tile.dart +++ b/lib/default_day_tile.dart @@ -1,5 +1,5 @@ import 'package:calendarro/calendarro.dart'; -import 'package:calendarro/date_utils.dart'; +import 'package:calendarro/date_utils.dart' as dateUtil; import 'package:flutter/material.dart'; class CalendarroDayItem extends StatelessWidget { @@ -11,9 +11,9 @@ class CalendarroDayItem extends StatelessWidget { @override Widget build(BuildContext context) { - bool isWeekend = DateUtils.isWeekend(date); + bool isWeekend = dateUtil.DateUtils.isWeekend(date); var textColor = isWeekend ? Colors.grey : Colors.black; - bool isToday = DateUtils.isToday(date); + bool isToday = dateUtil.DateUtils.isToday(date); calendarroState = Calendarro.of(context); bool daySelected = calendarroState.isDateSelected(date); @@ -32,18 +32,18 @@ class CalendarroDayItem extends StatelessWidget { return Expanded( child: GestureDetector( - child: Container( - height: 40.0, - decoration: boxDecoration, - child: Center( - child: Text( - "${date.day}", - textAlign: TextAlign.center, - style: TextStyle(color: textColor), - ))), - onTap: handleTap, - behavior: HitTestBehavior.translucent, - )); + child: Container( + height: 40.0, + decoration: boxDecoration, + child: Center( + child: Text( + "${date.day}", + textAlign: TextAlign.center, + style: TextStyle(color: textColor), + ))), + onTap: handleTap, + behavior: HitTestBehavior.translucent, + )); } void handleTap() { @@ -54,4 +54,4 @@ class CalendarroDayItem extends StatelessWidget { calendarroState.setSelectedDate(date); calendarroState.setCurrentDate(date); } -} \ No newline at end of file +} diff --git a/pubspec.lock b/pubspec.lock index 7642229..8426fcf 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -7,28 +7,49 @@ packages: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.2.0" + version: "2.5.0-nullsafety.3" boolean_selector: dependency: transitive description: name: boolean_selector url: "https://pub.dartlang.org" source: hosted - version: "1.0.4" + version: "2.1.0-nullsafety.3" + characters: + dependency: transitive + description: + name: characters + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0-nullsafety.5" charcode: dependency: transitive description: name: charcode url: "https://pub.dartlang.org" source: hosted - version: "1.1.2" + version: "1.2.0-nullsafety.3" + clock: + dependency: transitive + description: + name: clock + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0-nullsafety.3" collection: dependency: transitive description: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.14.11" + version: "1.15.0-nullsafety.5" + fake_async: + dependency: transitive + description: + name: fake_async + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.0-nullsafety.3" flutter: dependency: "direct main" description: flutter @@ -45,35 +66,21 @@ packages: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.5" + version: "0.12.10-nullsafety.3" meta: dependency: transitive description: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.1.6" + version: "1.3.0-nullsafety.6" path: dependency: transitive description: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.6.2" - pedantic: - dependency: transitive - description: - name: pedantic - url: "https://pub.dartlang.org" - source: hosted - version: "1.7.0" - quiver: - dependency: transitive - description: - name: quiver - url: "https://pub.dartlang.org" - source: hosted - version: "2.0.3" + version: "1.8.0-nullsafety.3" sky_engine: dependency: transitive description: flutter @@ -85,55 +92,55 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.5.5" + version: "1.8.0-nullsafety.4" stack_trace: dependency: transitive description: name: stack_trace url: "https://pub.dartlang.org" source: hosted - version: "1.9.3" + version: "1.10.0-nullsafety.6" stream_channel: dependency: transitive description: name: stream_channel url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.1.0-nullsafety.3" string_scanner: dependency: transitive description: name: string_scanner url: "https://pub.dartlang.org" source: hosted - version: "1.0.4" + version: "1.1.0-nullsafety.3" term_glyph: dependency: transitive description: name: term_glyph url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.2.0-nullsafety.3" test_api: dependency: transitive description: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.5" + version: "0.2.19-nullsafety.6" typed_data: dependency: transitive description: name: typed_data url: "https://pub.dartlang.org" source: hosted - version: "1.1.6" + version: "1.3.0-nullsafety.5" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.0.8" + version: "2.1.0-nullsafety.5" sdks: - dart: ">=2.2.2 <3.0.0" + dart: ">=2.12.0-0.0 <3.0.0"