7
0

【Flutter】カレンダーの日付上限を設定する(CupertinoDatePicker)

Last updated at Posted at 2024-07-01

こんにちは。
今回はCupertinoDatePickerの日付の上限を設定する方法を紹介します。

方法

スクリーンショット 2023-08-13 20.18.51.png

CupertinoDatePickerの日付の上限を設定するには、次の2つの引数を使います。
2つの引数に、上限にしたい日付を指定します。

  • minimumDate : 日付の過去の上限
  • maximumDate : 日付の未来の上限

使用例


class _MyHomePageState extends State<MyHomePage> {
  var _selectDate = DateTime.now();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xffFFFFFF),
      appBar: AppBar(
        title: const Text("CupertinoDatePicker"),
      ),
      body: Center(
        child: CupertinoDatePicker(
          mode: CupertinoDatePickerMode.date,
          initialDateTime: _selectDate,
          minimumDate: DateTime(DateTime.now().year - 1), //1年前まで
          maximumDate: DateTime(DateTime.now().year + 1), //1年後まで
          onDateTimeChanged: (newDate) {
            setState(() {
              _selectDate = newDate;
            });
          },
        ),
      ),
    );
  }
}

実行例

calender_limit.gif

最後に

ここまで読んでいただき、ありがとうございました!
いいねしてくれたら、スキップして喜びます:heartpulse:

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