你真的会用Flutter日期类组件吗

移动开发 作者: 2024-08-25 11:00:02
Flutter系统提供了一些日期选择类组件,比如DayPicker、MonthPicker、YearPicker、showDatePicker、CupertinoDatePicker等,其中前4个为M
  • selectedDate:选中的日期,选中的日期有圆形背景。
  • currentDate:当前日期,文字高亮。
  • onChanged:用户选择的日期发生变化时回调。
  • firstDate:可选日期的开始值。
  • lastDate:可选日期的结束值。
  • displayedMonth:显示的月份
DateTime _selectedDate = DateTime.now();

DayPicker(
  selectedDate: _selectedDate,currentDate: DateTime.now(),onChanged: (date) {
    setState(() {
      _selectedDate = date;
    });
  },firstDate: DateTime(2020,5,1),lastDate: DateTime(2020,31),displayedMonth: DateTime(2020,5),)
DayPicker(
  selectableDayPredicate: (date) {
    return date.difference(DateTime.now()).inMilliseconds < 0;
  },...
)
DateTime _selectedDate = DateTime.now();
MonthPicker(
  selectedDate: _selectedDate,12),)
YearPicker(
  selectedDate: _selectedDate,firstDate: DateTime(2000,)
RaisedButton(
  onPressed: () async {
    var result = await showDatePicker(
        context: context,initialDate: DateTime.now(),firstDate: DateTime(2020),lastDate: DateTime(2030));
    print('$result');
  },)
showDatePicker(
  builder: (context,child) {
    return Theme(
      data: ThemeData.dark(),child: child,);
  },...
)
 var _dateTime = DateTime.now();
CupertinoDatePicker(
  initialDateTime: _dateTime,onDateTimeChanged: (date) {
    setState(() {
      _dateTime = date;
    });
  },)
  • time:只显示时间,效果:4 | 14 | PM
  • date:只显示日期,效果:July | 13 | 2012
  • dateAndTime:时间和日期都显示,效果: Fri Jul 13 | 4 | 14 | PM
CupertinoDatePicker(
  minimumDate: DateTime.now().add(Duration(days: -1)),maximumDate: DateTime.now().add(Duration(days: 1)),...
)
CupertinoDatePicker(
  use24hFormat: true,...
)
RaisedButton(
  onPressed: () async {
    showTimePicker(
        context: context,initialTime: TimeOfDay.now());
  },)
showTimePicker(
    context: context,initialTime: TimeOfDay.now(),builder: (context,child) {
      return MediaQuery(
        data: MediaQuery.of(context)
            .copyWith(alwaysUse24HourFormat: true),);
    });
CupertinoTimerPicker(
  onTimerDurationChanged: (Duration duration){
  },)
CupertinoTimerPicker(
  mode: CupertinoTimerPickerMode.hm,...
)
var now = DateTime.now();
return Container(
  height: 200,child: CupertinoTimerPicker(
    initialTimerDuration: Duration(hours: now.hour,minutes: now.minute,seconds: now.second),onTimerDurationChanged: (Duration duration) {},),);
dependencies:
  flutter_localizations:
    sdk: flutter    
MaterialApp(
  localeListResolutionCallback:
          (List<Locale> locales,Iterable<Locale> supportedLocales) {
        return Locale('zh');
      },localeResolutionCallback:
          (Locale locale,localizationsDelegates: [
        GlobalMaterialLocalizations.delegate,GlobalWidgetsLocalizations.delegate,GlobalCupertinoLocalizations.delegate,],supportedLocales: [
        const Locale('zh','CH'),const Locale('en','US'),...
)
class MyLocalizationsDelegate
    extends LocalizationsDelegate<CupertinoLocalizations> {
  const MyLocalizationsDelegate();

  @override
  bool isSupported(Locale locale) => locale.languageCode == 'zh';

  @override
  Future<CupertinoLocalizations> load(Locale locale) =>
      ZhCupertinoLocalizations.load(locale);

  @override
  bool shouldReload(MyLocalizationsDelegate old) => false;

  @override
  String toString() => 'DefaultCupertinoLocalizations.delegate(zh)';
}
class ZhCupertinoLocalizations implements CupertinoLocalizations {
  const ZhCupertinoLocalizations();

  static const List<String> _shortWeekdays = <String>[
    '自周一','自周二','自周三','自周四','自周五','自周六','自周日',];

  static const List<String> _shortMonths = <String>[
    '1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月',];

  static const List<String> _months = <String>[
    '1月',];

  @override
  String datePickerYear(int yearIndex) => yearIndex.toString();

  @override
  String datePickerMonth(int monthIndex) => _months[monthIndex - 1];

  @override
  String datePickerDayOfMonth(int dayIndex) => dayIndex.toString();

  @override
  String datePickerHour(int hour) => hour.toString();

  @override
  String datePickerHourSemanticsLabel(int hour) => hour.toString() + " o'clock";

  @override
  String datePickerMinute(int minute) => minute.toString().padLeft(2,'0');

  @override
  String datePickerMinuteSemanticsLabel(int minute) {
    if (minute == 1) return '1 分';
    return minute.toString() + ' 分';
  }

  @override
  String datePickerMediumDate(DateTime date) {
    return '${_shortWeekdays[date.weekday - DateTime.monday]} '
        '${_shortMonths[date.month - DateTime.january]} '
        '${date.day.toString().padRight(2)}';
  }

  @override
  DatePickerDateOrder get datePickerDateOrder => DatePickerDateOrder.mdy;

  @override
  DatePickerDateTimeOrder get datePickerDateTimeOrder =>
      DatePickerDateTimeOrder.date_time_dayPeriod;

  @override
  String get anteMeridiemAbbreviation => '上午';

  @override
  String get postMeridiemAbbreviation => '下午';

  @override
  String get todayLabel => '今天';

  @override
  String get alertDialogLabel => 'Alert';

  @override
  String timerPickerHour(int hour) => hour.toString();

  @override
  String timerPickerMinute(int minute) => minute.toString();

  @override
  String timerPickerSecond(int second) => second.toString();

  @override
  String timerPickerHourLabel(int hour) => hour == 1 ? '小时' : '小时';

  @override
  String timerPickerMinuteLabel(int minute) => '分.';

  @override
  String timerPickerSecondLabel(int second) => '秒.';

  @override
  String get cutButtonLabel => '剪贴';

  @override
  String get copyButtonLabel => '拷贝';

  @override
  String get pasteButtonLabel => '黏贴';

  @override
  String get selectAllButtonLabel => '选择全部';

  static Future<CupertinoLocalizations> load(Locale locale) {
    return SynchronousFuture<CupertinoLocalizations>(
        const ZhCupertinoLocalizations());
  }

  /// A [LocalizationsDelegate] that uses [DefaultCupertinoLocalizations.load]
  /// to create an instance of this class.
  static const LocalizationsDelegate<CupertinoLocalizations> delegate =
      MyLocalizationsDelegate();
}
原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_68358.html