flutter日期时间选择器的代码怎么写

flutter日期时间选择器的代码怎么写,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

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;   });  } }

关于flutter日期时间选择器的代码怎么写问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。