LoginSignup
1
0

【flutter】showDatePickerを使ってカレンダーから日時を選択したい。

Posted at

こんにちは!
今回はshowDatePickerを使って、カレンダーから日時を選択する方法について、紹介したいと思います。

実装例

タイトルなし.gif

実装コード

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),
      ),
    ),
  ),

参考にさせていただいたサイト

終わり

今回の記事は以上になります。
今回も最後まで読んでいただきありがとうございました!
では、また次の記事で〜!

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0