颤振文本字段"Changing the content within the the composing region may cause the input"



我创建了一个新的Flutter应用程序,并试图使用我以前在几个应用程序中使用过的过程在TextField小部件中设置初始值。每当我编辑字段中的内容时,Android Studio Run窗口就会显示:

W/TextInputPlugin(18696): Changing the content within the the composing region may cause the input method to behave strangely, and is therefore discouraged. See https://github.com/flutter/flutter/issues/78827 for more details
W/IInputConnectionWrapper(18696): getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper(18696): getSelectedText on inactive InputConnection
W/IInputConnectionWrapper(18696): getTextAfterCursor on inactive InputConnection
W/IInputConnectionWrapper(18696): beginBatchEdit on inactive InputConnection
W/IInputConnectionWrapper(18696): getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper(18696): endBatchEdit on inactive InputConnection

当我查看引用的链接时,我看不到任何描述解决方案的内容。

为了测试这一点,我制作了一个新的、干净的应用程序,只使用了Text小部件和TextEdit小部件,但我仍然遇到了同样的问题。除了设置默认值之外,我什么都不做。最终,我想捕获对输入字段的更改并将值写入本地存储,但我认为我应该首先修复这个输入错误。

这是我的测试应用程序的代码:

import 'package:flutter/material.dart';
const appName = 'TextField Test';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: appName,
debugShowCheckedModeBanner: false,
theme: ThemeData.light(),
darkTheme: ThemeData.dark(),
home: HomePage(title: appName),
);
}
}
class HomePage extends StatefulWidget {
HomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
late TextEditingController textEditingController;
String _inputValue = '';
@override
void initState() {
super.initState();
textEditingController = TextEditingController(text: _inputValue);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Padding(
padding: const EdgeInsets.all(10.0),
child:
Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
Text('This is some content in a Text widget'),
SizedBox(height: 10),
TextField(
autofocus: true,
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'Enter some text here'),
controller: textEditingController,
),
]),
));
}
}

有人能告诉我我做错了什么吗?

当我在控制器中添加一个监听器时,我注意到它对我键入的每个字符都会多次触发:

I/flutter (18696): Listener fired the e
I/flutter (18696): Listener fired the e
I/flutter (18696): Listener fired the en
I/flutter (18696): Listener fired the en

以下是侦听器代码:

@override
void initState() {
super.initState();
textEditingController = TextEditingController(text: _inputValue);
textEditingController.addListener(() {
print('Listener fired ${textEditingController.text}');
});
}

这只是一个警告。如果你不使用电脑键盘,而是在模拟器上使用虚拟键盘,这会让你感到困扰。

不幸的是,这看起来是一个长期存在的问题,Flutter团队根据此线程尚未解决:https://github.com/flutter/flutter/issues/9471.我遇到了同样的问题。

所以我在这里遇到了同样的"问题"。

我使用TextFormField小部件作为我的输入。当我没有为简单的标签输入定义keyboardType道具并开始键入随机输入时,就会出现这些日志。

我发现,当我定义keyboardType: TextInputType.name时,日志会永远消失。我还用keyboardType: TextInputType.emailkeyboardType: TextInputType.password测试了它,它在两者上都有效。

希望能有所帮助。

尝试添加static关键字。

static late TextEditingController textEditingController;

在我这边,当我在像这样的小部件构建功能

错误的textEditingController声明

@override
Widget build(BuildContext context) {
TextEditingController textEditingController = TextEditingController();

return TextField(
controller: textEditingController ,
decoration: InputDecoration(
hintText: "hintText",
labelText: "placeholder",
),
keyboardType: TextInputType.name,
onChanged: (text){

},
);  }

当我在像这样的小部件函数之外声明textEditingController时,这个问题就解决了

文本编辑控制器的正确声明

TextEditingController textEditingController = TextEditingController();
@override
Widget build(BuildContext context) {
return TextField(
controller: textEditingController ,
decoration: InputDecoration(
hintText: "hintText",
labelText: "placeholder",
),
keyboardType: TextInputType.name,
onChanged: (text){
});
}

同样,当设置textEditingController.text时,我建议在initState函数中设置它,而不是在小部件构建函数中设置,如下所示

@override
void initState() {
super.initState();
textEditingController.text = someValueVariable;
}

我希望这能有所帮助。

我遇到了相同的消息,这就是我解决它的方法:

TextFormField中,我添加了属性keyboardType: TextInputType.text,根据您的输入类型进行相应更改(例如:名称号码文本电话等(

之后,我添加了函数onEditingComplete: () {<your code here>},这允许您在文本编辑后启用任何未来操作,如更改焦点、提交表单等。完整示例:

TextFormField(
autofocus: true,
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'Enter some text here'),
controller: textEditingController,
keyboardType: TextInputType.text,
onEditingComplete: () {
print(textEditingController.text);
},
),

这是我的解决方案。禁用Gboard设置中的所有设置>文本更正。

https://github.com/siqwin/mask_text_input_formatter/issues/64#issuecomment-1327205058

相关内容

最新更新