calendar_view

repository·master·Indexed 19 days ago

https://github.com/simformsolutionspvtltd/flutter_calendar_view

A Flutter package for implementing calendar UIs and event management. It supports Month, Day, Week, and MultiDay views with full CRUD capabilities for events and reminders. Key features include a shared EventController for data synchronization across views, highly customizable UI components via builders, and comprehensive localization support for multiple languages including RTL layouts.

Tokens
24.7K
Snippets
53
Records
68
Agent score
68%

What's inside calendar_view

  1. Overview of Calendar View features

    master

    The calendar_view package provides a comprehensive suite for implementing calendar UIs and event management in Flutter.

    Key Capabilities:

    • Multiple View Modes: Supports Month View, Day View, and Week View.
    • Event Management: Full CRUD (add, remove, update) capabilities for standard events and full-day events.
    • Reminder Management: Add, remove, and update reminders.
    • Customization: Highly customizable UI components.
    • Advanced Logic: Ability to show working days in week/day views and synchronize event data across multiple different views.
  2. Synchronize events between different calendar views

    master

    To ensure that DayView, WeekView, MonthView, and MultiDayView stay in sync (e.g., when an event is added or changed), you must use a single shared EventController<T>. There are two ways to achieve this:

    Option 1: Direct Controller Injection

    Pass the same instance of EventController to the controller parameter of each view.

    Option 2: Using CalendarControllerProvider

    Wrap your application (or the relevant part of the widget tree) with CalendarControllerProvider. Any calendar view inside this provider that does not explicitly receive a controller will automatically read it from the provider.

    // Option 1: Direct injection
    final controller = EventController();
    
    MonthView(controller: controller);
    WeekView(controller: controller);
    DayView(controller: controller);
    MultiDayView(controller: controller);
    
    // Option 2: Using Provider
    CalendarControllerProvider(
      controller: EventController(),
      child: MaterialApp(
        home: const CalendarScreen(),
      ),
    );
  3. Customize the iOS launch screen assets

    master

    To change the launch screen image for the iOS version of the application, you can use one of two methods:

    1. Direct File Replacement: Replace the existing image files located in the example/ios/Runner/Assets.xcassets/LaunchImage.imageset/ directory with your own assets.
    2. Xcode Interface:
      • Open the iOS workspace using open ios/Runner.xcworkspace.
      • In the Xcode Project Navigator, navigate to Runner/Assets.xcassets.
      • Drag and drop your desired images into the asset catalog to replace the defaults.
    open ios/Runner.xcworkspace
  4. Initialize the Calendar Controller Provider

    master

    Before using any calendar views, you must wrap your MaterialApp with a CalendarControllerProvider. This provider manages the EventController, which is used to sync event data across different views (Month, Day, Week, etc.).

    CalendarControllerProvider(
        controller: EventController(),
        child: MaterialApp(
            // Your initialization for material app.
        ),
    )
  5. Migrate from 1.x.x to 2.x.x

    master

    When upgrading to version 2.x.x, note that breaking changes may occur. It is recommended to pin your version in pubspec.yaml instead of using the caret (^) symbol.

    1. Migrate HeaderStyle

    leftIconVisible and rightIconVisible have been removed. To hide an icon, set its corresponding leftIconConfig or rightIconConfig to null.

    2. Migrate Page Builders (CalendarPageHeader, DayPageHeader, etc.)

    Instead of passing individual properties like backgroundColor or iconColor to the builder, use the headerStyle property with HeaderStyle.withSameIcons().

    3. Migrate CellBuilder signature

    A new isSelected parameter has been added to the CellBuilder typedef.

    # Recommended dependency pinning for 2.x.x
    dependencies:
      calendar_view: 2.0.0
    // New CellBuilder signature
    typedef CellBuilder<T extends Object?> = Widget Function(
      DateTime date,
      List<CalendarEventData<T>> event,
      bool isToday,
      bool isInMonth,
      bool isSelected, // New parameter
      bool hideDaysNotInMonth,
    );
  6. Customize MonthView appearance

    master

    To customize the MonthView without a full theme override, use monthViewThemeSettings. For complete control over how individual date cells are rendered, use monthViewBuilders.cellBuilder.

    Key customization options:

    • monthViewThemeSettings: Adjust selectedHighlightColor, selectedTitleColor, cellsInMonthHighlightColor, and weekDayTextStyle.
    • monthViewBuilders.cellBuilder: A builder function that provides context for each cell, including date, events, isToday, isInMonth, isSelected, and hideDaysNotInMonth.
    MonthView(
      monthViewThemeSettings: MonthViewThemeSettings(
        selectedHighlightColor: Colors.blue,
        selectedTitleColor: Colors.white,
        cellsInMonthHighlightColor: Colors.blue,
        weekDayTextStyle: TextStyle(color: Colors.black, fontWeight: FontWeight.bold),
      ),
      monthViewBuilders: MonthViewBuilders(
        cellBuilder: (date, events, isToday, isInMonth, isSelected, hideDaysNotInMonth) {
          return Container(
            decoration: BoxDecoration(
              color: isInMonth ? Colors.white : Colors.grey[200],
              border: Border.all(color: Colors.blue),
            ),
            child: Center(
              child: Text(
                date.day.toString(),
                style: TextStyle(
                  color: isSelected ? Colors.white : (isToday ? Colors.red : Colors.black),
                  fontWeight: (isToday || isSelected) ? FontWeight.bold : FontWeight.normal,
                ),
              ),
            ),
          );
        },
      ),
    )
  7. Customize WeekView appearance

    master

    The WeekView offers several granular customization points:

    • Weekdays & Numbers: Use weekNumberBuilder and weekDayBuilder.
    • Weekday Tiles: Override weekDayTileColor (from WeekViewThemeData) using weekTitleBackgroundColor per-widget.
    • Background: Override pageBackgroundColor (from WeekViewThemeData) using backgroundColor per-widget.
    • Timeline: Use timelineTextColor in WeekViewThemeData. Use markingStyle in DefaultTimeLineMark for text style, or timeLineBuilder to replace the timeline entirely.
    • Time Lines: Customize hourIndicatorSettings, halfHourIndicatorSettings, and quarterHourIndicatorSettings per-widget.
    • Dividers: Use dividerSettings to customize the divider between weekdays and full-day events. Use DividerSettings.none() to hide it.
    // Example: Customizing WeekView indicators and dividers
    weekView( // Assuming WeekView usage
      hourIndicatorSettings: HourIndicatorSettings(
        color: Colors.greenAccent,
        lineStyle: LineStyle.dashed,
      ),
      showHalfHours: true,
      halfHourIndicatorSettings: HourIndicatorSettings(
        color: Colors.redAccent,
        lineStyle: LineStyle.dashed,
      ),
      dividerSettings: DividerSettings(
        thickness: 2,
        height: 2,
        color: Colors.blueAccent,
        indent: 10,
        endIndent: 10,
      ),
    );
    
    // To hide divider:
    // dividerSettings: DividerSettings.none(),
  8. Localization in calendar_view

    master

    The calendar_view package supports localization for several components:

    • AM/PM labels and the "more" text.
    • Weekday abbreviations.
    • Number mapping (optional).
    • RTL layout support via the isRTL property.
    • Runtime locale switching.

    To implement localization, you should provide custom string builders (like weekDayStringBuilder, headerStringBuilder, etc.) to the calendar views to return localized strings.

  9. Customize calendar themes

    master

    You can customize the appearance of MonthView, DayView, WeekView, and MultiDayView using two primary approaches:

    1. Using CalendarThemeProvider (Recommended): Wrap your application (or a specific subtree) with CalendarThemeProvider and provide a CalendarThemeData object containing specific theme data for each view type.

    2. Using ThemeData extensions: Since all theme data classes extend ThemeExtension, you can add them directly to your app's ThemeData.extensions list.

    // Approach 1: CalendarThemeProvider
    CalendarThemeProvider(
      calendarTheme: CalendarThemeData(
        monthViewTheme: MonthViewThemeData.light().copyWith(
          cellInMonthColor: Colors.blue.shade50,
          cellBorderColor: Colors.blue.shade300,
        ),
        dayViewTheme: DayViewThemeData.light(),
        weekViewTheme: WeekViewThemeData.light(),
        multiDayViewTheme: MultiDayViewThemeData.light(),
      ),
      child: YourApp(),
    )
    
    // Approach 2: ThemeData extensions
    final myMonthViewTheme = MonthViewThemeData.light().copyWith(
      cellInMonthColor: Colors.blue.shade50,
      cellBorderColor: Colors.blue.shade300,
    );
    
    final theme = ThemeData.light().copyWith(
      extensions: [
        myMonthViewTheme,
        DayViewThemeData.light(),
        WeekViewThemeData.light(),
        MultiDayViewThemeData.light(),
      ],
    );
  10. Use built-in languages in Calendar View

    master

    The package provides 8 pre-configured languages including English (en), Spanish (es), Arabic (ar), French (fr), German (de), Hindi (hi), Chinese (zh), and Japanese (ja). Arabic supports RTL and localized numerals, and Hindi supports Devanagari numerals. You can switch the active language globally using PackageStrings.setLocale(String locale).

    import 'package:calendar_view/calendar_view.dart';
    
    void main() {
      // Switch to Spanish
      PackageStrings.setLocale('es');
    
      // Switch to Arabic (includes RTL and Arabic numerals)
      PackageStrings.setLocale('ar');
    
      // Switch to Hindi (includes Devanagari numerals)
      PackageStrings.setLocale('hi');
    
      runApp(MyApp());
    }
  11. Customize DayView appearance

    master

    Customize the DayView using DayViewThemeData for global styles or per-widget settings for specific elements:

    • Timeline Text: Use timelineTextColor in DayViewThemeData. For specific text styles, use markingStyle in DefaultTimeLineMark.
    • Live Indicator: Use liveIndicatorColor in DayViewThemeData. Customize per-widget using liveTimeIndicatorSettings.
    • Time Lines (Hour/Half-Hour/Quarter-Hour): Use hourLineColor, halfHourLineColor, or quarterHourLineColor in DayViewThemeData. Customize per-widget using hourIndicatorSettings, halfHourIndicatorSettings, or quarterHourIndicatorSettings.
    // Example: Customizing hour indicator settings
    HourIndicatorSettings(
      height: widget.heightPerMinute,
      color: Theme.of(context).colorScheme.surfaceContainerHighest,
      offset: 5,
    );