Flutter 在单击textformfield时如何显示日期选择器
当我单击要显示日期选择器的字段时,我创建了一个textFormField,然后在选择日期后,我必须从选择器中选择一个日期,以便在textFormField中设置所选日期。我不知道为什么要在单击Flutter 在单击textformfield时如何显示日期选择器,flutter,Flutter,当我单击要显示日期选择器的字段时,我创建了一个textFormField,然后在选择日期后,我必须从选择器中选择一个日期,以便在textFormField中设置所选日期。我不知道为什么要在单击textFormField时显示日期选择器 顺便说一句,您必须将TextFormField的enabled=false属性设置为TextFormField并将gesturedector设置为具有onTap属性的 正如另一个答案@Lekr0所指出的,现在可以使用TextFormField的onTap()属性来
textFormField
时显示日期选择器
顺便说一句,您必须将
TextFormField
的enabled=false
属性设置为TextFormField
并将gesturedector
设置为具有onTap属性的
正如另一个答案@Lekr0所指出的,现在可以使用TextFormField
的onTap()
属性来完成
new TextFormField(
decoration: new InputDecoration(hintText: 'DOB'),
maxLength: 10,
validator: validateDob,
onSaved: (String val) {
strDob = val;
},
),
Future _selectDate() async {
DateTime picked = await showDatePicker(
context: context,
initialDate: new DateTime.now(),
firstDate: new DateTime(2016),
lastDate: new DateTime(2019)
);
if(picked != null) setState(() => _value = picked.toString());
}
InkWell(
onTap: () {
_selectDate(); // Call Function that has showDatePicker()
},
child: IgnorePointer(
child: new TextFormField(
decoration: new InputDecoration(hintText: 'DOB'),
maxLength: 10,
// validator: validateDob,
onSaved: (String val) {},
),
),
),
原始答案:
简单的方法:
用IgnorePointer
包装TextFormField
&用InkWell
包装IgnorePointer
TextFormField(
onTap: (){
// Below line stops keyboard from appearing
FocusScope.of(context).requestFocus(new FocusNode());
// Show Date Picker Here
},
)
同样在您的中选择date()
makelastDate:newdatetime(2020))代码>否则将出现错误。您可以使用OnTap属性来实现此目的
要停止显示键盘,可以将TextFormField的readOnly属性设置为true
TextEditingController dateCtl = TextEditingController();
TextFormField(
controller: dateCtl,
decoration: InputDecoration(
labelText: "Date of birth",
hintText: "Ex. Insert your dob",),
onTap: () async{
DateTime date = DateTime(1900);
FocusScope.of(context).requestFocus(new FocusNode());
date = await showDatePicker(
context: context,
initialDate:DateTime.now(),
firstDate:DateTime(1900),
lastDate: DateTime(2100));
dateCtl.text = date.toIso8601String();},)
试试这个
TextFormField(
readOnly: true,
...
);
我正在使用AllPlaceComman小部件
TextEditingController initialDateval=TextEditingController();
Future\u selectDate()异步{
已拾取日期时间=等待显示日期选择器(
上下文:上下文,
initialDate:new DateTime.now(),
firstDate:新日期时间(2020年),
最后日期:新日期时间(2030年);
如果(已拾取!=null)
设定状态(
()=>{data.registrationdate=picked.toString(),
intialdateval.text=picked.toString()
}
);
}
TextFormField(
//focusNode:_focusNode,
键盘类型:TextInputType.phone,
自动更正:错误,
控制员:intialdateval,
已保存:(值){
data.registrationdate=值;
},
onTap:(){
_选择日期();
FocusScope.of(context).requestFocus(newfocusnode());
},
maxLines:1,
//初始值:“Aseem Wangoo”,
验证器:(值){
if(value.isEmpty | value.length<1){
返回“选择日期”;
}
},
装饰:输入装饰(
labelText:“注册日期”,
//是的,
图标:const图标(今天的图标、日历),
标签样式:
TextStyle(decorationStyle:TextDecorationStyle.solid),
),
),
此textFormField用于输入用户的出生日期。因此,当用户选择我们每年需要更改的字段时,我想显示datepicker。lastDate
。@JohnJoe no-我们可以像这样编码-lastDate:DateTime.now()。添加(持续时间(天:365))代码>我已经完成了,但它没有在TextFormField上显示所选日期。我错过了什么吗?@spycbanda我也在为此挣扎。出现日期选择器,但字段中未显示日期。你修好了吗?@ALizard注意这应该被标记为正确答案,没有必要在其他小部件中包装TextFormField
。我很好奇,如何清理FocusNode
使用的资源?你应该首先创建一个TextEditingController(你的控制器),然后在你的TextFormField中使用它,调用showDatePicker()方法,获取所选值,并通过命令yourController将其插入TextFormField。text请编辑您的答案以提供其他解释。这里的关键属性是什么?您在这里提到的原始代码丢失了什么?请记住,Stack Overflow旨在帮助未来读者了解类似问题,而不仅仅是为眼前的问题提供编码解决方案。此外,由于这将在两年内推出,因此还有六个其他答案,包括plus,请解释您的方法与已确定的答案之间的差异。什么时候你的方法可能会被优先考虑?它做了什么而原始答案没有?
TextFormField(
readOnly: true,
...
);
GestureDetector(
onTap: () {
var tm = showCupertinoDatePicker(
firstDate:
defaultDateTime.add(Duration(days: -1)),
lastDate: defaultDateTime.add(Duration(days: 1)),
context: context,
initialDate: newDate);
tm.then((selectedDate) {
if (selectedDate != null) {
setState(() {
newDate = selectedDate;
});
dateController.text =
"${DateTimeUtils.formatDate(newDate)}";
}
});
},
child: AbsorbPointer(
child: TextFormField(
controller: dateController,
autofocus: false,
validator: ((val) {
if (val.trim().isEmpty) {
return “Select Date";
}
return null;
}),
decoration: InputDecoration(
icon: Icon(Icons.today),
suffix: Text("Tap to change",
style: Theme.of(context)
.textTheme
.caption))),
),
),
TextEditingController intialdateval = TextEditingController();
Future _selectDate() async {
DateTime picked = await showDatePicker(
context: context,
initialDate: new DateTime.now(),
firstDate: new DateTime(2020),
lastDate: new DateTime(2030));
if (picked != null)
setState(
() => { data.registrationdate = picked.toString(),
intialdateval.text = picked.toString()
}
);
}
TextFormField(
// focusNode: _focusNode,
keyboardType: TextInputType.phone,
autocorrect: false,
controller: intialdateval,
onSaved: (value) {
data.registrationdate = value;
},
onTap: () {
_selectDate();
FocusScope.of(context).requestFocus(new FocusNode());
},
maxLines: 1,
//initialValue: 'Aseem Wangoo',
validator: (value) {
if (value.isEmpty || value.length < 1) {
return 'Choose Date';
}
},
decoration: InputDecoration(
labelText: 'Registration Date.',
//filled: true,
icon: const Icon(Icons.calendar_today),
labelStyle:
TextStyle(decorationStyle: TextDecorationStyle.solid),
),
),
final _dateController = useTextEditingController();
TextFormField(
readOnly: true,
controller: _dateController,
decoration: InputDecoration(
labelText: 'Date',
),
onTap: () async {
await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(2015),
lastDate: DateTime(2025),
).then((selectedDate) {
if (selectedDate != null) {
_dateController.text =
DateFormat('yyyy-MM-dd').format(selectedDate);
}
});
},
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter date.';
}
return null;
},
)