zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Flutter 日期选择器与时间选择器

日期flutter 时间 选择器
2023-09-11 14:14:53 时间

更多文章请查看 lutter从入门 到精通

1 日期选择器

在这里插入图片描述



  //设置默认显示的日期为当前
  DateTime initialDate = DateTime.now();
  
  void showDefaultYearPicker(BuildContext context) async {
    final DateTime dateTime = await showDatePicker(
      context: context,
      //定义控件打开时默认选择日期
      initialDate: initialDate,
      //定义控件最早可以选择的日期
      firstDate: DateTime(2018, 1),
      //定义控件最晚可以选择的日期
      lastDate: DateTime(2022, 1),
      builder: (BuildContext context, Widget child) {
        return Theme(
          data: CommonColors.themData,
          child: child,
        );
      },
    );
    if (dateTime != null && dateTime != initialDate) {}
  }

2 时间选择器

在这里插入图片描述



  //设置显示显示的时间为当前
  TimeOfDay initialTime = TimeOfDay.now();
  void showDefaultDatePicker(BuildContext context) async {
    final TimeOfDay timeOfDay = await showTimePicker(
      context: context,
      initialTime: initialTime,
      builder: (BuildContext context, Widget child) {
        return Theme(
          data: CommonColors.themData,
          child: child,
        );
      },
    );
    if (timeOfDay != null && timeOfDay != initialTime) {
      setState(() {
        initialTime = timeOfDay;
      });
    }
  }

本公众号会首发系列专题文章,付费的视频课程会在公众号中免费刊登,在你上下班的路上或者是睡觉前的一刻,本公众号都是你浏览知识干货的一个小选择,收藏不如行动,在那一刻,公众号会提示你该学习了。

在这里插入图片描述