Search code examples
flutterstatefulwidget

changing state of widget is not reflected, using flutter


password field has an "obscure" ability which is found in the buildInputForm defined below to hide and show the password string if clicked, the action is not reflected on the screen, i reverted back to older version of the code yet it did not change. im using VScode

    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 5),
      child: TextFormField(
        obscureText: isPassword,
        controller: controller,
        decoration: InputDecoration(
          hintText: hint,
          hintStyle: const TextStyle(color: kTextFieldColor),
          focusedBorder: const UnderlineInputBorder(
            borderSide: BorderSide(color: kPrimaryColor),
          ),
          suffixIcon: isPassword
              ? IconButton(
                  onPressed: () {
                    setState(() {
                      _isObscure = !_isObscure;
                    });
                  },
                  icon: _isObscure
                      ? const Icon(
                          Icons.visibility_off,
                          color: kTextFieldColor,
                        )
                      : const Icon(
                          Icons.visibility,
                          color: kPrimaryColor,
                        ),
                )
              : null,
        ),
        validator: validator,
        onSaved: onSaved,
        keyboardType: keyboardType,
        inputFormatters: inputFormatters,
      ),
    );
  }

Solution

  • Your code has logical bug.

    Use this on buildInputForm

     obscureText: isPassword ? _isObscure : false,