DatePickerUtils

A Flutter utility that provides simplified date picking functionality for mobile applications. This plugin offers customizable date pickers that work across platforms with localization support.

Features

  • Simple date picker with customizable UI
  • Support for multiple languages (English, Russian, Uzbek, and more)
  • Passport date validation options
  • Intuitive API for Flutter applications

Platform Support

  • Android
  • iOS

Installation

Add the dependency to your pubspec.yaml file:

dependencies:
  custom_date_picker_plus: ^1.0.1

Then run:

flutter pub get

Usage

Basic Implementation

class DatePickerUtils {
  simpleDatePicker({
    required BuildContext context,
    required String title,
    bool isPassportGivenDate = false,
  }) async {
    DateTime? datePicker = await DatePicker.showSimpleDatePicker(
      context,
      initialDate:
      DateTime(isPassportGivenDate ? (DateTime.now().year - 10) : 1990),
      backgroundColor: Theme.of(context).cardColor,
      firstDate:
      DateTime(isPassportGivenDate ? (DateTime.now().year - 20) : 1930),
      lastDate: DateTime.now(),
      dateFormat: "dd-MMMM-yyyy",
      locale: getDatePickerLanguage(getLanguage(context)),
      looping: true,
      itemTextStyle: Theme.of(context).textTheme.titleMedium,
      confirmText: tr('save'),
      cancelText: tr('cancel'),
      textColor: Theme.of(context).textTheme.titleMedium!.color,
      titleText: title,
      reverse: true,
    );
    if (datePicker != null) {
      return DateFormat('dd-MM-yyyy').format(datePicker);
    }
  }
}

Language Support

The date picker automatically adapts to the user's language settings. Supported languages:

  • English (en)
  • Russian (ru)
  • Uzbek (uz)
  • Old Uzbek (oz)
  • Tajik (tj)
// Get the appropriate DateTimePickerLocale based on language code
DateTimePickerLocale locale = getDatePickerLanguage(getLanguage(context));

API Reference

DatePickerUtils

simpleDatePicker

Shows a customizable date picker and returns the selected date in 'dd-MM-yyyy' format.

Parameters:

  • context (required): BuildContext for displaying the picker
  • title (required): String title for the date picker

Returns:

  • A Future<String?> with the selected date in 'dd-MM-yyyy' format, or null if cancelled

Helper Functions

getDatePickerLanguage

Converts a language code to the appropriate DateTimePickerLocale.

Parameters:

  • text: String language code ('en', 'ru', 'uz', 'oz', 'tj')

Returns:

  • The corresponding DateTimePickerLocale value

Example

import 'package:flutter/material.dart';
import 'package:date_picker_utils/date_picker_utils.dart';

class DatePickerExample extends StatelessWidget {
  final datePickerUtils = DatePickerUtils();
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Date Picker Example')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              onPressed: () async {
                final date = await datePickerUtils.simpleDatePicker(
                  context: context,
                  title: 'Select Date',
                );
                if (date != null) {
                  ScaffoldMessenger.of(context).showSnackBar(
                    SnackBar(content: Text('Selected: $date')),
                  );
                }
              },
              child: Text('Show Date Picker'),
            ),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: () async {
                final date = await datePickerUtils.simpleDatePicker(
                  context: context,
                  title: 'Birth Date',
                );
                if (date != null) {
                  ScaffoldMessenger.of(context).showSnackBar(
                    SnackBar(content: Text('Birth date: $date')),
                  );
                }
              },
              child: Text('Show Birthdate Date Picker'),
            ),
          ],
        ),
      ),
    );
  }
}

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements:

  • Open an issue on GitHub
  • Fork the repository
  • Create a pull request with your changes

License

This project is licensed under the MIT License - see the LICENSE file for details.