可以通过更新状态并传递一个新的InputDecoration对象来完成:
class MyAppHome extends StatefulWidget {
@override
_MyAppHomeState createState() => _MyAppHomeState();
}
class _MyAppHomeState extends State<MyAppHome> {
String _errorText;
@override
Widget build(BuildContext context) {
return new Scaffold(
body: Center(
child: TextField(
onSubmitted: (String text) {
setState(() {
if (text != "Hello") {
_errorText = 'Error: This is not an email';
} else {
_errorText = null;
}
});
},
decoration: InputDecoration(
hintText: "This is a hint",
errorText: _getErrorText(),
),
),
),
);
}
_getErrorText() {
return _errorText;
}
}