Flutter 日期时间DatePicker控件及国际化

移动开发 作者: 2024-08-25 01:30:01
注意:无特殊说明,Flutter版本及Dart版本如下: Flutter版本: 1.12.13+hotfix.5 Dart版本: 2.7.0 DatePicker Flutter并没有DatePick
RaisedButton(
  onPressed: () async {
    var result = await showDatePicker(
        context: context,initialDate: DateTime.now(),firstDate: DateTime(2020),lastDate: DateTime(2021));
    print('$result');
  },)
showDatePicker(
  selectableDayPredicate: (DateTime day) {
    return day.difference(DateTime.now()).inDays < 2;
  },...
)
showDatePicker(
  builder: (context,child) {
    return Theme(
      data: ThemeData.dark(),child: child,);
  },...
)

中文支持

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter    
MaterialApp(
  localizationsDelegates: [
    GlobalMaterialLocalizations.delegate,GlobalWidgetsLocalizations.delegate,],supportedLocales: [
    const Locale('zh','CH'),const Locale('en','US'),locale: Locale('zh'),...
)
showDatePicker(
  locale: Locale('zh'),...
)
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),);
    });
showTimePicker(
    context: context,child) {
      return Localizations(
          locale: const Locale('zh'),delegates: <LocalizationsDelegate>[
            GlobalMaterialLocalizations.delegate,]
      );
    });
 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,...
)
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) {},),);
原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_68129.html