new TextFormField( decoration: new InputDecoration(hintText: 'DOB'), maxLength: 10, validator: validateDob, onSaved: (String val) { strDob = val; }, ), Future _selectDate() async { DateTime picked = await showDatePicker( context: context, initialDate: new DateTime.now(), firstDate: new DateTime(2016), lastDate: new DateTime(2019) ); if(picked != null) setState(() => _value = picked.toString()); }
I created one textFormField when i click the field i want to display datepicker then i have to select one date from the picker after selecting the date i want to set the selected date in the textFormField.
To Implement Date Picker on TextField() and TextFormField(): First of all, you need to add intl Flutter package in your dependency to get formatted date output from date picker. Add the following line in your pubspec. yaml file to add intl package to your project.
DatePicker is a material widget in a flutter that lets the user select a date. Since there is no widget available for creating a date picker we will use showDatePicker() function. It will display a Material Design date picker in a dialog by calling flutter's inbuilt function.
Here's how you do it: Step 1: Inside the showDatePicker function, add the builder paramter. Step 2: Inside the builder parameter, return the Theme widget. Step 3: Inside the Theme widget, add the data property and define the new theme by specifying the colorScheme for the date picker dialog.
Update 2020:
As pointed by another answer @Lekr0 this can now be done using onTap()
property of TextFormField
.
TextFormField( onTap: (){ // Below line stops keyboard from appearing FocusScope.of(context).requestFocus(new FocusNode()); // Show Date Picker Here }, )
Original Answer:
Simple Way of Doing it :
Wrap your TextFormField
with IgnorePointer
& wrap IgnorePointer
with InkWell
InkWell( onTap: () { _selectDate(); // Call Function that has showDatePicker() }, child: IgnorePointer( child: new TextFormField( decoration: new InputDecoration(hintText: 'DOB'), maxLength: 10, // validator: validateDob, onSaved: (String val) {}, ), ), ),
Also in Your _selectDate()
make lastDate: new DateTime(2020));
else you will get error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With