I try to create some custom widget with some parameter in the constructor. This widget has some optional and required parameter. how can make Function
type parameter optional in my Widget
.
class TextInputWithIcon extends StatefulWidget {
final String iconPath;
final String placeHolder;
final Function(bool) onFocusChange;
const TextInputWithIcon(
{Key key,
@required this.iconPath,
this.placeHolder = "",
this.onFocusChange})
: super(key: key);
@override
_TextInputWithIconState createState() => _TextInputWithIconState();
}
class _TextInputWithIconState extends State<TextInputWithIcon> {
@override
Widget build(BuildContext context) {
return MY_WIDGET;
}
}
Named parameters are optional by default so you don't have to assign the default value.
and when calling the
onFocusChange
perform a null check:Have a look at Optional Parameters to understand better.
Edit: Thank you Jonah Williams to clarification.
You can use a default value that does nothing:
I created a static named function instead of just a closure as default value, because closures are not const and currently default values need to be const.
I added the
assert(...)
to ensure that an error is shown whennull
is passed explicitly.