0
点赞
收藏
分享

微信扫一扫

flutter的TextField如何提示验证错误


可以通过更新状态并传递一个新的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;
}
}


举报

相关推荐

0 条评论