Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select Birth date with DateFormat in flutter?

Tags:

flutter

dart

I have a field with birth Date format.

I tried some code but it's showing date as well as time but I want only Date.

is there any perfect way for it?

variables

String initValue="Select your Birth Date";
bool isDateSelected= false;
DateTime birthDate; // instance of DateTime
String birthDateInString;

here the code for dateTime picker.

GestureDetector(
   child: new Icon(Icons.calendar_today),
   onTap: ()async{
     final datePick= await showDatePicker(
        context: context,
        initialDate: new DateTime.now(),
        firstDate: new DateTime(1900),
        lastDate: new DateTime(2100)
       );
    if(datePick!=null && datePick!=birthDate){
      setState(() {
        birthDate=datePick; 
        isDateSelected=true;
        birthDateInString = "${birthDate.month}/${birthDate.day}/${birthDate.year}"; 
      });
     }
   }
 )

this widget is for showing Date

 new Text(isDateSelected ? "$birthDate":initValue),
like image 994
Shruti Ramnandan Sharma Avatar asked Jan 23 '26 20:01

Shruti Ramnandan Sharma


1 Answers

Define a variable.

String birthDateInString;
DateTime birthDate;

And then assign it value in your onTap

birthDateInString = "${birthDate.month}/${birthDate.day}/${birthDate.year}"; // 08/14/2019

Update:

GestureDetector(
    child: new Icon(Icons.calendar_today),
    onTap: ()async{
      final datePick= await showDatePicker(
          context: context,
          initialDate: new DateTime.now(),
          firstDate: new DateTime(1900),
          lastDate: new DateTime(2100)
      );
      if(datePick!=null && datePick!=birthDate){
        setState(() {
          birthDate=datePick;
          isDateSelected=true;
          
          // put it here
          birthDateInString = "${birthDate.month}/${birthDate.day}/${birthDate.year}"; // 08/14/2019
          
        });
      }
    }
)
like image 51
CopsOnRoad Avatar answered Jan 25 '26 12:01

CopsOnRoad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!