3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【flutter】iOS風のDatePickerを表示するには?

Last updated at Posted at 2023-09-12

こんにちは。
今回は、CupertinoDatePicker(iOS風)を実装する方法を紹介します。

方法

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

まず、「Cupertinoパッケージ」をインポートします。
「CupertinoDatePicker」のウィジェットを使い、iOS風のDatePickerを表示することが出来ます。

「CupertinoDatePicker」は、引数「onDateTimeChanged」に、値が変更された時の処理を記述することで使えます。

使用例

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';  //インポートする

void main() {
  runApp(const MainApp());
}

var selectDate = DateTime.now(); //今回は、現在時刻をセット

class MainApp extends StatefulWidget {
  const MainApp({super.key});

  @override
  State<MainApp> createState() => _MainAppState();
}

class _MainAppState extends State<MainApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            backgroundColor: Colors.pink,
            title: const Text("Flutter!!!!"),
          ),
          body: Center(
            child: CupertinoDatePicker(  //以下を追加
              initialDateTime: selectDate,
              mode: CupertinoDatePickerMode.date,
              onDateTimeChanged: (dateTime) {
                setState(() => selectDate = dateTime);
              },
            ),
          )),
    );
  }
}


実行例

CupertinoDatePicker.gif

3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?