본문 바로가기
프로그래밍/Flutter-Dart

[Flutter] DatePicker (iOS style)

by 채연2 2021. 3. 23.

pubspec.yaml

dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^1.0.2
  intl:
  flutter_localizations:
    sdk: flutter

 

 

 

Locale 설정

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Image Picker Demo',
      home: MyHomePage(),
      debugShowCheckedModeBanner: false,
      localizationsDelegates: [
        GlobalMaterialLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate
      ],
      supportedLocales: [
        Locale('ko', ''),
        Locale('en', ''),
      ],
    );
  }
}

 

 

 

utils/birthdate_picker.dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

class BirthDatePicker extends StatelessWidget {
  final void Function(DateTime) onDateTimeChanged;
  final String initDateStr;

  BirthDatePicker({
    required this.onDateTimeChanged,
    required this.initDateStr,
  });

  @override
  Widget build(BuildContext context) {
    final initDate = DateFormat('yyyy-MM-dd').parse(initDateStr ?? '2000-01-01');
    return SizedBox(
      height: 300,
      child: CupertinoDatePicker(
        minimumYear: 1900,
        initialDateTime: initDate,
        onDateTimeChanged: onDateTimeChanged,
        mode: CupertinoDatePickerMode.date,
      ),
    );
  }
}

 

 

 

DatePicker onDateTimeChanged 정의

DateTime _dateTime = DateTime.now();

void onDateTimeChanged(dateTime) {
   setState(() {
      _dateTime = dateTime;
   });
}

 

 

 

DatePicker

Column(
   children: <Widget>[
      BirthDatePicker(
         onDateTimeChanged: onDateTimeChanged,
         initDateStr: _dateTime.toString()
      ),
      RaisedButton(
         child: Text('Click!'),
         onPressed: () {
            setState(() {
               _dateStr = '${_dateTime.year}년  ${_dateTime.month}월  ${_dateTime.day}일';
            });
         },
      ),
      Container(
         padding: EdgeInsets.only(top: 20),
         child: Text(_dateStr),
      ),
   ],
)

 

 

 

결과

 

 

 

참고 : velog.io/@adbr/flutter-Cupertino-iOS-style-datepicker-%EC%BF%A0%ED%8D%BC%ED%8B%B0%EB%85%B8-%EB%94%94%EC%9E%90%EC%9D%B8-datepicker-%EC%84%A4%EC%A0%95

 

[flutter] Cupertino (iOS-style) datepicker 쿠퍼티노 디자인 datepicker 설정

flutter 별도의 라이브러리 붙이지않고 기본 cupertino style datepicker 만드는 코드

velog.io

 

320x100

'프로그래밍 > Flutter-Dart' 카테고리의 다른 글

[Flutter] Custom Horizontal Number Picker  (0) 2021.03.23
[Flutter] Horizontal Number Picker  (0) 2021.03.23
[Flutter] Image Picker  (2) 2021.03.23
[Flutter] Login App (1)  (0) 2021.03.23
[Flutter] wifi connect 정보  (0) 2021.03.22

댓글