Elias Marrero
Elias Marrero

Reputation: 29

Flutter - Is there a way to make the Calendar move from year to year?

I'm trying to do a Birth Date picker, and I'm using the "showDatePicker" which is perfect, but I noticed that if I want to choose a date in 1998, I have to scroll a lot.

Is there a way to make this easier for the user? Maybe a way to choose the year?

Here's what I have:

GestureDetector(
  onTap: () {
    showDatePicker(
      context: context,
      initialDate: DateTime.now(),
      firstDate: DateTime(1950),
      lastDate: DateTime.now(),
      builder: (BuildContext context, Widget child) {
        return Theme(
          data: ThemeData.dark(),
          child: child,
        );
      },
    ).then((date) {
      setState(() {
        if (date != null)
        dateText = '${date.month}/${date.day}/${date.year}';
        });
      });
    },

Upvotes: 1

Views: 637

Answers (2)

royarg
royarg

Reputation: 581

You could wish to make the user choose the year upfront by providing this parameter in showDatePicker():

initialDatePickerMode: DatePickerMode.year,

Upvotes: 0

wcyankees424
wcyankees424

Reputation: 2664

I don't think flutters date / time picker has the functionality you want but there is a nice package that does have it. Heres the link to install the dependencies its really easy to use and it allows you to scroll through years. Once you have it this is how you implement it.

RaisedButton(
            child: Text('Pick Date'),
            onPressed: () => DatePicker.showDatePicker(
              context,
              showTitleActions: true,
              minTime: DateTime(1900, 3, 5),
              maxTime: DateTime.now(),
              onChanged: (date) {
                print('change $date');
              },
            ),
          ),

Upvotes: 1

Related Questions