LoginSignup
0
0

【flutter】showTimePickerを使って選択した時間を表示したい

Last updated at Posted at 2024-05-17

こんにちは!
今回はshowTimePickerを使って、選択した時間を表示する方法について紹介します。
最後まで読んでいただけたら嬉しいです!

1,実装

showtimepicker.gif

2,コード

  TimeOfDay selectedDayOfTime = TimeOfDay.now();

  Future<void> _selectTime(BuildContext context) async {
    final TimeOfDay? picked = await showTimePicker(
      context: context,
      initialTime: selectedDayOfTime,
      builder: (context, child) {
        return Theme(
          data: Theme.of(context).copyWith(
            timePickerTheme: TimePickerThemeData(
              backgroundColor: Colors.white,
              dialBackgroundColor: Colors.lightGreen,
              hourMinuteColor: const Color(0xff0A000000),
              hourMinuteTextColor: Colors.green,
              dialHandColor: Colors.green,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(8.0),
              ),
            ),
            textButtonTheme: TextButtonThemeData(
                style: TextButton.styleFrom(foregroundColor: Colors.green)),
            colorScheme: const ColorScheme.light(
              primary: Colors.lightGreen,
            ),
          ),
          child: child!,
        );
      },
    );

    if (picked != null) {
      setState(() {
        selectedDayOfTime = picked;
      });
    }
  }
}

    Text(
        '選択した時刻: ${selectedDayOfTime.hour.toString().padLeft(2, "0")}:${selectedDayOfTime.minute.toString().padLeft(2, "0")}',
            style:const TextStyle(fontSize: 20, fontWeight: FontWeight.w600),
              ),
              const SizedBox(
                height: 20,
              ),
      SizedBox(
        height: 60,
        width: 150,
        child: ElevatedButton(
          style: ElevatedButton.styleFrom(
              backgroundColor: Colors.grey,
              side: const BorderSide(color: Colors.white)),
          onPressed: () => _selectTime(context),
          child: const Text(
            '時間選択',
            style: TextStyle(color: Colors.white),
          ),
        ),
      ),

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

終わりに

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

0
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
0
0