显示TextField的错误文本不起作用

时间:2020-05-30 04:13:51

标签: flutter textfield

我正在应用程序上构建changePassword模块,我想检查该字段是否为空但不能正常工作,而且我不知道为什么

Card(
  color: Color(0xFFDBE2EF),
  margin: EdgeInsets.symmetric(
      vertical: 10, horizontal: 25),
  child: TextField(
    controller: passwordConfirmController,
    obscureText: true,
    decoration: InputDecoration(
        border: OutlineInputBorder(),
        labelText: 'Confirmar contraseña',
        labelStyle:
            TextStyle(fontFamily: 'Montserrat')),
  ),
),
SizedBox(
  height: 20,
),
RaisedButton(
    color: Color(0xFFDBE2EF),
    onPressed: () => {
          setState(() {
            print("Valor _validate antes");
            print(_validate);
            print("Valor isEmpty");
            print(passwordController.text.isEmpty);
            passwordController.text.isEmpty
                ? _validate = true
                : _validate = false;
            _isLoading = true;
            print("Valor _validate después");
            print(_validate);
          }),
          changePass(passwordController.text),
        },
    shape: RoundedRectangleBorder(
      borderRadius: BorderRadius.circular(5),
    ),
    child: Text(
      'Actualizar',
      style: TextStyle(
          fontSize: 20,
          fontFamily: 'Montserrat',
          color: Color(0xFF002D53),
          fontWeight: FontWeight.bold),
    )),

我检查密码是否为空的凸起按钮上的值似乎在打印期望值,但错误文本不起作用

2 个答案:

答案 0 :(得分:0)

您需要将错误文本传递到TextField

TextField(
  decoration: InputDecoration(
    errorText:"some error",    
  ),

答案 1 :(得分:0)

您必须在setState()中更改_validate属性,并根据该更改TextField错误文本

class FirstScreen extends StatefulWidget {
  @override
  _FirstScreenState createState() => _FirstScreenState();
}

class _FirstScreenState extends State<FirstScreen> {
  final passwordConfirmController = TextEditingController();
  bool _validate = false;

  void _validatePassword() {
    if (passwordConfirmController.text.isEmpty) {
      _validate = true;
    } else {
      _validate = false;
    }
    setState(() {
      _validate;
      print("Valor _validate después");
      print(_validate);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Column(
        children: <Widget>[
          Card(
            color: Color(0xFFDBE2EF),
            margin: EdgeInsets.symmetric(vertical: 10, horizontal: 25),
            child: TextField(
              controller: passwordConfirmController,
              obscureText: true,
              decoration: InputDecoration(
                  errorText: _validate ? "Enter Password" : null,
                  border: OutlineInputBorder(),
                  labelText: 'Confirmar contraseña',
                  labelStyle: TextStyle(fontFamily: 'Montserrat')),
            ),
          ),
          SizedBox(
            height: 20,
          ),
          RaisedButton(
              color: Color(0xFFDBE2EF),
              onPressed: () => {_validatePassword()},
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(5),
              ),
              child: Text(
                'Actualizar',
                style: TextStyle(
                    fontSize: 20,
                    fontFamily: 'Montserrat',
                    color: Color(0xFF002D53),
                    fontWeight: FontWeight.bold),
              )),
        ],
      ),
    );
  }
}