DatePicker in Flutter


In flutter, DatePicker is a widget to select a date. It allows you to select a date by year, month and day.

In this article, we will discuss about DatePicker in flutter. Here, we will discuss each and every steps by example.


Future<DateTime?> showDatePicker({
  required BuildContext context,
  required DateTime initialDate,
  required DateTime firstDate,
  required DateTime lastDate,
  DateTime? currentDate,
  DatePickerEntryMode initialEntryMode = DatePickerEntryMode.calendar,
  bool Function(DateTime)? selectableDayPredicate,
  String? helpText,
  String? cancelText,
  String? confirmText,
  Locale? locale,
  bool useRootNavigator = true,
  RouteSettings? routeSettings,
  TextDirection? textDirection,
  Widget Function(BuildContext, Widget?)? builder,
  DatePickerMode initialDatePickerMode =,
  String? errorFormatText,
  String? errorInvalidText,
  String? fieldHintText,
  String? fieldLabelText,

Code Implementation

import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:flutter/rendering.dart';

class DatePickerPage extends StatefulWidget {
  const DatePickerPage({Key? key}) : super(key: key);

  State<DatePickerPage> createState() => _DatePickerPageState();

class _DatePickerPageState extends State<DatePickerPage> {
  DateTime date =;
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Date Picker Example"),
        centerTitle: true,
        elevation: 0,
      body: Center(
        child: Column(
          children: [
              style: const TextStyle(
                fontSize: 26.0,
                fontWeight: FontWeight.bold,
            const SizedBox(
              height: 20.0,
                onPressed: () async {
                  DateTime? newdate = await showDatePicker(
                      context: context,
                      initialDate: date,
                      firstDate: DateTime(1900),
                      lastDate: DateTime(2100));
                  if (newdate == null) return;
                  setState(() {
                    date = newdate;
                child: const Padding(
                  padding: EdgeInsets.all(14.0),
                  child: Text(
                    "Select a Date",
                        TextStyle(fontSize: 22.0, fontWeight: FontWeight.w600),


After clicking on select a date button, a DatePicker context is appear as shown below.

When you wants to change the year, then select year option and change the year you want.

Select the day you want and click to ok button then date will change automatically to your page. But if you don’t select a ok button, date will not be change to your main page. It still remains the previous one.

After clicking a ok , selected date will come as shown below.

Here is the github link of this project:

Thank you for reading this article. If you have any queries regarding any topic, feel free to share with us. We will try to solve your problems.

Happy Learning.

Leave A Reply

Your email address will not be published.