DEV Community

Parth
Parth

Posted on

Creating a Date Picker #Flutter

this tutorial focuses on adding a Date Picker to a Flutter app.

so let's do this...

we will first create a simple button on which we can press and get our date picker pop.

FlatButton(
    child: Text("Date: " + selectedDate.toString()),
    onPressed: () async{
        DateTime date = await showDatePicker(
        context: context, 
        initialDate: DateTime.now(), 
        firstDate: DateTime(2010), 
        lastDate: DateTime.now().add(Duration(days: 1)));

        if (date != null){
            setState((){
                selectedDate = date;
            });
        }
    },
)
Enter fullscreen mode Exit fullscreen mode

here, the initialDate represents the current selected date, firstDate represents the date from where your calendar will start and the lastDate represents the ending date of your calendar.

you can even use it's "then" function to get the value selected from that picker.

so that's it go and try.

Top comments (0)