基本属性

  1. CupertinoDatePicker({ // iOS风格的日期选择器小部件。
  2. Key key,
  3. this.mode = CupertinoDatePickerMode.dateAndTime, // 显示模式
  4. @required this.onDateTimeChanged, // 当选定的日期和/或时间发生更改时的 回调
  5. DateTime initialDateTime, // 设置默认选择日期
  6. this.minimumDate, // 选择器可以滚动到的最小日期 没有限制则为空
  7. this.maximumDate, // 选择器可以滚动到的最大日期 没有限制则为空
  8. this.minimumYear = 1, // 选择器可以滚动到的最小年份
  9. this.maximumYear, //
  10. this.minuteInterval = 1, //分钟微调器的粒度(如果在当前模式下显示)。必须是60的整数倍。
  11. this.use24hFormat = false,
  12. this.backgroundColor, // 背景色
  13. }) : initialDateTime = initialDateTime ?? DateTime.now(),
  14. assert(mode != null),
  15. assert(onDateTimeChanged != null),
  16. assert(minimumYear != null),
  17. assert(
  18. minuteInterval > 0 && 60 % minuteInterval == 0,
  19. 'minute interval is not a positive integer factor of 60',
  20. ),

基本用法【日期】

  1. showDialog(
  2. context: context,
  3. builder: (context) {
  4. return AlertDialog(
  5. title: Text('选择结束日期', style: TextStyle( fontSize: 18, fontWeight: FontWeight.w500 ), textAlign: TextAlign.center,),
  6. content: Container(
  7. width: _width,
  8. height: 150,
  9. child: CupertinoDatePicker(
  10. initialDateTime: DateTime.now().add(Duration( days: -90 )), // 设置默认几天 3个月前 需要大于minimumDate
  11. mode: CupertinoDatePickerMode.date,
  12. minimumDate: DateTime.now().add(Duration( days: -91)), // 当前日期前进多少天
  13. maximumDate: DateTime.now().add(Duration(days: 0)), // 当前日期的后几天
  14. // backgroundColor: Colors.red,
  15. onDateTimeChanged: (e){
  16. print(e);
  17. },
  18. ),
  19. ),
  20. actions: <Widget>[
  21. FlatButton(child: Text('取消'),onPressed: (){
  22. Navigator.of(context).pop('cancel');
  23. },),
  24. FlatButton(child: Text('确认'),onPressed: (){
  25. Navigator.of(context).pop('ok');
  26. },),
  27. ],
  28. );
  29. });

image.png

  1. showDialog(
  2. context: context,
  3. builder: (context) {
  4. return AlertDialog(
  5. title: Text('选择结束日期', style: TextStyle( fontSize: 18, fontWeight: FontWeight.w500 ), textAlign: TextAlign.center,),
  6. content: Container(
  7. width: _width,
  8. height: 150,
  9. child: CupertinoDatePicker(
  10. mode: CupertinoDatePickerMode.date,
  11. minimumDate: DateTime.now().add(Duration( days: -91)), // 当前日期前进多少天
  12. maximumDate: DateTime.now().add(Duration(days: 1)), // 当前日期的后几天
  13. // backgroundColor: Colors.red,
  14. onDateTimeChanged: (e){
  15. print(e);
  16. },
  17. ),
  18. ),
  19. actions: <Widget>[
  20. FlatButton(child: Text('取消'),onPressed: (){
  21. Navigator.of(context).pop('cancel');
  22. },),
  23. FlatButton(child: Text('确认'),onPressed: (){
  24. Navigator.of(context).pop('ok');
  25. },),
  26. ],
  27. );
  28. });

image.png

基本用法【时间】

修改mode即可 mode: CupertinoDatePickerMode.dateAndTime,
image.png

国际化

本来日期是英文的,需要弄成中文就需要添加国际化标准

pubspec.yaml文件添加

  1. dependencies:
  2. flutter_localizations:
  3. sdk: flutter

在MaterialApp主文件添加

  1. localeListResolutionCallback:
  2. (List<Locale> locales, Iterable<Locale> supportedLocales) {
  3. return Locale('zh');
  4. },
  5. localeResolutionCallback:
  6. (Locale locale, Iterable<Locale> supportedLocales) {
  7. return Locale('zh');
  8. },
  9. localizationsDelegates: [
  10. GlobalMaterialLocalizations.delegate,
  11. GlobalWidgetsLocalizations.delegate,
  12. GlobalCupertinoLocalizations.delegate,
  13. ],
  14. supportedLocales: [
  15. const Locale('zh', 'CH'),
  16. const Locale('en', 'US'),
  17. ],

image.png