我正在尝试从flutter的默认datepicker小部件中获取日期,但是当我选择日期时,就会有时间。我只想要日期而不是时间。&还希望更改日期格式。
这是我在某处示例中使用的内容。
DateTime selectedDate = DateTime.now(); Future<Null> _selectDate(BuildContext context) async { final DateTime picked = await showDatePicker( context: context, initialDate: selectedDate, firstDate: DateTime(2019), lastDate: DateTime(2020)); if (picked != null && picked != selectedDate) setState(() { selectedDate = picked; }); }
这就是我获得字符串价值的地方。它打印为“ 2019-04-16 12:18:06.018950”
child: FlatButton( materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, onPressed: () { _selectDate(context); print("Selected Date = $selectedDate"); }, child: Text(selectedDate == null ? "Select Date" : selectedDate.toString()), )),
我 只 想要此“ 2019-04-16 12:18:06.018950”中的日期,并且还更改显示日期的格式,例如 “ dd-mm-yyyy”
这可能吗?
我制作了Custom DateFormat,可以使用此代码,然后将输出为:16-04-2019
TextEditingController _datecontroller = new TextEditingController(); var myFormat = DateFormat('d-MM-yyyy'); Future<void> _selectDate(BuildContext context) async { final DateTime picked = await showDatePicker( context: context, initialDate: date, firstDate: DateTime(2015, 8), lastDate: DateTime(2101)); setState(() { date = picked ?? date; }); }
和Inkwell小部件是这样的
InkWell( onTap: () => _selectDate(context), child: IgnorePointer( child: TextField( controller: _datecontroller, decoration: InputDecoration( hintText: ('${myFormat.format(date)}'), ), ), ), ),