I am trying to create a reusable widget. But if I am OK to set up all the parameters, I do not want to make all of them mandatory. I just want to use 4 or 5 parameters. The other ones will be optional. I have done some research and try to add [] on the optional parameter, but I am getting an error.
ERROR : The parameter 'parametersValidate' can't have a value of 'null' because of its type, but the implicit default value is 'null'.
If you could help me fixing this it would be appreciated.
class TextFormFieldWidget extends StatefulWidget {
String hintText;
Widget prefixIcon;
String defaultText;
TextInputType textInputType;
FocusNode focusNode;
bool obscureText;
TextEditingController controller;
Function functionValidate;
String parametersValidate;
TextInputAction actionKeyboard;
Function onSubmitField;
Function onFieldTap;
TextFormFieldWidget(
this.hintText,
this.textInputType,
this.controller,
[this.parametersValidate],
this.onSubmitField,
this.onFieldTap,
this.defaultText,
{Key? key,
required this.actionKeyboard,
required this.prefixIcon,
required this.focusNode,
required this.obscureText,
required this.functionValidate,
}
) : super(key: key);
@override
_TextFormFieldWidgetState createState() => _TextFormFieldWidgetState();
}
class _TextFormFieldWidgetState extends State<TextFormFieldWidget> {
double bottomPaddingToError = 12;
@override
Widget build(BuildContext context) {
return Theme(
data: Theme.of(context).copyWith(
primaryColor: Colors.black,
),
child: TextFormField(
cursorColor: Colors.black,
// obscureText: widget.obscureText,
keyboardType: widget.textInputType,
textInputAction: widget.actionKeyboard,
// focusNode: widget.focusNode,
style: const TextStyle(
color: Colors.black,
fontSize: 16.0,
fontWeight: FontWeight.w200,
fontStyle: FontStyle.normal,
letterSpacing: 1.2,
),
initialValue: widget.defaultText,
decoration: InputDecoration(
prefixIcon: widget.prefixIcon,
hintText: widget.hintText,
enabledBorder: const OutlineInputBorder(
borderSide: BorderSide(color: Colors.black),
),
focusedBorder: const OutlineInputBorder(
borderSide: BorderSide(color: Colors.black),
),
hintStyle: const TextStyle(
color: Colors.grey,
fontSize: 14.0,
fontWeight: FontWeight.w300,
fontStyle: FontStyle.normal,
letterSpacing: 1.2,
),
contentPadding: EdgeInsets.only(
top: 12, bottom: bottomPaddingToError, left: 8.0, right: 8.0),
isDense: true,
errorStyle: const TextStyle(
color: Colors.red,
fontSize: 12.0,
fontWeight: FontWeight.w300,
fontStyle: FontStyle.normal,
letterSpacing: 1.2,
),
errorBorder: const OutlineInputBorder(
borderSide: BorderSide(color: Colors.black),
),
focusedErrorBorder: const OutlineInputBorder(
borderSide: BorderSide(color: Colors.black),
),
),
controller: widget.controller,
validator: (value) {
if (widget.functionValidate != null) {
String resultValidate =
widget.functionValidate(value, widget.parametersValidate);
if (resultValidate != null) {
return resultValidate;
}
}
return null;
},
onFieldSubmitted: (value) {
if (widget.onSubmitField != null) widget.onSubmitField();
},
onTap: () {
if (widget.onFieldTap != null) widget.onFieldTap();
},
),
);
}
}
String? commonValidation(String value, String messageError) {
var required = requiredValidator(value, messageError);
if (required != null) {
return required;
}
return null;
}
String? requiredValidator(value, messageError) {
if (value.isEmpty) {
return messageError;
}
return null;
}
void changeFocus(
BuildContext context, FocusNode currentFocus, FocusNode nextFocus) {
currentFocus.unfocus();
FocusScope.of(context).requestFocus(nextFocus);
}
CodePudding user response:
Try the following code:
class TextFormFieldWidget extends StatefulWidget {
String hintText;
Widget? prefixIcon;
String? defaultText;
TextInputType? textInputType;
FocusNode? focusNode;
bool? obscureText;
TextEditingController controller;
Function? functionValidate;
String? parametersValidate;
TextInputAction? actionKeyboard;
Function onSubmitField;
Function? onFieldTap;
TextFormFieldWidget({
Key? key,
required this.hintText,
this.textInputType,
required this.controller,
this.parametersValidate,
required this.onSubmitField,
this.onFieldTap,
this.defaultText,
this.actionKeyboard,
this.prefixIcon,
this.focusNode,
this.obscureText,
this.functionValidate,
}) : super(key: key);
@override
_TextFormFieldWidgetState createState() => _TextFormFieldWidgetState();
}
class _TextFormFieldWidgetState extends State<TextFormFieldWidget> {
double bottomPaddingToError = 12;
@override
Widget build(BuildContext context) {
return Theme(
data: Theme.of(context).copyWith(
primaryColor: Colors.black,
),
child: TextFormField(
cursorColor: Colors.black,
// obscureText: widget.obscureText,
keyboardType: widget.textInputType,
textInputAction: widget.actionKeyboard,
// focusNode: widget.focusNode,
style: const TextStyle(
color: Colors.black,
fontSize: 16.0,
fontWeight: FontWeight.w200,
fontStyle: FontStyle.normal,
letterSpacing: 1.2,
),
initialValue: widget.defaultText,
decoration: InputDecoration(
prefixIcon: widget.prefixIcon,
hintText: widget.hintText,
enabledBorder: const OutlineInputBorder(
borderSide: BorderSide(color: Colors.black),
),
focusedBorder: const OutlineInputBorder(
borderSide: BorderSide(color: Colors.black),
),
hintStyle: const TextStyle(
color: Colors.grey,
fontSize: 14.0,
fontWeight: FontWeight.w300,
fontStyle: FontStyle.normal,
letterSpacing: 1.2,
),
contentPadding: EdgeInsets.only(
top: 12, bottom: bottomPaddingToError, left: 8.0, right: 8.0),
isDense: true,
errorStyle: const TextStyle(
color: Colors.red,
fontSize: 12.0,
fontWeight: FontWeight.w300,
fontStyle: FontStyle.normal,
letterSpacing: 1.2,
),
errorBorder: const OutlineInputBorder(
borderSide: BorderSide(color: Colors.black),
),
focusedErrorBorder: const OutlineInputBorder(
borderSide: BorderSide(color: Colors.black),
),
),
controller: widget.controller,
validator: (value) {
if (widget.functionValidate != null) {
String resultValidate =
widget.functionValidate(value, widget.parametersValidate);
if (resultValidate != null) {
return resultValidate;
}
}
return null;
},
onFieldSubmitted: (value) {
if (widget.onSubmitField != null) widget.onSubmitField();
},
onTap: () {
if (widget.onFieldTap != null) widget.onFieldTap();
},
),
);
}
}
String? commonValidation(String value, String messageError) {
var required = requiredValidator(value, messageError);
if (required != null) {
return required;
}
return null;
}
String? requiredValidator(value, messageError) {
if (value.isEmpty) {
return messageError;
}
return null;
}
void changeFocus(
BuildContext context, FocusNode currentFocus, FocusNode nextFocus) {
currentFocus.unfocus();
FocusScope.of(context).requestFocus(nextFocus);
}