From c1270ab5eb5a6d468022eaa70404010c773202eb Mon Sep 17 00:00:00 2001 From: Keita Watanabe Date: Fri, 4 Jun 2021 09:07:35 +0900 Subject: [PATCH 01/10] update sdk version --- example/pubspec.lock | 69 ++++++++++++++++++++++++-------------------- example/pubspec.yaml | 3 ++ pubspec.lock | 67 +++++++++++++++++++++++------------------- pubspec.yaml | 2 +- 4 files changed, 79 insertions(+), 62 deletions(-) diff --git a/example/pubspec.lock b/example/pubspec.lock index e92c641..d0ae838 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -7,35 +7,49 @@ packages: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.2.0" + version: "2.6.1" boolean_selector: dependency: transitive description: name: boolean_selector url: "https://pub.dartlang.org" source: hosted - version: "1.0.4" + version: "2.1.0" calendarro: dependency: "direct dev" description: path: ".." relative: true source: path - version: "1.0.1" + version: "1.2.0" + characters: + dependency: transitive + description: + name: characters + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0" charcode: dependency: transitive description: name: charcode url: "https://pub.dartlang.org" source: hosted - version: "1.1.2" + version: "1.2.0" + clock: + dependency: transitive + description: + name: clock + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0" collection: dependency: transitive description: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.14.11" + version: "1.15.0" cupertino_icons: dependency: "direct main" description: @@ -43,6 +57,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.1.2" + fake_async: + dependency: transitive + description: + name: fake_async + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.0" flutter: dependency: "direct main" description: flutter @@ -59,35 +80,21 @@ packages: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.5" + version: "0.12.10" meta: dependency: transitive description: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.1.6" + version: "1.3.0" 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" sky_engine: dependency: transitive description: flutter @@ -99,55 +106,55 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.5.5" + version: "1.8.1" stack_trace: dependency: transitive description: name: stack_trace url: "https://pub.dartlang.org" source: hosted - version: "1.9.3" + version: "1.10.0" stream_channel: dependency: transitive description: name: stream_channel url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.1.0" string_scanner: dependency: transitive description: name: string_scanner url: "https://pub.dartlang.org" source: hosted - version: "1.0.4" + version: "1.1.0" term_glyph: dependency: transitive description: name: term_glyph url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.2.0" test_api: dependency: transitive description: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.5" + version: "0.3.0" typed_data: dependency: transitive description: name: typed_data url: "https://pub.dartlang.org" source: hosted - version: "1.1.6" + version: "1.3.0" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.0.8" + version: "2.1.0" sdks: - dart: ">=2.2.2 <3.0.0" + dart: ">=2.12.0 <3.0.0" diff --git a/example/pubspec.yaml b/example/pubspec.yaml index b7de302..6fa6594 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -1,6 +1,9 @@ name: sample description: sample app +environment: + sdk: ">=2.12.0 <3.0.0" + dependencies: flutter: sdk: flutter diff --git a/pubspec.lock b/pubspec.lock index 7642229..ad6d1df 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.6.1" boolean_selector: dependency: transitive description: name: boolean_selector url: "https://pub.dartlang.org" source: hosted - version: "1.0.4" + version: "2.1.0" + characters: + dependency: transitive + description: + name: characters + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0" charcode: dependency: transitive description: name: charcode url: "https://pub.dartlang.org" source: hosted - version: "1.1.2" + version: "1.2.0" + clock: + dependency: transitive + description: + name: clock + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0" collection: dependency: transitive description: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.14.11" + version: "1.15.0" + fake_async: + dependency: transitive + description: + name: fake_async + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.0" 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" meta: dependency: transitive description: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.1.6" + version: "1.3.0" 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" 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.1" stack_trace: dependency: transitive description: name: stack_trace url: "https://pub.dartlang.org" source: hosted - version: "1.9.3" + version: "1.10.0" stream_channel: dependency: transitive description: name: stream_channel url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.1.0" string_scanner: dependency: transitive description: name: string_scanner url: "https://pub.dartlang.org" source: hosted - version: "1.0.4" + version: "1.1.0" term_glyph: dependency: transitive description: name: term_glyph url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.2.0" test_api: dependency: transitive description: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.5" + version: "0.3.0" typed_data: dependency: transitive description: name: typed_data url: "https://pub.dartlang.org" source: hosted - version: "1.1.6" + version: "1.3.0" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.0.8" + version: "2.1.0" sdks: - dart: ">=2.2.2 <3.0.0" + dart: ">=2.12.0 <3.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index 0318ad7..850a09c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -4,7 +4,7 @@ version: 1.2.0 homepage: https://github.com/adamstyrc/calendarro environment: - sdk: '>1.19.0 <3.0.0' + sdk: ">=2.12.0 <3.0.0" dependencies: flutter: From c39b6591a368f068076025903671d0e8c841d0c4 Mon Sep 17 00:00:00 2001 From: Keita Watanabe Date: Fri, 4 Jun 2021 09:11:52 +0900 Subject: [PATCH 02/10] fix CalendarroPage constructor --- lib/calendarro_page.dart | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/calendarro_page.dart b/lib/calendarro_page.dart index 9dce19e..25bd53c 100644 --- a/lib/calendarro_page.dart +++ b/lib/calendarro_page.dart @@ -14,12 +14,10 @@ class CalendarroPage extends StatelessWidget { int startDayOffset; CalendarroPage({ - this.pageStartDate, - this.pageEndDate, - this.weekdayLabelsRow - }) { - startDayOffset = pageStartDate.weekday - DateTime.monday; - } + required this.pageStartDate, + required this.pageEndDate, + required this.weekdayLabelsRow + }) : startDayOffset = pageStartDate.weekday - DateTime.monday; @override Widget build(BuildContext context) { From 84099b70efdfca5169b425de549e0c07ba883061 Mon Sep 17 00:00:00 2001 From: Keita Watanabe Date: Fri, 4 Jun 2021 09:21:52 +0900 Subject: [PATCH 03/10] fix DateUtils imports --- example/lib/main.dart | 2 +- lib/calendarro.dart | 2 +- lib/calendarro_page.dart | 2 +- lib/default_day_tile.dart | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 96ad596..cb29c76 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,5 +1,5 @@ import 'package:calendarro/date_utils.dart'; -import 'package:flutter/material.dart'; +import 'package:flutter/material.dart' hide DateUtils; import 'package:calendarro/calendarro.dart'; void main() => runApp(new MyApp()); diff --git a/lib/calendarro.dart b/lib/calendarro.dart index 07eff6d..2b52391 100644 --- a/lib/calendarro.dart +++ b/lib/calendarro.dart @@ -5,7 +5,7 @@ import 'package:calendarro/date_range.dart'; import 'package:calendarro/default_weekday_labels_row.dart'; import 'package:calendarro/date_utils.dart'; import 'package:calendarro/default_day_tile_builder.dart'; -import 'package:flutter/material.dart'; +import 'package:flutter/material.dart' hide DateUtils; abstract class DayTileBuilder { Widget build(BuildContext context, DateTime date, DateTimeCallback onTap); diff --git a/lib/calendarro_page.dart b/lib/calendarro_page.dart index 25bd53c..518285f 100644 --- a/lib/calendarro_page.dart +++ b/lib/calendarro_page.dart @@ -1,7 +1,7 @@ import 'package:calendarro/calendarro.dart'; import 'package:calendarro/date_utils.dart'; import 'package:calendarro/default_weekday_labels_row.dart'; -import 'package:flutter/material.dart'; +import 'package:flutter/material.dart' hide DateUtils; class CalendarroPage extends StatelessWidget { diff --git a/lib/default_day_tile.dart b/lib/default_day_tile.dart index 4f303cc..e2f5160 100644 --- a/lib/default_day_tile.dart +++ b/lib/default_day_tile.dart @@ -1,6 +1,6 @@ import 'package:calendarro/calendarro.dart'; import 'package:calendarro/date_utils.dart'; -import 'package:flutter/material.dart'; +import 'package:flutter/material.dart' hide DateUtils; class CalendarroDayItem extends StatelessWidget { CalendarroDayItem({this.date, this.calendarroState, this.onTap}); From 69fa6848608bad0cf2321166c2fa5e5b47ea1cf8 Mon Sep 17 00:00:00 2001 From: Keita Watanabe Date: Fri, 4 Jun 2021 09:23:34 +0900 Subject: [PATCH 04/10] fix list --- lib/date_utils.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/date_utils.dart b/lib/date_utils.dart index 78de495..37a5044 100644 --- a/lib/date_utils.dart +++ b/lib/date_utils.dart @@ -99,7 +99,7 @@ class DateUtils { int monthsNumber = calculateMonthsDifference(startDate, endDate); - List weeksNumbersMonthly = List(); + List weeksNumbersMonthly = []; if (monthsNumber == 0) { From bb3ef596806767bf574cfb87d9bebde70562d3d4 Mon Sep 17 00:00:00 2001 From: Keita Watanabe Date: Fri, 4 Jun 2021 09:29:54 +0900 Subject: [PATCH 05/10] fix CalendarroDayItem --- lib/default_day_tile.dart | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/default_day_tile.dart b/lib/default_day_tile.dart index e2f5160..3e44187 100644 --- a/lib/default_day_tile.dart +++ b/lib/default_day_tile.dart @@ -3,22 +3,25 @@ import 'package:calendarro/date_utils.dart'; import 'package:flutter/material.dart' hide DateUtils; class CalendarroDayItem extends StatelessWidget { - CalendarroDayItem({this.date, this.calendarroState, this.onTap}); + CalendarroDayItem({ + required this.date, + required this.calendarroState, + this.onTap, + }); DateTime date; CalendarroState calendarroState; - DateTimeCallback onTap; + DateTimeCallback? onTap; @override Widget build(BuildContext context) { bool isWeekend = DateUtils.isWeekend(date); var textColor = isWeekend ? Colors.grey : Colors.black; bool isToday = DateUtils.isToday(date); - calendarroState = Calendarro.of(context); bool daySelected = calendarroState.isDateSelected(date); - BoxDecoration boxDecoration; + BoxDecoration? boxDecoration; if (daySelected) { boxDecoration = BoxDecoration(color: Colors.blue, shape: BoxShape.circle); } else if (isToday) { @@ -47,9 +50,7 @@ class CalendarroDayItem extends StatelessWidget { } void handleTap() { - if (onTap != null) { - onTap(date); - } + onTap?.call(date); calendarroState.setSelectedDate(date); calendarroState.setCurrentDate(date); From 28e63f765afbc6b2933075c179b498145389d054 Mon Sep 17 00:00:00 2001 From: Keita Watanabe Date: Fri, 4 Jun 2021 09:31:58 +0900 Subject: [PATCH 06/10] fix main --- example/lib/main.dart | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index cb29c76..663dae3 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -19,15 +19,14 @@ class MyApp extends StatelessWidget { class MyHomePage extends StatelessWidget { final String title; - Calendarro monthCalendarro; - MyHomePage({Key key, this.title}) : super(key: key); + MyHomePage({Key? key, required this.title}) : super(key: key); @override Widget build(BuildContext context) { var startDate = DateUtils.getFirstDayOfCurrentMonth(); var endDate = DateUtils.getLastDayOfNextMonth(); - monthCalendarro = Calendarro( + final monthCalendarro = Calendarro( startDate: startDate, endDate: endDate, displayMode: DisplayMode.MONTHS, From c1c2f0b81e1ff3fdc4f0ba8dec4c34483c914390 Mon Sep 17 00:00:00 2001 From: Keita Watanabe Date: Fri, 4 Jun 2021 09:40:56 +0900 Subject: [PATCH 07/10] fix firstWhereOrElse --- lib/calendarro.dart | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/calendarro.dart b/lib/calendarro.dart index 2b52391..6c925a6 100644 --- a/lib/calendarro.dart +++ b/lib/calendarro.dart @@ -216,10 +216,8 @@ 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.firstWhereOrNull( + (currentDate) => DateUtils.isSameDay(currentDate, date)); return matchedSelectedDate != null; break; @@ -363,10 +361,8 @@ class CalendarroState extends State { } void _setMultiSelectedDate(DateTime date) { - final alreadyExistingDate = selectedDates.firstWhere((currentDate) => - DateUtils.isSameDay(currentDate, date), - orElse: () => null - ); + final alreadyExistingDate = selectedDates.firstWhereOrNull((currentDate) => + DateUtils.isSameDay(currentDate, date)); if (alreadyExistingDate != null) { selectedDates.remove(alreadyExistingDate); @@ -375,3 +371,10 @@ class CalendarroState extends State { } } } + +extension IterableExt on Iterable { + T? firstWhereOrNull(bool Function(T element) test) { + final list = where(test); + return list.isEmpty ? null : list.first; + } +} From 1849fdc5f2a7d330456d74debeea6146705d2ca7 Mon Sep 17 00:00:00 2001 From: Keita Watanabe Date: Fri, 4 Jun 2021 09:48:36 +0900 Subject: [PATCH 08/10] fix CalendarroState constructor --- lib/calendarro.dart | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/calendarro.dart b/lib/calendarro.dart index 6c925a6..ee35192 100644 --- a/lib/calendarro.dart +++ b/lib/calendarro.dart @@ -126,15 +126,15 @@ class Calendarro extends StatefulWidget { } class CalendarroState extends State { - DateTime selectedSingleDate; + DateTime? selectedSingleDate; List selectedDates; - int pagesCount; - PageView pageView; + int pagesCount = -1; + PageView? pageView; CalendarroState({ this.selectedSingleDate, - this.selectedDates + required this.selectedDates }); @override @@ -165,7 +165,7 @@ class CalendarroState extends State { void setCurrentDate(DateTime date) { setState(() { int page = widget.getPageForDate(date); - pageView.controller.jumpToPage(page); + pageView?.controller.jumpToPage(page); }); } @@ -180,12 +180,13 @@ class CalendarroState extends State { widget.endDate) + 1; } + final selectedDate = selectedSingleDate; pageView = PageView.builder( itemBuilder: (context, position) => _buildCalendarPage(position), itemCount: pagesCount, controller: PageController( initialPage: - selectedSingleDate != null ? widget.getPageForDate(selectedSingleDate) : 0), + selectedDate != null ? widget.getPageForDate(selectedDate) : 0), onPageChanged: (page) { if (widget.onPageSelected != null) { DateRange pageDateRange = _calculatePageDateRange(page); @@ -213,7 +214,10 @@ class CalendarroState extends State { bool isDateSelected(DateTime date) { switch (widget.selectionMode) { case SelectionMode.SINGLE: - return DateUtils.isSameDay(selectedSingleDate, date); + final selectedDate = selectedSingleDate; + return selectedDate == null + ? false + : DateUtils.isSameDay(selectedDate, date); break; case SelectionMode.MULTI: final matchedSelectedDate = selectedDates.firstWhereOrNull( From b012c3ee63fe52895f155d80ae8ec8d0f85fbe09 Mon Sep 17 00:00:00 2001 From: Keita Watanabe Date: Fri, 4 Jun 2021 10:21:55 +0900 Subject: [PATCH 09/10] fix Calendarro constructor --- lib/calendarro.dart | 85 +++++++++++++++---------------- lib/default_day_tile_builder.dart | 2 +- 2 files changed, 43 insertions(+), 44 deletions(-) diff --git a/lib/calendarro.dart b/lib/calendarro.dart index ee35192..4fef639 100644 --- a/lib/calendarro.dart +++ b/lib/calendarro.dart @@ -8,7 +8,7 @@ import 'package:calendarro/default_day_tile_builder.dart'; import 'package:flutter/material.dart' hide DateUtils; abstract class DayTileBuilder { - Widget build(BuildContext context, DateTime date, DateTimeCallback onTap); + Widget build(BuildContext context, DateTime date, DateTimeCallback? onTap); } enum DisplayMode { MONTHS, WEEKS } @@ -24,57 +24,42 @@ class Calendarro extends StatefulWidget { SelectionMode selectionMode; DayTileBuilder dayTileBuilder; Widget weekdayLabelsRow; - DateTimeCallback onTap; - CurrentPageCallback onPageSelected; + DateTimeCallback? onTap; + CurrentPageCallback? onPageSelected; - DateTime selectedSingleDate; + DateTime? selectedSingleDate; List selectedDates; - int startDayOffset; - CalendarroState state; + int startDayOffset = 0; + CalendarroState? state; double dayTileHeight = 40.0; double dayLabelHeight = 20.0; Calendarro({ - Key key, - this.startDate, - this.endDate, + Key? key, + DateTime? startDate, + DateTime? endDate, this.displayMode = DisplayMode.WEEKS, - this.dayTileBuilder, + DayTileBuilder? dayTileBuilder, this.selectedSingleDate, - this.selectedDates, + List? selectedDates, this.selectionMode = SelectionMode.SINGLE, this.onTap, this.onPageSelected, - this.weekdayLabelsRow, - }) : super(key: key) { - if (startDate == null) { - startDate = DateUtils.getFirstDayOfCurrentMonth(); - } - startDate = DateUtils.toMidnight(startDate); - - if (endDate == null) { - endDate = DateUtils.getLastDayOfCurrentMonth(); - } - endDate = DateUtils.toMidnight(endDate); - - if (startDate.isAfter(endDate)) { + Widget? weekdayLabelsRow, + }) : + this.startDate = DateUtils.toMidnight(startDate ?? DateUtils.getFirstDayOfCurrentMonth()), + this.endDate = DateUtils.toMidnight(endDate ?? DateUtils.getLastDayOfCurrentMonth()), + this.dayTileBuilder = dayTileBuilder ?? DefaultDayTileBuilder(), + this.weekdayLabelsRow = weekdayLabelsRow ??CalendarroWeekdayLabelsView(), + this.selectedDates = selectedDates ?? [], + super(key: key) { + startDayOffset = this.startDate.weekday - DateTime.monday; + + if (this.startDate.isAfter(this.endDate)) { throw new ArgumentError("Calendarro: startDate is after the endDate"); } - startDayOffset = startDate.weekday - DateTime.monday; - - if (dayTileBuilder == null) { - dayTileBuilder = DefaultDayTileBuilder(); - } - - if (weekdayLabelsRow == null) { - weekdayLabelsRow = CalendarroWeekdayLabelsView(); - } - - if (selectedDates == null) { - selectedDates = List(); - } } static CalendarroState of(BuildContext context) => @@ -101,11 +86,15 @@ class Calendarro extends StatefulWidget { } int getPositionOfDate(DateTime date) { + final start = startDate; + if (start == null) { + throw StateError('startDate is null'); + } int daysDifference = date - .difference(DateUtils.toMidnight(startDate)) + .difference(DateUtils.toMidnight(start)) .inDays; - int weekendsDifference = ((daysDifference + startDate.weekday) / 7).toInt(); + int weekendsDifference = ((daysDifference + start.weekday) / 7).toInt(); var position = daysDifference - weekendsDifference * 2; return position; } @@ -129,7 +118,7 @@ class CalendarroState extends State { DateTime? selectedSingleDate; List selectedDates; - int pagesCount = -1; + int? pagesCount; PageView? pageView; CalendarroState({ @@ -295,18 +284,23 @@ class CalendarroState extends State { } DateRange _calculatePageDateRangeInMonthsMode(int pagePosition) { + final count = pagesCount; + if (count == null) { + throw StateError('pagesCount is null'); + } + DateTime pageStartDate; DateTime pageEndDate; if (pagePosition == 0) { pageStartDate = widget.startDate; - if (pagesCount <= 1) { + if (count <= 1) { pageEndDate = widget.endDate; } else { var lastDayOfMonth = DateUtils.getLastDayOfMonth(widget.startDate); pageEndDate = lastDayOfMonth; } - } else if (pagePosition == pagesCount - 1) { + } else if (pagePosition == count - 1) { pageStartDate = DateUtils.getFirstDayOfMonth(widget.endDate); pageEndDate = widget.endDate; } else { @@ -321,6 +315,11 @@ class CalendarroState extends State { } DateRange _calculatePageDateRangeInWeeksMode(int pagePosition) { + final count = pagesCount; + if (count == null) { + throw StateError('pagesCount is null'); + } + DateTime pageStartDate; DateTime pageEndDate; @@ -328,7 +327,7 @@ class CalendarroState extends State { pageStartDate = widget.startDate; pageEndDate = DateUtils.addDaysToDate(widget.startDate, 6 - widget.startDayOffset); - } else if (pagePosition == pagesCount - 1) { + } else if (pagePosition == count - 1) { pageStartDate = DateUtils.addDaysToDate( widget.startDate, 7 * pagePosition - widget.startDayOffset); pageEndDate = widget.endDate; diff --git a/lib/default_day_tile_builder.dart b/lib/default_day_tile_builder.dart index 4b89b24..d997918 100644 --- a/lib/default_day_tile_builder.dart +++ b/lib/default_day_tile_builder.dart @@ -7,7 +7,7 @@ class DefaultDayTileBuilder extends DayTileBuilder { DefaultDayTileBuilder(); @override - Widget build(BuildContext context, DateTime date, DateTimeCallback onTap) { + Widget build(BuildContext context, DateTime date, DateTimeCallback? onTap) { return CalendarroDayItem(date: date, calendarroState: Calendarro.of(context), onTap: onTap); } } \ No newline at end of file From 530b78c04ea7bd80070606c63035e0ef9b5e1b28 Mon Sep 17 00:00:00 2001 From: Keita Watanabe Date: Fri, 4 Jun 2021 10:31:39 +0900 Subject: [PATCH 10/10] fix errors --- lib/calendarro.dart | 14 +++++++------- lib/calendarro_page.dart | 7 +++++-- lib/default_day_tile_builder.dart | 7 ++++++- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/lib/calendarro.dart b/lib/calendarro.dart index 4fef639..f0522ff 100644 --- a/lib/calendarro.dart +++ b/lib/calendarro.dart @@ -62,27 +62,27 @@ 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); - return state; + return state!; } void setSelectedDate(DateTime date) { - state.setSelectedDate(date); + state?.setSelectedDate(date); } void toggleDate(DateTime date) { - state.toggleDateSelection(date); + state?.toggleDateSelection(date); } void setCurrentDate(DateTime date) { - state.setCurrentDate(date); + state?.setCurrentDate(date); } int getPositionOfDate(DateTime date) { @@ -179,7 +179,7 @@ class CalendarroState extends State { onPageChanged: (page) { if (widget.onPageSelected != null) { DateRange pageDateRange = _calculatePageDateRange(page); - widget.onPageSelected(pageDateRange.startDate, pageDateRange.endDate); + widget.onPageSelected?.call(pageDateRange.startDate, pageDateRange.endDate); } }, ); diff --git a/lib/calendarro_page.dart b/lib/calendarro_page.dart index 518285f..f40c9e8 100644 --- a/lib/calendarro_page.dart +++ b/lib/calendarro_page.dart @@ -69,12 +69,15 @@ class CalendarroPage extends StatelessWidget { List buildCalendarRow( BuildContext context, DateTime rowStartDate, DateTime rowEndDate) { - List items = []; + CalendarroState? calendarroState = Calendarro.of(context); + if (calendarroState == null) { + throw StateError('calendarroState is null'); + } + List items = []; DateTime currentDate = rowStartDate; 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); diff --git a/lib/default_day_tile_builder.dart b/lib/default_day_tile_builder.dart index d997918..616721b 100644 --- a/lib/default_day_tile_builder.dart +++ b/lib/default_day_tile_builder.dart @@ -8,6 +8,11 @@ class DefaultDayTileBuilder extends DayTileBuilder { @override Widget build(BuildContext context, DateTime date, DateTimeCallback? onTap) { - return CalendarroDayItem(date: date, calendarroState: Calendarro.of(context), onTap: onTap); + final state = Calendarro.of(context); + if (state == null) { + throw StateError('calendarroState is null'); + } + + return CalendarroDayItem(date: date, calendarroState: state, onTap: onTap); } } \ No newline at end of file