こんにちは!
今回はshowDatePickerを使って、カレンダーから日時を選択する方法について、紹介したいと思います。
実装例
実装コード
Future<void> _selectDate(BuildContext context) async {
final DateTime? picked = await showDatePicker(
context: context,
initialDate: selectedDate,
firstDate: DateTime(2023),
lastDate: DateTime(2026),
barrierColor: Colors.green);
if (picked != null) {
setState(() {
selectedDate = picked;
});
}
}
Text(
'選択したい日付${selectedDate.year}/${selectedDate.month}/${selectedDate.day}'),
const SizedBox(height: 20),
SizedBox(
height: 60,
width: 150,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.green,
side: const BorderSide(color: Colors.white)),
onPressed: () => _selectDate(context),
child: const Text(
'日付選択',
style: TextStyle(color: Colors.white),
),
),
),
参考にさせていただいたサイト
終わり
今回の記事は以上になります。
今回も最後まで読んでいただきありがとうございました!
では、また次の記事で〜!