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

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐