Easy Ways to Build a Heat Map Calendar Using the Flutter Event Calendar
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (175).NET Core  (29).NET MAUI  (209)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (220)BoldSign  (15)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (67)Flutter  (133)JavaScript  (222)Microsoft  (119)PDF  (81)Python  (1)React  (101)Streamlit  (1)Succinctly series  (131)Syncfusion  (922)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (69)WPF  (159)Xamarin  (161)XlsIO  (37)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (151)Chart  (133)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (64)Development  (634)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (42)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (509)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (11)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (388)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (598)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Easy Ways to Build a Heat Map Calendar Using the Flutter Event Calendar

Easy Ways to Build a Heat Map Calendar Using the Flutter Event Calendar

In this blog post, we are going to discuss how to design a heat map calendar and apply color for the different month cells based on a user’s mobile usage using the Flutter event Calendar widget.

A heat map is a data visualization technique that uses colors the way a bar graph uses height and width. To create a heat map calendar, we require data for a month’s dates. In our example, we are going to display the digital well-being data of a smartphone.

These days, smartphones have become essential, and our daily usage of smartphones is increasingly high. We are using mobile phones for 8 to 9 hours a day. So, to track a user’s mobile phone usage, we can use digital well-being data. The Syncfusion Flutter event Calendar, with its rich feature set, allows users to design a heatmap calendar using custom builder support.

Heat Map Using Flutter Event Calendar
Heat Map Using Flutter Event Calendar

Note: If you are new to the Calendar widget, please go through the Getting started with Flutter event Calendar documentation before proceeding.

Handling data

First, get the required data (mobile usage in hours) and color to render. Then, render the background color to the corresponding UI. For demo purposes, we are providing some random data.

Refer to the following code example.

final List<int> _usageHours = <int>[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,  22, 23];
  final List<HeatMapData> _heatMapDataCollection = <HeatMapData>[];
  final Random random = Random();

  @override
  void initState() {
    _addHeatMapData();
    super.initState();
  }

  void _addHeatMapData() {
    for (int i = 0; i < 100; i++) {
      final int hour = random.nextInt(_usageHours.length);
      final Color color = _getMonthCellBackgroundColor(hour);
      _heatMapDataCollection.add(HeatMapData(hour, color));
    }
  }

  Color _getMonthCellBackgroundColor(int hour) {
    if(hour < 6){
      return _kLightGrey;
    }else if(hour < 11){
      return _kLightGreen;
    }else if(hour < 16){
      return _kMidGreen;
    }else if(hour < 21){
      return _kDarkGreen;
    }

    return _kDarkerGreen;
  }

Designing the heat map calendar

In the Flutter event calendar, you can customize the presentation of data and its interaction using the builder support. Customize the calendar month cell to apply the background color related to the corresponding data in each cell using the monthCellBuilder property. Then, assign the data values to the corresponding UI.

Now, return the designed custom UI with the data in the monthCellBuilder callback.

Refer to the following code example.

/// Returns the calendar widget based on the properties passed.
  SfCalendar _getHeatMapCalendar(bool isMobilePlatform) {
    return SfCalendar(
        showNavigationArrow: !isMobilePlatform,
        view: CalendarView.month,
        monthCellBuilder: _monthCellBuilder,
        showDatePickerButton: true,
        maxDate: _maxDate,
        initialDisplayDate: DateTime.now().subtract(Duration(days: 100)),
        monthViewSettings: const MonthViewSettings(
          showTrailingAndLeadingDates: false,
        ));
  }

  /// Returns the cell  text color based on the cell background color.
  Color _getCellTextColor(Color backgroundColor) {
    if (backgroundColor == _kDarkGreen || backgroundColor == _kDarkerGreen) {
      return Colors.white;
    }

    return Colors.black;
  }

  /// Returns the builder for month cell.
  Widget _monthCellBuilder(
      BuildContext buildContext, MonthCellDetails details) {
     Color backgroundColor = _heatMapDataCollection[random.nextInt(100)].color;
     if(details.date.isAfter(_maxDate)){
       backgroundColor = Colors.transparent;
     }else if(isSameDate(details.date, _maxDate)){
       backgroundColor = _getMonthCellBackgroundColor(random.nextInt(_maxDate.hour));
     }
    final Color defaultColor = Colors.white;
    return Container(
      decoration: BoxDecoration(
          color: backgroundColor,
          border: Border.all(color: defaultColor, width: 0.5)),
      child: Center(
        child: Text(
          details.date.day.toString(),
          style: TextStyle(color: _getCellTextColor(backgroundColor)),
        ),
      ),
    );
  }

Now, the Calendar widget is completely configured. Run the sample to see the event calendar with heat map data.

Heat Map Calendar Using Flutter Event Calendar
Heat Map Calendar Using Flutter Event Calendar

Resource

For more information, refer to designing a heat map using the Flutter event Calendar demo.

Conclusion

Thanks for reading! In this blog post, we’ve seen how to design a heatmap calendar using the Flutter event Calendar. This widget is available in AndroidiOSwebWindowsMac, and Linux platforms. With this heat map data visualization, you can easily understand the trends in data with different color patterns!

Also, you can check out our other Flutter project demos in this GitHub repository. Feel free to try out these demos and share your feedback or questions in the comments section.

Our Calendar component is also available in our Blazor, ASP.NET (CoreMVC), JavaScriptAngularReactVueXamarinUWPWinFormsWPF, and WinUI platforms. Use them to build astonishing applications!

For existing customers, the new version is available for download from the License and Downloads page. If you are not yet a Syncfusion customer, you can try our 30-day free trial to check out our available features.

You can contact us through our support forumDirect-Trac, or feedback portal. We are always happy to assist you!

Related blogs

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed