Skip to content

Commit

Permalink
Merge pull request #46 from ProbablyRational/master
Browse files Browse the repository at this point in the history
#45_43: DateUtils and ancestorStateOfType fixes
  • Loading branch information
adamstyrc authored Sep 7, 2021
2 parents f2870ca + 9f1172c commit 490d2fc
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 127 deletions.
103 changes: 45 additions & 58 deletions lib/calendarro.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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");
Expand All @@ -77,14 +77,11 @@ class Calendarro extends StatefulWidget {
}
}

static CalendarroState of(BuildContext context) =>
context.ancestorStateOfType(const TypeMatcher<CalendarroState>());

static CalendarroState of(BuildContext context) => context.findAncestorStateOfType<CalendarroState>();

@override
CalendarroState createState() {
state = CalendarroState(
selectedSingleDate: selectedSingleDate,
selectedDates: selectedDates);
state = CalendarroState(selectedSingleDate: selectedSingleDate, selectedDates: selectedDates);
return state;
}

Expand All @@ -102,19 +99,15 @@ 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;
}

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 {
Expand All @@ -132,10 +125,7 @@ class CalendarroState extends State<Calendarro> {
int pagesCount;
PageView pageView;

CalendarroState({
this.selectedSingleDate,
this.selectedDates
});
CalendarroState({this.selectedSingleDate, this.selectedDates});

@override
void initState() {
Expand Down Expand Up @@ -175,17 +165,18 @@ class CalendarroState extends State<Calendarro> {
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);
Expand All @@ -198,16 +189,13 @@ class CalendarroState extends State<Calendarro> {
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) {
Expand All @@ -216,10 +204,9 @@ class CalendarroState extends State<Calendarro> {
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;
Expand All @@ -230,11 +217,11 @@ class CalendarroState extends State<Calendarro> {
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;
}
Expand Down Expand Up @@ -301,18 +288,19 @@ class CalendarroState extends State<Calendarro> {
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);
Expand All @@ -324,16 +312,16 @@ class CalendarroState extends State<Calendarro> {

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);
}

Expand Down Expand Up @@ -363,10 +351,9 @@ class CalendarroState extends State<Calendarro> {
}

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);
Expand Down
39 changes: 16 additions & 23 deletions lib/calendarro_page.dart
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,44 +12,39 @@ 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;
}

@override
Widget build(BuildContext context) {
return Container(
child: Column(
children: buildRows(context),
mainAxisSize: MainAxisSize.min
)
);
children: buildRows(context), mainAxisSize: MainAxisSize.min));
}

List<Widget> buildRows(BuildContext context) {
List<Widget> 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;
Expand All @@ -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;
Expand All @@ -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(
Expand Down
32 changes: 16 additions & 16 deletions lib/default_day_tile.dart
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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);
Expand All @@ -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() {
Expand All @@ -54,4 +54,4 @@ class CalendarroDayItem extends StatelessWidget {
calendarroState.setSelectedDate(date);
calendarroState.setCurrentDate(date);
}
}
}
Loading

0 comments on commit 490d2fc

Please sign in to comment.