在Flutter中的键盘中输入文本后,单击完成按钮,TextField的onChanged属性将消失


class AddTaskScreen extends StatelessWidget {
const AddTaskScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
String newTask = '';
return Container(
color: const Color.fromARGB(255, 117, 117, 117),
child: Container(
decoration: const BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20.0),
topRight: Radius.circular(20.0),
),
),
child: Container(
padding: const EdgeInsets.all(30.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
'Add Task',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.amber[900],
fontSize: 30.0,
),
),
TextField(
onChanged: (value) {
newTask = value;
},
autofocus: true,
textCapitalization: TextCapitalization.sentences,
textAlign: TextAlign.center,
decoration: const InputDecoration(
focusedBorder: UnderlineInputBorder(
borderSide:
BorderSide(color: Color.fromRGBO(255, 111, 0, 1)),
),
),
),
const SizedBox(height: 30.0),
TextButton(
onPressed: () {
Provider.of<TaskData>(context, listen: false).addTask(newTask);
Navigator.pop(context);
},
style: TextButton.styleFrom(
backgroundColor: Colors.amber[900],
),
child: const Text(
'Add',
style: TextStyle(color: Colors.white),
),
),
],
),
),
),
);
}
}

如果我直接点击添加按钮而不点击键盘上的完成按钮,数据就会被添加到屏幕上。但在输入文本后点击完成按钮时,我在调试控制台上看到了这一行。

I/TextInputPlugin( 6522): Composing region changed by the framework. Restarting the input method.

尽管在键盘上单击完成后单击添加按钮,会添加一个空字段。

我试过使用一个控制器,它只是静态地将文本保存到键盘上,但对将文本添加到屏幕没有帮助。

点击软键盘中的done按钮会触发onSubmitted方法,而不是onChanged方法,请确保也覆盖

最新更新