Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 如何在Flatter中使用DateFormat选择出生日期?_Flutter_Dart - Fatal编程技术网

Flutter 如何在Flatter中使用DateFormat选择出生日期?

Flutter 如何在Flatter中使用DateFormat选择出生日期?,flutter,dart,Flutter,Dart,我有一个出生日期格式的字段 我尝试了一些代码,但它显示的日期以及时间,但我只想要日期 有什么完美的方法吗 变数 String initValue="Select your Birth Date"; bool isDateSelected= false; DateTime birthDate; // instance of DateTime String birthDateInString; 这里是日期时间选择器的代码 GestureDetector( child: new Icon(Ic

我有一个出生日期格式的字段

我尝试了一些代码,但它显示的日期以及时间,但我只想要日期

有什么完美的方法吗

变数

String initValue="Select your Birth Date";
bool isDateSelected= false;
DateTime birthDate; // instance of DateTime
String birthDateInString;
这里是日期时间选择器的代码

GestureDetector(
   child: new Icon(Icons.calendar_today),
   onTap: ()async{
     final datePick= await showDatePicker(
        context: context,
        initialDate: new DateTime.now(),
        firstDate: new DateTime(1900),
        lastDate: new DateTime(2100)
       );
    if(datePick!=null && datePick!=birthDate){
      setState(() {
        birthDate=datePick; 
        isDateSelected=true;
        birthDateInString = "${birthDate.month}/${birthDate.day}/${birthDate.year}"; 
      });
     }
   }
 )
此小部件用于显示日期

 new Text(isDateSelected ? "$birthDate":initValue),
定义一个变量

String birthDateInString;
DateTime birthDate;
然后在
onTap

birthDateInString = "${birthDate.month}/${birthDate.day}/${birthDate.year}"; // 08/14/2019

更新:

GestureDetector(
    child: new Icon(Icons.calendar_today),
    onTap: ()async{
      final datePick= await showDatePicker(
          context: context,
          initialDate: new DateTime.now(),
          firstDate: new DateTime(1900),
          lastDate: new DateTime(2100)
      );
      if(datePick!=null && datePick!=birthDate){
        setState(() {
          birthDate=datePick;
          isDateSelected=true;
          
          // put it here
          birthDateInString = "${birthDate.month}/${birthDate.day}/${birthDate.year}"; // 08/14/2019
          
        });
      }
    }
)

最好使用
DateFormat
(来自软件包),而不是手动形成显示字符串。这样,您可以本地化日期。e、 g

import 'package:intl/intl.dart';
....
new Text(isDateSelected ? DateFormat.yMMMd().format(birthDate) : initValue)

省道参考:


您需要为您的用例使用正确的格式化程序

没有任何名为dateFormat的变量,我已经编辑了代码。现在检查
birthDate.month
,`birthDate.day`,
birthDate.year
,这些是从哪里取的?谢谢。我已经按照你说的更新了我的代码,但是我得到了一个错误:
.month
.day
.year
不支持变量
birthDate
我仍然看到旧代码,你在哪里更新了它?谢谢你,工作得很好。我在数据类型上犯了一些错误,这就是为什么不支持。要声明birthDate变量:
DateTime birthDate