Flutter Date Picker and Flutter Time Picker on TextField Tap
i mobile app developer in this flutter tutorial I am sharing how to use the flutter date picker. Are you creating an application form for mobile applications with data input fields for dates? Look at the example below to discover how to add an option to select dates on TextField() along with a TextFormFiled() widget in order that every time the user taps the field, a date picker dialogue will pop
Let’s Start to Make a Flutter Date Picker Example.
Firstly create a flutter project and build is successful. if you have an old project you can follow the below steps.
Add Depandacy in your pubspec.yaml file
dependencies:
flutter:
sdk: flutter
intl: ^0.18.0
after adding dependencies update your pubspec file to get the dependencies function. after that, you can open your main. dart file and add the below code.
Flutter Date Picker and Flutter Time Picker on TextField Tap
Hi mobile app developer in this flutter tutorial I am sharing how to use the flutter date picker. Are you creating an application form for mobile applications with data input fields for dates? Look at the example below to discover how to add an option to select dates on TextField() along with a TextFormFiled() widget in order that every time the user taps the field, a date picker dialogue will pop up.
To Implement Date Picker on TextField()
In the beginning, you must include the intl Flutter in your dependency list to receive formatted date output from the date picker. Include the following line in the pubspec.yaml file to add the intl packages to the project you are working on.
Let’s Start to Make a Flutter Date Picker Example.
Firstly create a flutter project and build is successful. if you have an old project you can follow the below steps.
Add Depandacy in your pubspec.yaml file
dependencies:
flutter:
sdk: flutter
intl: ^0.18.0
after adding dependencies update your pubspec file to get the dependencies function. after that, you can open your main. dart file and add the below code.
For Date Picker, you can add this method. and set the start date year and last date year and the DateFormate for getting the string for a date. Also, you can set the team color of the date picker using with Theme.
DateTime currentDate = DateTime.now();
Future<void> _selectDate(BuildContext context) async {
final DateTime? pickedDate = await showDatePicker(
context: context,
initialDate: currentDate,
firstDate: DateTime(2015),
lastDate: DateTime(2050),
builder: (context, child) {
return Theme(
data: Theme.of(context).copyWith(
colorScheme: ColorScheme.light(
primary: COLOR_THEME, // <-- SEE HERE
onPrimary: COLOR_GRAY, // <-- SEE HERE
onSurface: Colors.blue, // <-- SEE HERE
),
textButtonTheme: TextButtonThemeData(
style: TextButton.styleFrom(
primary: Colors.black, // button text color
),
),
),
child: child!,
);
},
);
if (pickedDate != null && pickedDate != currentDate)
setState(() {
currentDate = pickedDate;
formattedDate = DateFormat('yyyy-MM-dd').format(currentDate);
});
}
Full example source code click here