Flutter Calendar

 

Flutter Calendar

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.

Flutter provides a simple widget named table_calendar to show the calendar in our app. The table calendar is highly customizable and has many features, such as gesture, animation, and multiple formats.

The table_calendar provides many features, which are given below:

  • It is easy to use API.
  • It provides Custom Builders for UI control.
  • It has vertical auto-sizing.
  • It provides beautiful animations.
  • It provides gesture handling.
  • It provides multiple calendar formats such as a month, weak, year, etc.
  • We can also use multiple days of the week formats.

Let see step by step to create and display the calendar.

Step 1: Create a new Flutter project in the IDE you are using and give its title as Flutter Calendar Example.

Step 2: Open the project, navigate to the lib folder, and open the pubspec.yaml file. In this file, we need to add the table_calendar dependency as below:

  1. dependencies:  
  2.   flutter:  
  3.     sdk: flutter  
  4.   table_calendar: ^2.1.0  

Step 3: After adding the above dependency, we need to run the below command to get the required packages:

  1. $ flutter pub get  

Step 4: Next, import the dependency in the dart file as below:

  1. import 'package:syncfusion_flutter_calendar/calendar.dart';  

Step 5: After this, we need a calendar controller and initialize the calendar as CalendarController.

  1. CalendarController _controller;  
  2.   
  3. @override  
  4. void initState() {  
  5.   super.initState();  
  6.   _controller = CalendarController();  
  7. }  

Step 6: Next, we have to add the calendar widget as a child of any widget. Here, we are going to add the calendar widget as a child of the scaffold widget.

  1. @override  
  2. Widget build(BuildContext context) {  
  3.   return Scaffold(  
  4.     body: SingleChildScrollView(  
  5.       child: Column(  
  6.         children: <Widget>[  
  7.           TableCalendar()  
  8.         ],  
  9.       ),  
  10.     ),  
  11.   );  

Step 7: Now, we can write our logic and add styling to display the calendar.

Example

Below is the complete code to display the calendar in the app.

  1. import 'package:flutter/material.dart';  
  2. import 'package:table_calendar/table_calendar.dart';  
  3.   
  4. void main() => runApp(MyApp());  
  5.   
  6. class MyApp extends StatelessWidget {  
  7.   @override  
  8.   Widget build(BuildContext context) {  
  9.     return MaterialApp(  
  10.       theme: ThemeData(  
  11.         primarySwatch: Colors.green,  
  12.       ),  
  13.       home: HomeCalendarPage(),  
  14.     );  
  15.   }  
  16. }  
  17.   
  18. class HomeCalendarPage extends StatefulWidget {  
  19.   @override  
  20.   _HomeCalendarPageState createState() => _HomeCalendarPageState();  
  21. }  
  22.   
  23. class _HomeCalendarPageState extends State<HomeCalendarPage> {  
  24.   CalendarController _controller;  
  25.   
  26.   @override  
  27.   void initState() {  
  28.     super.initState();  
  29.     _controller = CalendarController();  
  30.   }  
  31.   
  32.   @override  
  33.   Widget build(BuildContext context) {  
  34.     return Scaffold(  
  35.       appBar: AppBar(  
  36.         title: Text('Flutter Calendar Example'),  
  37.       ),  
  38.       body: SingleChildScrollView(  
  39.         child: Column(  
  40.           crossAxisAlignment: CrossAxisAlignment.start,  
  41.           children: <Widget>[  
  42.             TableCalendar(  
  43.               initialCalendarFormat: CalendarFormat.month,  
  44.               calendarStyle: CalendarStyle(  
  45.                   todayColor: Colors.blue,  
  46.                   selectedColor: Theme.of(context).primaryColor,  
  47.                   todayStyle: TextStyle(  
  48.                       fontWeight: FontWeight.bold,  
  49.                       fontSize: 22.0,  
  50.                       color: Colors.white)  
  51.               ),  
  52.               headerStyle: HeaderStyle(  
  53.                 centerHeaderTitle: true,  
  54.                 formatButtonDecoration: BoxDecoration(  
  55.                   color: Colors.brown,  
  56.                   borderRadius: BorderRadius.circular(22.0),  
  57.                 ),  
  58.                 formatButtonTextStyle: TextStyle(color: Colors.white),  
  59.                 formatButtonShowsNext: false,  
  60.               ),  
  61.               startingDayOfWeek: StartingDayOfWeek.monday,  
  62.               onDaySelected: (date, events) {  
  63.                 print(date.toUtc());  
  64.               },  
  65.               builders: CalendarBuilders(  
  66.                 selectedDayBuilder: (context, date, events) => Container(  
  67.                     margin: const EdgeInsets.all(5.0),  
  68.                     alignment: Alignment.center,  
  69.                     decoration: BoxDecoration(  
  70.                         color: Theme.of(context).primaryColor,  
  71.                         borderRadius: BorderRadius.circular(8.0)),  
  72.                     child: Text(  
  73.                       date.day.toString(),  
  74.                       style: TextStyle(color: Colors.white),  
  75.                     )),  
  76.                 todayDayBuilder: (context, date, events) => Container(  
  77.                     margin: const EdgeInsets.all(5.0),  
  78.                     alignment: Alignment.center,  
  79.                     decoration: BoxDecoration(  
  80.                         color: Colors.blue,  
  81.                         borderRadius: BorderRadius.circular(8.0)),  
  82.                     child: Text(  
  83.                       date.day.toString(),  
  84.                       style: TextStyle(color: Colors.white),  
  85.                     )),  
  86.               ),  
  87.               calendarController: _controller,  
  88.             )  
  89.           ],  
  90.         ),  
  91.       ),  
  92.     );  
  93.   }  
  94. }  

Output:

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.

Flutter Calendar

If we select another date, we can see that the current date and selected date are in a different color. See the below image.

Flutter Calendar

We can also display the week of the month, as shown in this image.

Flutter Calendar


Post a Comment

Previous Post Next Post