简体   繁体   中英

Make Function parameter optional in custom widget flutter

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;
   }
}

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 .

With null safety

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();

}

Without null safety

const TextInputWithIcon(
      {Key key,
      @required this.iconPath,
      this.placeHolder = "",
      this.onFocusChange
})
      : super(key: key);

Usage:

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);
    }

  }

Dart 2.17 update:

Although it often makes sense to place positional arguments first, named arguments can be placed anywhere in the argument list when it suits your API:

repeat(times: 2, () {
  ...
});

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.

Another option if you don't like named parameters (like me :/) is:

function_name (argument1, [argument2]) {
   // statements
}

arguments in brackets are optional.

source

Optional Argument with default values

To specify the optional argument with default values, we use {} curly braces.

In optional positional argument and optional named argument, if we don't specify the value in an argument then it is set to NULL.

function_name (argument1, {argument2 = default_value}) {
  // statements
}

The syntax for calling the function

// if you want to override new value
function_name(argumentName : value); 

Sample

ShowMyDetails(String name,
 {String lastName = "Sanket", int age = 20}){
  print(name);
  print(lastName);
  print(age);
}

main() {
  ShowMyDetails("Jay", age: 24);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM