我一直在尝试通过controller:TextEditingController(text:"Enter DOB")
在Flutter
可编辑的TextField
小部件内的初始文本,但我暂时找不到解决办法。
我想使selectedDate
(或初始文本)在从TextField
中的showDatePicker
方法中选择日期后可编辑。
TextField(
// This controller will show the initial value as 'Enter DOB'
// and when the Date is selected from the showDatePicker it will
// get updated.
controller: TextEditingController(
// the text: property will check if the current value is null or not.
// if null it will show the "Enter your DOB" text, else the selected## Heading ##Date
// will get updated.
text: validator.selectedDate == null
? "Enter your DOB"
: "${validator.selectedDate}".split(' ')[0],
),
decoration: InputDecoration(
suffixIcon: IconButton(
icon: Icon(Icons.calendar_today_rounded),
onPressed: () {
validator.selectDate(context);
},
),
labelText: "yyyy-mm-dd",
),
onChanged: (String value) {
validator.changeDOB(value);
},
);
,
selectedDate
是DateTime
类型的变量,初始值为null
。selectDate(BuildContext context)
是showDatePicker(BuildContext context)
方法的一个方法。changeDOB(String value)
是一个将selectedDate
的值更新为TextField
的方法。
注意: 我使用Provider
作为状态管理方法。
我认为你必须将TextEditingController
作为局部变量,然后将其更改为setState()
是这样的:
import 'package:flutter/material.dart';
class DateTimeExample extends StatefulWidget {
@override
_DateTimeExampleState createState() => _DateTimeExampleState();
}
class _DateTimeExampleState extends State<DateTimeExample> {
TextEditingController _dobController; // Local variable to store TextFiled text
@override
void initState() {
// Initialize TextEditingController object with default text.
_dobController = TextEditingController(text: "Enter your DOB: ");
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body:Container(
child: Center(
child: TextField(
controller: _dobController,
decoration: InputDecoration(
suffixIcon: IconButton(
icon: Icon(Icons.calendar_today_rounded),
onPressed: () {
String selectedDate= validator.selectDate(context);
setState(() {
_dobController.text = selectedDate; // Change the text and refersh UI
});
},
),
labelText: "YYYY-MM-DD",
),
onChanged: (value) {
validator.changeDOB(value);
},
),
),
),
);
}
}
注意:你的validator.selecteDate(Context)
函数必须返回一个String
,这样它才能刷新你的状态。
SOLVED
我已经解决了我的问题,上面的答案给了我一个想法去做。
-
我刚刚声明了一个
TextEditingController()..text="Enter DOB"
变量,并将其传递给showDatePicker(BuildContext context)
方法。 -
从那里我给
controller.text
输入selectedDate.toString().split(' ')[0]
的值。 -
以及以
controller.text
为参数的changeDOB(controller.text)
。 -
我的代码如下:
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
class MyClass with ChangeNotifier {
// passing the initial text to the textField through controller.
var controller = TextEditingController()..text = "Enter DOB";
DateTime selectedDate; // initial value = null;
selectDate(BuildContext context) async {
var picked = await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(1800),
lastDate: DateTime.now(),
);
if (picked != null && picked != selectedDate) {
selectedDate = picked;
// passing the selectedDate to the controller.
controller.text = selectedDate.toString().split(' ')[0];
changeDOB(controller.text);
}
notifyListeners();
}
void changeDOB(String value){
//TODO: Your validation logic here.
}
}