From 1e31ac077136842346da203ad762447b203b87cb Mon Sep 17 00:00:00 2001 From: Adarsh Date: Thu, 8 Jul 2021 19:55:23 +0530 Subject: [PATCH] Added OnDateClicked to calendar. Triggers when date boxes clicked --- .gitignore | 1 + lib/heatmap_calendar.dart | 7 ++++++- lib/heatmap_day.dart | 31 ++++++++++++++++++------------- lib/week_columns.dart | 6 +++++- 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 7bf00e8..a201fa6 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ .packages .pub/ build/ +.idea/ # If you're building an application, you may want to check-in your pubspec.lock pubspec.lock diff --git a/lib/heatmap_calendar.dart b/lib/heatmap_calendar.dart index fe522c4..58fcf35 100644 --- a/lib/heatmap_calendar.dart +++ b/lib/heatmap_calendar.dart @@ -42,6 +42,9 @@ class HeatMapCalendar extends StatefulWidget { /// Helps avoiding overspacing issues final double safetyMargin; + // Callback for when a user clicks on date box + final void Function(DateTime) OnDateClicked; + const HeatMapCalendar( {Key key, @required this.input, @@ -52,7 +55,8 @@ class HeatMapCalendar extends StatefulWidget { this.textOpacity: 0.2, this.labelTextColor: Colors.black, this.dayTextColor: Colors.black, - this.safetyMargin: 0}) + this.safetyMargin: 0, + this.OnDateClicked}) : super(key: key); @override @@ -111,6 +115,7 @@ class HeatMapCalendarState extends State { dayTextColor: widget.dayTextColor, columnsToCreate: getColumnsToCreate(constraints.maxWidth) - 1, date: DateTime.now(), + OnDateClicked: (date) => widget.OnDateClicked(date), ) ], ), diff --git a/lib/heatmap_day.dart b/lib/heatmap_day.dart index 8313246..db32403 100644 --- a/lib/heatmap_day.dart +++ b/lib/heatmap_day.dart @@ -10,6 +10,7 @@ class HeatMapDay extends StatelessWidget { final Duration animationDuration; final Color textColor; final FontWeight fontWeight; + final VoidCallback OnDateClicked; const HeatMapDay( {Key key, @@ -21,7 +22,8 @@ class HeatMapDay extends StatelessWidget { this.opacity: 0.3, this.animationDuration: const Duration(milliseconds: 300), this.textColor: Colors.black, - this.fontWeight}) + this.fontWeight, + this.OnDateClicked}) : super(key: key); /// Loop for getting the right color based on [thresholds] values @@ -43,18 +45,21 @@ class HeatMapDay extends StatelessWidget { @override Widget build(BuildContext context) { - return Container( - alignment: Alignment.center, - height: size, - width: size, - color: getColorFromThreshold(), - margin: EdgeInsets.all(2.0), - child: AnimatedOpacity( - opacity: opacity, - duration: animationDuration, - child: Text( - currentDay.toString(), - style: TextStyle(fontWeight: fontWeight, color: textColor), + return InkWell( + onTap: () => OnDateClicked(), + child: Container( + alignment: Alignment.center, + height: size, + width: size, + color: getColorFromThreshold(), + margin: EdgeInsets.all(2.0), + child: AnimatedOpacity( + opacity: opacity, + duration: animationDuration, + child: Text( + currentDay.toString(), + style: TextStyle(fontWeight: fontWeight, color: textColor), + ), ), ), ); diff --git a/lib/week_columns.dart b/lib/week_columns.dart index 0ec7fb0..f4403bb 100644 --- a/lib/week_columns.dart +++ b/lib/week_columns.dart @@ -22,6 +22,8 @@ class WeekColumns extends StatelessWidget { final DateTime date; + final void Function(DateTime) OnDateClicked; + const WeekColumns( {Key key, @required this.squareSize, @@ -32,7 +34,8 @@ class WeekColumns extends StatelessWidget { @required this.monthLabels, @required this.dayTextColor, @required this.columnsToCreate, - @required this.date}) + @required this.date, + this.OnDateClicked}) : super(key: key); /// The main logic for generating a list of columns representing a week @@ -80,6 +83,7 @@ class WeekColumns extends StatelessWidget { currentDay: currentDate.day, opacity: currentOpacity, textColor: dayTextColor, + OnDateClicked: () => OnDateClicked(currentDate), ); columnItems.add(heatMapDay);