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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…