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 paramete
You can use a default value that does nothing:
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 = _dummyOnFocusChange})
: assert(onFocusChange != null), super(key: key);
@override
_TextInputWithIconState createState() => _TextInputWithIconState();
static dynamic _dummyOnFocusChange(bool val) {}
}
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 when null
is passed explicitly.