我有一个旨在让用户编辑数据text field
的flutter
应用程序。当 TextField 出现时,它已经有数据了,这很好用。问题在于,当用户将光标放在 TextField 的末尾或其中的任何位置并开始键入时,光标会移回开头,有时会删除第一个或两个单词。
我试过将函数调用放在 setState 中的控制器,但没有帮助。如果我根本不使用控制器,问题就会消失,但是在他们在框外单击后我无法保存他们的输入。
这是文本字段的代码,它在更改
TextField(
decoration: InputDecoration(
border: InputBorder.none
),
controller: controller,
autofocus: true,
onChanged: (text) {
controller..text = text;
controller..selection = TextSelection.collapsed(offset: controller.text.length);
},
maxLines: 8,
Run Code Online (Sandbox Code Playgroud)
这是我创建控制器的地方
TextEditingController controller = new TextEditingController();
controller.text = *initial text here*;
Run Code Online (Sandbox Code Playgroud)
这是颤振医生
[?] Flutter (Channel stable, v1.5.4-hotfix.2, on Microsoft Windows [Version 10.0.17134.829], locale en-US)
• Flutter version 1.5.4-hotfix.2 at C:\Users\jhall\Desktop\flutter
• Framework revision 7a4c33425d (9 weeks ago), 2019-04-29 11:05:24 -0700
• Engine revision 52c7a1e849
• Dart version 2.3.0 (build 2.3.0-dev.0.5 a1668566e5)
[?] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
• Android SDK at C:\Users\jhall\AppData\Local\Android\sdk
• Android NDK location not configured (optional; useful for native profiling support)
• Platform android-Q, build-tools 28.0.3
• Java binary at: C:\Program Files\Android\Android Studio1\jre\bin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)
• All Android licenses accepted.
[?] Android Studio (version 3.4)
• Android Studio at C:\Program Files\Android\Android Studio1
• Flutter plugin version 35.3.1
• Dart plugin version 183.6270
• Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)
[?] VS Code, 64-bit edition (version 1.24.1)
• VS Code at C:\Program Files\Microsoft VS Code
• Flutter extension version 2.21.1
[?] Connected device (1 available)
• Android SDK built for x86 • emulator-5554 • android-x86 • Android 9 (API 28) (emulator)
• No issues found!
Run Code Online (Sandbox Code Playgroud)
所以我基本上只需要您期望从文本输入字段获得的正常功能,让用户能够将光标放在他们想要的任何地方并输入。
我试过一起摆脱 on Change 并且问题仍然存在。
wz3*_*366 12
您可以在更改文本之前捕获光标位置,然后将光标位置重新应用到新文本:
onChanged: (text) {
TextSelection previousSelection = controller.selection;
controller.text = text;
controller.selection = previousSelection;
}
Run Code Online (Sandbox Code Playgroud)
你不必做
controller..text = text;
里面onChanged
因为一旦你把它连接到文本字段控制器的文本自动改变。
原因是一旦你将一些文本设置为controller
,它就会将re-applies
文本移动到前面。
在你的情况下:
TextField(
decoration: InputDecoration(
border: InputBorder.none
),
controller: controller,
autofocus: true,
onChanged: (text) {},
maxLines: 8,
)
Run Code Online (Sandbox Code Playgroud)
应该可以解决问题。
onChange
我认为如果您从代码中删除 ,您就会得到您所需要的!
一件重要且可能导致问题的事情是您的控制器必须仅设置一次!您可以将其放在 Widget 状态的构造函数中或放在initState()
. 它会是这样的:
import 'package:flutter/material.dart';
class MyStatefulPage extends StatefulWidget {
@override
State<MyStatefulPage> createState() {
return _MyStatefulPageState();
}
}
class _MyStatefulPageState extends State<MyStatefulPage> {
TextEditingController controller;
@override
void initState() {
super.initState();
controller = new TextEditingController();
controller.text = 'My Initial Text';
}
@override
Widget build(BuildContext context) {
return TextField(
decoration: InputDecoration(
border: InputBorder.none
),
controller: controller,
autofocus: true,
maxLines: 8,
);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
13177 次 |
最近记录: |