Created
July 24, 2021 12:25
-
-
Save Frankdroid7/86c44e92e429eb954b1581405fb8b549 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
class CustomTextField extends StatefulWidget { | |
String errorText; | |
String labelText; | |
EdgeInsets margin; | |
bool isPasswordField; | |
Function(String value) onSave; | |
Function validator; | |
CustomTextField({ | |
this.onSave, | |
this.labelText, | |
this.validator, | |
this.errorText, | |
this.isPasswordField = false, | |
this.margin = const EdgeInsets.symmetric( | |
vertical: 8.0, | |
), | |
}); | |
@override | |
_CustomTextFieldState createState() => _CustomTextFieldState(); | |
} | |
class _CustomTextFieldState extends State<CustomTextField> { | |
bool obscureText = true; | |
@override | |
Widget build(BuildContext context) { | |
return Padding( | |
padding: widget.margin, | |
child: TextFormField( | |
onSaved: widget.onSave, | |
obscureText: widget.isPasswordField ? obscureText : false, | |
decoration: InputDecoration( | |
labelText: widget.labelText, | |
border: OutlineInputBorder( | |
borderSide: BorderSide( | |
width: 2, | |
color: Colors.grey, | |
), | |
), | |
suffixIcon: widget.isPasswordField | |
? IconButton( | |
icon: obscureText | |
? Icon(Icons.visibility_off) | |
: Icon(Icons.visibility), | |
onPressed: () { | |
setState(() { | |
obscureText = !obscureText; | |
}); | |
}, | |
) | |
: SizedBox.shrink(), | |
), | |
validator: (value) { | |
return widget.validator(value) ? null : widget.errorText; | |
}, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment