{"id":2128,"date":"2022-04-09T18:20:08","date_gmt":"2022-04-09T18:20:08","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=2128"},"modified":"2022-04-09T18:20:08","modified_gmt":"2022-04-09T18:20:08","slug":"flutter-calendar","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/04\/09\/flutter-calendar\/","title":{"rendered":"Flutter Calendar"},"content":{"rendered":"\n<p>A calendar is a system used to organize the days, weeks, or months for commercial, religious, social, or administrative purposes. It keeps a record about which events fall on a particular date and when the special events will happen. In this section, we are going to explain how to display and use the calendar widget in our Flutter application.<\/p>\n\n\n\n<p>Flutter\u00a0provides a simple widget named\u00a0<strong>table_calendar<\/strong>\u00a0to show the calendar in our app. The table calendar is highly customizable and has many features, such as gesture, animation, and multiple formats.<\/p>\n\n\n\n<p>The table_calendar provides many features, which are given below:<\/p>\n\n\n\n<ul><li>It is easy to use API.<\/li><li>It provides Custom Builders for UI control.<\/li><li>It has vertical auto-sizing.<\/li><li>It provides beautiful animations.<\/li><li>It provides gesture handling.<\/li><li>It provides multiple calendar formats such as a month, weak, year, etc.<\/li><li>We can also use multiple days of the week formats.<\/li><\/ul>\n\n\n\n<p>Let see step by step to create and display the calendar.<\/p>\n\n\n\n<p><strong>Step 1:<\/strong>\u00a0Create a new Flutter project in the\u00a0IDE\u00a0you are using and give its title as Flutter Calendar Example.<\/p>\n\n\n\n<p><strong>Step 2:<\/strong>&nbsp;Open the project, navigate to the lib folder, and open the&nbsp;<strong>pubspec.yaml<\/strong>&nbsp;file. In this file, we need to add the table_calendar dependency as below:<a href=\"https:\/\/www.javatpoint.com\/flutter-calendar#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/flutter-calendar#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/flutter-calendar#\"><\/a><\/p>\n\n\n\n<ol><li>dependencies:&nbsp;&nbsp;<\/li><li class=\"\">&nbsp;&nbsp;flutter:&nbsp;&nbsp;<\/li><li>&nbsp;&nbsp;&nbsp;&nbsp;sdk:&nbsp;flutter&nbsp;&nbsp;<\/li><li class=\"\">&nbsp;&nbsp;table_calendar:&nbsp;^2.1.0&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Step 3:<\/strong>&nbsp;After adding the above dependency, we need to run the below command to get the required packages:<a href=\"https:\/\/www.javatpoint.com\/flutter-calendar#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/flutter-calendar#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/flutter-calendar#\"><\/a><\/p>\n\n\n\n<ol><li>$&nbsp;flutter&nbsp;pub&nbsp;get&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Step 4:<\/strong>&nbsp;Next, import the dependency in the dart file as below:<a href=\"https:\/\/www.javatpoint.com\/flutter-calendar#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/flutter-calendar#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/flutter-calendar#\"><\/a><\/p>\n\n\n\n<ol><li><strong>import<\/strong>&nbsp;&#8216;package:syncfusion_flutter_calendar\/calendar.dart&#8217;;&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Step 5:<\/strong>\u00a0After this, we need a calendar controller and initialize the calendar as\u00a0<strong>CalendarController<\/strong>.<\/p>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/flutter-calendar#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/flutter-calendar#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/flutter-calendar#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CalendarController _controller;  \n  \n@override  \nvoid initState() {  \n  super.initState();  \n  _controller = CalendarController();  \n} <\/code><\/pre>\n\n\n\n<p><strong>Step 6:<\/strong>\u00a0Next, we have to add the\u00a0<strong>calendar widget<\/strong>\u00a0as a child of any\u00a0widget. Here, we are going to add the calendar widget as a child of the scaffold widget.<a href=\"https:\/\/www.javatpoint.com\/flutter-calendar#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/flutter-calendar#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/flutter-calendar#\"><\/a><\/p>\n\n\n\n<p><span style=\"font-family: var(--list--font-family); background-color: var(--global--color-background); color: var(--global--color-primary); font-size: var(--global--font-size-base);\">\u00a0<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@override  \nWidget build(BuildContext context) {  \n  return Scaffold(  \n    body: SingleChildScrollView(  \n      child: Column(  \n        children: &lt;Widget>&#91;  \n          TableCalendar()  \n        ],  \n      ),  \n    ),  \n  ); <\/code><\/pre>\n\n\n\n<p><strong>Step 7:<\/strong>&nbsp;Now, we can write our logic and add styling to display the calendar.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Below is the complete code to display the calendar in the app.<a href=\"https:\/\/www.javatpoint.com\/flutter-calendar#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/flutter-calendar#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/flutter-calendar#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import 'package:flutter\/material.dart';  \nimport 'package:table_calendar\/table_calendar.dart';  \n  \nvoid main() => runApp(MyApp());  \n  \nclass MyApp extends StatelessWidget {  \n  @override  \n  Widget build(BuildContext context) {  \n    return MaterialApp(  \n      theme: ThemeData(  \n        primarySwatch: Colors.green,  \n      ),  \n      home: HomeCalendarPage(),  \n    );  \n  }  \n}  \n  \nclass HomeCalendarPage extends StatefulWidget {  \n  @override  \n  _HomeCalendarPageState createState() => _HomeCalendarPageState();  \n}  \n  \nclass _HomeCalendarPageState extends State&lt;HomeCalendarPage> {  \n  CalendarController _controller;  \n  \n  @override  \n  void initState() {  \n    super.initState();  \n    _controller = CalendarController();  \n  }  \n  \n  @override  \n  Widget build(BuildContext context) {  \n    return Scaffold(  \n      appBar: AppBar(  \n        title: Text('Flutter Calendar Example'),  \n      ),  \n      body: SingleChildScrollView(  \n        child: Column(  \n          crossAxisAlignment: CrossAxisAlignment.start,  \n          children: &lt;Widget>&#91;  \n            TableCalendar(  \n              initialCalendarFormat: CalendarFormat.month,  \n              calendarStyle: CalendarStyle(  \n                  todayColor: Colors.blue,  \n                  selectedColor: Theme.of(context).primaryColor,  \n                  todayStyle: TextStyle(  \n                      fontWeight: FontWeight.bold,  \n                      fontSize: 22.0,  \n                      color: Colors.white)  \n              ),  \n              headerStyle: HeaderStyle(  \n                centerHeaderTitle: true,  \n                formatButtonDecoration: BoxDecoration(  \n                  color: Colors.brown,  \n                  borderRadius: BorderRadius.circular(22.0),  \n                ),  \n                formatButtonTextStyle: TextStyle(color: Colors.white),  \n                formatButtonShowsNext: false,  \n              ),  \n              startingDayOfWeek: StartingDayOfWeek.monday,  \n              onDaySelected: (date, events) {  \n                print(date.toUtc());  \n              },  \n              builders: CalendarBuilders(  \n                selectedDayBuilder: (context, date, events) => Container(  \n                    margin: const EdgeInsets.all(5.0),  \n                    alignment: Alignment.center,  \n                    decoration: BoxDecoration(  \n                        color: Theme.of(context).primaryColor,  \n                        borderRadius: BorderRadius.circular(8.0)),  \n                    child: Text(  \n                      date.day.toString(),  \n                      style: TextStyle(color: Colors.white),  \n                    )),  \n                todayDayBuilder: (context, date, events) => Container(  \n                    margin: const EdgeInsets.all(5.0),  \n                    alignment: Alignment.center,  \n                    decoration: BoxDecoration(  \n                        color: Colors.blue,  \n                        borderRadius: BorderRadius.circular(8.0)),  \n                    child: Text(  \n                      date.day.toString(),  \n                      style: TextStyle(color: Colors.white),  \n                    )),  \n              ),  \n              calendarController: _controller,  \n            )  \n          ],  \n        ),  \n      ),  \n    );  \n  }  \n}<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p>When we run the app in the device or emulator, we should see the UI similar to the below screenshot. Here, we can see the previous and next arrow icon to display the month. The week starts from Monday, and date 14 is my current date.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static.javatpoint.com\/tutorial\/flutter\/images\/flutter-calendar.png\" alt=\"Flutter Calendar\"\/><\/figure>\n\n\n\n<p>If we select another date, we can see that the current date and selected date are in a different color. See the below image.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static.javatpoint.com\/tutorial\/flutter\/images\/flutter-calendar2.png\" alt=\"Flutter Calendar\"\/><\/figure>\n\n\n\n<p>We can also display the week of the month, as shown in this image.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static.javatpoint.com\/tutorial\/flutter\/images\/flutter-calendar3.png\" alt=\"Flutter Calendar\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>A calendar is a system used to organize the days, weeks, or months for commercial, religious, social, or administrative purposes. It keeps a record about which events fall on a particular date and when the special events will happen. In this section, we are going to explain how to display and use the calendar widget [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[585],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/2128"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=2128"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/2128\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=2128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=2128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=2128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}