I try to create some custom widgets with some parameters in the constructor. This widget has some optional and required parameters.
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; } }
Curly brackets {} are used to specify optional, named parameters in Dart.
A parameter wrapped by { } is a named optional parameter. Also, it is necessary for you to use the name of the parameter if you want to pass your argument.
Steps to Pass Data to Stateful Widget in Flutter To pass data to stateful widget, first of all, create two pages. Now from the first page open the second page and pass the data. Inside the second page, access the variable using the widget. For example widget.
Optional parameters can be either positional or named, but not both.
Named parameters are optional by default so you don't have to assign the default value.
If a parameter is optional but can’t be null, provide a default value.
class TextInputWithIcon extends StatefulWidget { final String iconPath; final String placeHolder; final Function(bool)? onFocusChange; // nullable and optional const TextInputWithIcon( {Key? key, required this.iconPath, // non-nullable and required this.placeHolder = "", // non-nullable but optional with a default value this.onFocusChange, // nullable and optional }) : super(key: key); @override _TextInputWithIconState createState() => _TextInputWithIconState(); }
const TextInputWithIcon( {Key key, @required this.iconPath, this.placeHolder = "", this.onFocusChange }) : super(key: key);
void _focusChanged(bool value) { // using null-aware operator (for both with and without null safety) onFocusChange?.call(value); // or without null-aware operator // with null safety if(onFocusChange != null) { onFocusChange!(value); } // without null safety if(onFocusChange != null) { onFocusChange(value); } }
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:
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 a 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With