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

Flutter TextField "Changing the content within the the composing region may cause the input"

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

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

当我查看引用的 link 时,我没有看到任何描述解决方案的内容。

为了测试这个,我制作了一个新的、干净的应用程序,只有一个 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}');
    });
  }

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

这只是一个警告。 İ如果你不使用电脑键盘,而是在模拟器上使用虚拟键盘。

尝试添加静态关键字。

 static late TextEditingController textEditingController;