在iOS模拟器键盘上按输入/返回按钮不会存储文本字段更改的数据为什么?



我正在制作一个TodoList,并使用onChanged参数将TextField的值存储在变量中。

当我点击外部以关闭iOS模拟器上的软件键盘时,它会正确存储值。

如果我使用软件键盘上的输入/返回按钮,它将存储空。

为什么会这样?

如何避免此行为?

我正在使用提供程序包作为状态管理解决方案。

class AddThingScreen extends StatelessWidget {
String title;

@override
Widget build(BuildContext context) {
return Container(
color: Color(0xff757575),
child: Container(
padding: EdgeInsets.all(20),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topRight: Radius.circular(20),
topLeft: Radius.circular(20),
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Text(
'Add thing',
style: TextStyle(
color: Colors.lightBlueAccent,
fontSize: 36.0,
),
),
TextField(
controller: textEditingController,
autofocus: true,
autocorrect: true,
textAlign: TextAlign.center,
decoration: InputDecoration(
focusColor: Colors.lightBlueAccent,
),
onChanged: (newThingTitle) {
title = newThingTitle;
},
),
FlatButton.icon(
color: Colors.lightBlueAccent,
icon: Icon(
Icons.add,
color: Colors.white,
),
label: Text(
'Add it',
style: TextStyle(
color: Colors.white,
),
textAlign: TextAlign.center,
),
onPressed: () {
Provider.of<ThingData>(context).addThing(title);
Navigator.pop(context);
},
)
],
),
),
);
}
}

如您所见,我调用了存储在 ThingData 类中的一种方法,该方法将一个新事物添加到列表中,然后通知听众。

class ThingData extends ChangeNotifier {
List<Thing> _things = [
Thing(name: 'Buy Cheese', isDone: false),
Thing(name: 'Buy Flatbread', isDone: true),
Thing(name: 'Buy Hot Sauce', isDone: false),
];
int get thingCount {
return _things.length;
}
UnmodifiableListView<Thing> get thingsList {
return UnmodifiableListView<Thing>(_things);
}
void addThing(String newThingTitle) {
final thing = Thing(name: newThingTitle);
_things.add(thing);
notifyListeners();
}
}

因为你已经在StatelessWidget的构建方法的主体中定义了title

如果你真的想存储标题的状态,你需要使用 StatelessWidget:

class AddThingScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _AddThingScreenState();
}
}
class _AddThingScreen State extends State<AddThingScreen> {
String title;
@override
initState() {
super.initState();
title = '';
}
@override
Widget build(BuildContext context) {
return Container(
color: Color(0xff757575),
child: Container(
padding: EdgeInsets.all(20),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topRight: Radius.circular(20),
topLeft: Radius.circular(20),
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Text(
'Add thing',
style: TextStyle(
color: Colors.lightBlueAccent,
fontSize: 36.0,
),
),
TextField(
autofocus: true,
autocorrect: true,
textAlign: TextAlign.center,
decoration: InputDecoration(
focusColor: Colors.lightBlueAccent,
),
onChanged: (newThingTitle) {
setState(() => {
title = newThingTitle;
});
},
),
FlatButton.icon(
color: Colors.lightBlueAccent,
icon: Icon(
Icons.add,
color: Colors.white,
),
label: Text(
'Add it',
style: TextStyle(
color: Colors.white,
),
textAlign: TextAlign.center,
),
onPressed: () {
Provider.of<ThingData>(context).addThing(title);
Navigator.pop(context);
},
)
],
),
),
);
}
}

您需要向文本字段添加键盘操作:

TextFormField( 
...
textInputAction: TextInputAction.done,
...
)

然后使用onFieldSubmited回调处理行为,如下所示:

TextFormField( 
...
textInputAction: TextInputAction.done,
onFieldSubmitted: (newThingTitle) {
title = newThingTitle;
},
...
)

最新更新