Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
430 views
in Technique[技术] by (71.8m points)

flutter - Trouble accessing attribute from Stateful Widget

class PasswordTextField extends StatefulWidget {
  final String hintText;
  final IconData icon;
  final TextEditingController controller;
  final FormFieldValidator<String> validator;
  PasswordTextField({
    Key key,
    this.hintText ,
    this.icon = Icons.lock,
    this.validator,
    this.controller
  }) : super(key: key);
  @override
    _PasswordTextFieldState createState() => _PasswordTextFieldState();
  }
  class _PasswordTextFieldState extends State<PasswordTextField> {

    var _passwordVisible;
    var _iconColor;
    @override
    void initState() {
      _passwordVisible = true;
      _iconColor = Colors.grey;
    }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return TextFieldContainer(
      child: TextFormField(
        controller: controller, //here
        validator: validator, //here
        obscureText: _passwordVisible,
        cursorColor: kPrimaryightColor,
        decoration: InputDecoration(
          labelText: "Your Password",
          labelStyle: TextStyle(color: kPrimaryColor) ,
          hintText: hintText, /ere
          border: InputBorder.none,
          icon: Icon(
            icon, //here
            color: kPrimaryColor,
          ),
          suffixIcon: IconButton(
            icon: Icon(Icons.remove_red_eye),
          color: _iconColor,
          onPressed: (){
            setState(() {
              if(_passwordVisible == true){
                _passwordVisible = false;
                _iconColor = kPrimaryColor;
              }else{
                _passwordVisible = true;
                _iconColor = Colors.grey;
              }
            });
          },
        ),
      ),
    ),
    );
  }
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    throw UnimplementedError();
  }
}

I want to access some attribute from my stateful widget, but I don't know how, it work like this in stateless, is there any another way to use this on another file?

I use it like this PasswordTextField( validator:(value){ if(value.isEmpty){ return "Please insert something";
}else if(value.length < 6){ return "Enter Correct Password Format (6 character)"; }else{ return null; }

question from:https://stackoverflow.com/questions/65650644/trouble-accessing-attribute-from-stateful-widget

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can access the fields declared in your Stateful widget class in the state class by using the predefined field widget i.e. widget.controller. So your code will look like this:

  @override
  Widget build(BuildContext context) {
    return TextFieldContainer(
      child: TextFormField(
        controller: widget.controller, // here
        validator: widget.validator, // here
        obscureText: _passwordVisible,
        cursorColor: kPrimaryightColor,
        decoration: InputDecoration(
          labelText: "Your Password",
          labelStyle: TextStyle(color: kPrimaryColor) ,
          hintText: widget.hintText, // here
          border: InputBorder.none,
          icon: Icon(
            widget.icon, // here
            color: kPrimaryColor,
          ),
          // Define other controls
        ),
      ),
    );
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...