I have a section in my Flutter app where a user can click on a date and it will retirever from the database all entries from that date, The problem is if i go into the calendar and click a date say from two months ago, when i open the calendar again it will have today highlighted instead of the date i picked previously meaning i have to scroll all the way back to my desired date, How do I fix this
THe code:
child: TextFormField(
controller: _date,
decoration: const InputDecoration(
icon: Icon(Icons.calendar_today_rounded),
labelText: "Select Date"),
onTap: () async {
DateTime? pickeddate = await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(2000),
lastDate: DateTime(2101));
if (pickeddate != null) {
setState(() {
_date.text = DateFormat('yyyy-MM-dd').format(pickeddate);
currentDateSelected = _date.text;
// updateDate(DateFormat('yyyy-MM-dd').format(pickeddate));
_refreshSets();
//print(DateFormat('yyyy-MM-dd').format(pickeddate));
});
}
//Here i need to send off to the database that pickeddate
},
),
2
Answers
Read the
showDatePicker
constructor in the docThe
currentDate
represents the current day (i.e. today). This date will be highlighted in the day grid. If null, the date of DateTime.now() will be used. That causes your problem.You have a property by the name
initialDate
which is used to set the initial value of the date picker.Ensure that you are using
StatefulWidget
and declaring the selectedDate variable as shown below: