Android 当flifter中的文本字段中的文本发生更改时,是否有方法仅更改文本?
我在颤振中有一个文本字段,我想知道是否有一种方法可以只获取更改的文本,我们可以称之为delta,当文本字段中的文本更改时Android 当flifter中的文本字段中的文本发生更改时,是否有方法仅更改文本?,android,flutter,dart,Android,Flutter,Dart,我在颤振中有一个文本字段,我想知道是否有一种方法可以只获取更改的文本,我们可以称之为delta,当文本字段中的文本更改时 TextField( style: TextStyle(color: Colors.red), controller: _controller, focusNode: _focusNode, cursorColor: Colors.red, onChanged: (s) { print(s); }, ), 使用下面
TextField(
style: TextStyle(color: Colors.red),
controller: _controller,
focusNode: _focusNode,
cursorColor: Colors.red,
onChanged: (s) {
print(s);
},
),
使用下面的代码,我将仅在文本更改时获取整个文本值
例如:如果textfield中的文本是“Hello worl”,并且我添加了一个“d”,则onChanged中的值是“Hello world”。
是否有方法仅获取插入/删除/剪切/粘贴到操作执行位置的文本
我想得到一个自定义类型的变量,比如CustomTextAction,它包含一个索引,用于文本更改的位置、操作类型、执行的操作类型以及更改的内容或类似内容的值。例如,在上述情况下,它将是:
CustomTextAction:
- index: 10
- type: "insertion"
- value: "d"
类似的功能是否已经存在于Flitter中,或者我应该自己编写吗?这就是逻辑
class _MyWidgetState extends State<MyWidget> {
getNewValue(String oldValue, String currentValue) {
if (oldValue.length < currentValue.length) {
String newLetter = currentValue.replaceAll(oldValue, "")[0];
print("You added: " + newLetter);
return newLetter;
} else {
String newLetter = oldValue.replaceAll(currentValue, "")[0];
print("You deleted: " + newLetter);
return newLetter;
}
}
String oldValue = "";
// TextEditingController _controller = new TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Column(
children: [
TextFormField(onChanged: (currentValue) {
String newValue = getNewValue(oldValue, currentValue);
oldValue = currentValue;
print(newValue);
}),
],
),
);
}
}
class\u MyWidgetState扩展状态{
getNewValue(字符串oldValue、字符串currentValue){
if(oldValue.length
它只给出了您添加的最后一个字母,但您可以使用onEditingComplete中的函数来获得完整的更改