CalendarDatePicker2

repository·main·Indexed 41 days ago

https://github.com/theideasaler/calendar_date_picker2

A lightweight, highly customizable Flutter package for date selection. It supports single, multi, and range selection modes with views for day, month, year, and scroll. The library provides two primary widgets: CalendarDatePicker2 for real-time updates and CalendarDatePicker2WithActionButtons for confirmed selections via OK/Cancel buttons. It features Material 3 compatibility, multi-language support, and extensive UI customization through builder functions for days, months, and years.

Tokens
11.7K
Snippets
16
Records
25
Agent score
46%

What's inside CalendarDatePicker2

  1. Overview of CalendarDatePicker2

    main

    CalendarDatePicker2 is a lightweight, highly customizable calendar picker for Flutter. It extends the standard CalendarDatePicker and supports three selection modes: single, multi, and range. It also supports four different views: day, month, year, and scroll.

    Key features include:

    • Support for null initial dates.
    • Built-in showCalendarDatePicker2Dialog function.
    • Multi-language support.
    • Material 3 compatibility.
  2. Understand the difference between CalendarDatePicker2 and CalendarDatePicker2WithActionButtons

    main

    The library provides two primary widgets depending on how you want to handle user selection and confirmation:

    1. CalendarDatePicker2: This widget contains only the calendar UI. It emits an event immediately whenever the user taps a different date. Use this when you want real-time updates.

    2. CalendarDatePicker2WithActionButtons: This widget includes the calendar UI plus action buttons (typically CANCEL and OK). It only emits the updated value when the user explicitly taps the OK button. Use this when you want to allow users to confirm their selection before applying changes.

  3. Configure Single, Multi, and Range date pickers

    main

    You can change the selection mode by setting the calendarType property within the CalendarDatePicker2Config instance. The available types are:

    • CalendarDatePicker2Type.single: Default mode (single date selection).
    • CalendarDatePicker2Type.multi: Allows selecting multiple individual dates.
    • CalendarDatePicker2Type.range: Allows selecting a start and end date range.
    // Multi Date Picker
    CalendarDatePicker2(
      config: CalendarDatePicker2Config(
          calendarType: CalendarDatePicker2Type.multi,
      ),
      value: _dates,
      onValueChanged: (dates) => _dates = dates,
    );
    
    // Range Date Picker
    CalendarDatePicker2(
      config: CalendarDatePicker2Config(
          calendarType: CalendarDatePicker2Type.range,
      ),
      value: _dates,
      onValueChanged: (dates) => _dates = dates,
    );
  4. Enable multi-language support

    main

    CalendarDatePicker2 supports multiple languages via Flutter's localization system. To enable it, you must configure your wrapping MaterialApp with localizationsDelegates and a list of supportedLocales. This allows the picker to display month names, day names, and other UI elements in the specified languages (e.g., Chinese, Russian, Spanish, Hindi).

    MaterialApp(
      localizationsDelegates: GlobalMaterialLocalizations.delegates,
      supportedLocales: const [
        Locale('en', ''),
        Locale('zh', ''),
        Locale('ru', ''),
        Locale('es', ''),
        Locale('hi', ''),
      ],
      ...
    );
  5. Customize iOS Launch Screen Assets

    main

    To customize the iOS launch screen, you can replace the existing image files within the example/ios/Runner/Assets.xcassets/LaunchImage.imageset/ directory with your own assets.

    Alternatively, you can use Xcode for a more visual approach:

    1. Open the iOS workspace using open ios/Runner.xcworkspace.
    2. In the Xcode Project Navigator, navigate to Runner/Assets.xcassets.
    3. Drag and drop your desired images into the asset catalog.
    open ios/Runner.xcworkspace
  6. Basic setup of CalendarDatePicker2

    main

    To use the CalendarDatePicker2 widget, you must provide a config (of type CalendarDatePicker2Config) and a value (a List<DateTime?>). The onValueChanged callback is used to update your local state when dates are selected.

    Note that value must always be a List, even for single date selection.

    CalendarDatePicker2(
      config: CalendarDatePicker2Config(),
      value: _dates,
      onValueChanged: (dates) => _dates = dates,
    );
  7. Configure the CalendarDatePicker2Mode

    main

    The calendarViewMode determines the primary view of the picker. Use CalendarDatePicker2Mode to switch between different selection granularities.

    • day: Standard day grid view.
    • month: Month selection view.
    • year: Year selection view.
    • scroll: A vertically scrollable calendar view. This mode requires the calendar to be wrapped inside a height-constrained widget.
  8. Configure the CalendarDatePicker2Type

    main

    The calendarType determines the selection mode of the picker. Use CalendarDatePicker2Type to choose between single, multiple, or range selection.

    • single: Allows selecting a single date.
    • multi: Allows selecting multiple dates.
    • range: Allows selecting a range of two dates. Note that when using range, you can also enable rangeBidirectional in the config to allow selecting dates in reverse-chronological order.
    // Example of setting the type to range
    CalendarDatePicker2Config(
      calendarType: CalendarDatePicker2Type.range,
      rangeBidirectional: true,
    );
  9. Customize Range Selection Appearance

    main

    When using CalendarDatePicker2Type.range, you can customize how the selected range is highlighted.

    • selectedRangeHighlightColor: Sets the highlight color for days included in the selected range.
    • selectedRangeDayTextStyle: Sets the text style for days within the selected range.
    • selectedRangeHighlightBuilder: A function to provide full control over the range picker highlight widget.
    • selectedRangeDecorationPredicate: A function to determine the BoxDecoration for a day within a selected range. It provides dayToBuild, decoration, isStartDate, and isEndDate.