flutter - 无法从 onchange 更新字符串变量
问题描述
我对颤动很陌生,我试图将 String newTask更新为文本字段中的值,但它一直将其重置为 null。
class AddTask extends StatelessWidget {
@override
Widget build(BuildContext context) {
String newTask;
return Container(
color: Color(0xff757575),
child: Container(
padding: EdgeInsets.symmetric(vertical:20.0,horizontal: 40.0),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(topLeft: Radius.circular(20.0),topRight: Radius.circular(20.0)),
),
child: Column(
children: <Widget>[
Text('Add Task',style: TextStyle(fontSize: 30.0,color: Colors.lightBlueAccent,fontWeight: FontWeight.bold),),
TextField(
onChanged:(value){
newTask = value;
},
autofocus: true,),
FlatButton(
child: Text('Add Task'),
onPressed: (){
print(newTask);
},
),
],
),
),
);
}
}
我究竟做错了什么?
解决方案
please use below in onChanged
event of TextField..
TextField(
onChanged:(value){
newTask = value;
},
autofocus: true,),
according to me this is correct I also run this code and its return right value.
推荐阅读
- r - 在 R 中为对过去几年敏感的面板数据创建虚拟变量
- mysql - MySQL 说:文档 #2054 - 服务器请求客户端未知的身份验证方法
- qt - QtQuick StackView 加载问题
- oracle-sqldeveloper - SQL Developer 未使用我的机器的主机名进行连接
- javascript - 如何从 ip 地址 javascript 等格式中获取最后一位数字?
- android - 自定义相机变焦崩溃(重启)三星手机
- java - 如何在 JAVA 中使用 TypeSafe 从配置中解析地图?
- python - 对于任何索引,返回前两个索引的最小值
- flutter - Flutter:列对齐项目以具有相同的宽度
- css - 使用样式组件时删除了父样式