Flutter TextField“更改组合区域内的内容可能会导致输入”

问题描述

我创建了一个新的 Flutter 应用程序,并尝试使用我之前在多个应用程序中使用过的过程在 TextField 小部件中设置初始值。每当我在字段中编辑内容时,Android Studio 运行窗口都会显示

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}');
    });
  }

解决方法

不幸的是,根据此线程:mutiny thread control docs,这看起来像是一个长期存在的问题,尚未由 Flutter 团队解决。我遇到了同样的问题。