Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter how to display datepicker when textformfield is clicked

Tags:

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.

like image 493
Vishali Avatar asked Jan 10 '19 11:01

Vishali


People also ask

How do you show date in TextFormField Flutter?

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.

How do you show date picker in Flutter?

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.

How do you customize the date picker in Flutter?

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.


1 Answers

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.

like image 166
anmol.majhail Avatar answered Oct 21 '22 20:10

anmol.majhail