Flutter 按iOS模拟器键盘上的enter/return按钮不会';t在更改数据时存储文本字段为什么?

Flutter 按iOS模拟器键盘上的enter/return按钮不会';t在更改数据时存储文本字段为什么?,flutter,provider,Flutter,Provider,我正在创建TodoList,并使用onChanged参数将TextField的值存储在变量中 当我点击outside关闭iOS模拟器上的软件键盘时,它会正确存储值 如果我使用软件键盘上的输入/返回按钮,它将存储空值 为什么会这样 我能做些什么来避免这种行为 我使用provider包作为状态管理解决方案 class AddThingScreen extends StatelessWidget { String title; @override Widget build(BuildC

我正在创建TodoList,并使用onChanged参数将TextField的值存储在变量中

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

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

为什么会这样

我能做些什么来避免这种行为

我使用provider包作为状态管理解决方案

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);
              },
            )
          ],
        ),
      ),
    );
  }
}
class AddThingScreen扩展了无状态小部件{
字符串标题;
@凌驾
小部件构建(构建上下文){
返回容器(
颜色:颜色(0xFF7575),
子:容器(
填充:边缘设置。全部(20),
装饰:盒子装饰(
颜色:颜色,白色,
borderRadius:仅限borderRadius(
右上角:半径。圆形(20),
左上:半径。圆形(20),
),
),
子:列(
crossAxisAlignment:crossAxisAlignment.stretch,
儿童:[
正文(
“添加内容”,
样式:TextStyle(
颜色:Colors.lightBlueAccent,
字体大小:36.0,
),
),
文本字段(
控制器:textEditingController,
自动对焦:对,
自动更正:正确,
textAlign:textAlign.center,
装饰:输入装饰(
focusColor:Color.lightBlueAccent,
),
更改后:(新标题){
标题=新标题;
},
),
FlatButton.icon(
颜色:Colors.lightBlueAccent,
图标:图标(
Icons.add,
颜色:颜色,白色,
),
标签:文本(
“添加它”,
样式:TextStyle(
颜色:颜色,白色,
),
textAlign:textAlign.center,
),
已按下:(){
Provider.of(上下文).addThing(标题);
Navigator.pop(上下文);
},
)
],
),
),
);
}
}
如您所见,我调用了一个存储在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();
  }
}

类ThingData扩展ChangeNotifier{
列出事物=[
事物(名称:“买奶酪”,isDone:false),
事物(名称:“购买扁平面包”,isDone:true),
东西(名字:“买辣酱”,isDone:false),
];
我得到东西计数{
返回长度;
}
不可修改的ListView获取内容列表{
返回不可修改的ListView(_things);
}
void addThing(字符串newThingTitle){
最终事物=事物(名称:newThingTitle);
_事物。添加(事物);
notifyListeners();
}
}

因为您在无状态小部件的构建方法主体中定义了
标题

如果确实要存储标题的状态,则需要使用无状态小部件:

class AddThingScreen扩展StatefulWidget{
@凌驾
状态createState(){
返回_AddThingScreenState();
}
}
类_AddThingScreen状态扩展状态{
字符串标题;
@凌驾
initState(){
super.initState();
标题='';
}
@凌驾
小部件构建(构建上下文){
返回容器(
颜色:颜色(0xFF7575),
子:容器(
填充:边缘设置。全部(20),
装饰:盒子装饰(
颜色:颜色,白色,
borderRadius:仅限borderRadius(
右上角:半径。圆形(20),
左上:半径。圆形(20),
),
),
子:列(
crossAxisAlignment:crossAxisAlignment.stretch,
儿童:[
正文(
“添加内容”,
样式:TextStyle(
颜色:Colors.lightBlueAccent,
字体大小:36.0,
),
),
文本字段(
自动对焦:对,
自动更正:正确,
textAlign:textAlign.center,
装饰:输入装饰(
focusColor:Color.lightBlueAccent,
),
更改后:(新标题){
设置状态(()=>{
标题=新标题;
});
},
),
FlatButton.icon(
颜色:Colors.lightBlueAccent,
图标:图标(
Icons.add,
颜色:颜色,白色,
),
标签:文本(
“添加它”,
样式:TextStyle(
颜色:颜色,白色,
),
textAlign:textAlign.center,
),
已按下:(){
Provider.of(上下文).addThing(标题);
Navigator.pop(上下文);
},
)
],
),
),
);
}
}

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

TextFormField(
...
textInputAction:textInputAction.done,
...
)
然后使用
onfildsubmited
回调处理行为,如下所示:

TextFormField(
...
textInputAction:textInputAction.done,
onfielsubmitted:(newThingTitle){
标题=新标题;
},
...
)

眼光很好,如果我在本地管理状态,但我在使用提供程序包管理状态,这将是正确的。我将修改我的问题,使之更准确。感谢您的努力。当然,最后的“东西”是使用集中状态管理进行管理的,但中间文本不是,因此您需要将其存储在某个地方,直到您将其交给
ThingData
我已将其移出构建方法,并且出现了相同的行为。因此,我有两个问题。您帮助解决了这些问题