我有一个带有TextField和TextEditingController的简单演示应用程序。当我尝试
当我在iOS模拟器中打开应用程序时,当我退格时会出现两个事件。我只希望有一个事件。
复制步骤:
x
在控制台输出中,我看到:
flutter: current value: x
flutter: current value: x
flutter: current value:
但是我期望
flutter: current value: x
flutter: current value:
为什么我两次看到x
?
这是我的代码:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: Scaffold(
appBar: AppBar(
title: const Text('Backspace demo'),
),
body: Center(
child: BackspaceDemo(),
),
));
}
}
class BackspaceDemo extends StatefulWidget {
@override
_BackspaceDemoState createState() {
return _BackspaceDemoState();
}
}
class _BackspaceDemoState extends State<BackspaceDemo> {
final _controller = TextEditingController();
@override
void initState() {
super.initState();
_controller.addListener(handleTextChange);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
void handleTextChange() {
print("current value: " + _controller.text);
}
@override
Widget build(BuildContext context) {
return TextField(
controller: _controller,
autocorrect: false,
autofocus: true,
decoration: InputDecoration(
hintText: "Enter a text ...",
));
}
}
谢谢。