DatePicker Widget
Project Preview
- Interested in seeing the live demo? Click here to explore.
- Want to view the source code? Visit the project on GitHub.
Code Snippet
import 'package:flutter/material.dart';
class DatePickerWidget extends StatefulWidget {
const DatePickerWidget({super.key});
@override
State<DatePickerWidget> createState() => _DatePickerWidgetState();
}
class _DatePickerWidgetState extends State<DatePickerWidget> {
DateTime selectedDate = DateTime.now();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("DatePicker Widget"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'${selectedDate.year} - ${selectedDate.month} - ${selectedDate.day}',
),
ElevatedButton(
onPressed: () async {
final DateTime? dateTime = await showDatePicker(
context: context,
initialDate: selectedDate,
firstDate: DateTime(2000),
lastDate: DateTime(3000),
);
if (dateTime != null) {
setState(() {
selectedDate = dateTime;
});
}
},
child: const Text('Choose date'),
),
],
),
),
);
}
}