繁体   English   中英

在自定义小部件颤动中使函数参数可选

[英]Make Function parameter optional in custom widget flutter

我尝试在构造函数中创建一些带有一些参数的自定义小部件。 这个小部件有一些可选和必需的参数。

如何在我的Widget中使Function类型参数可选。

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

可选参数可以是位置参数或命名参数,但不能两者兼有。

默认情况下,命名参数是可选的,因此您不必分配默认值。

如果参数是可选的但不能为空,则提供默认值

具有零安全性

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

  }

飞镖 2.17 更新:

尽管首先放置位置参数通常是有意义的,但命名参数可以放置在参数列表中适合您的 API 的任何位置:

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

查看可选参数以更好地理解。

编辑:谢谢乔纳·威廉姆斯的澄清。

您可以使用不执行任何操作的默认值:

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

我创建了一个静态命名函数,而不仅仅是一个闭包作为默认值,因为闭包不是 const 并且当前默认值需要是 const。

我添加了assert(...)以确保在显式传递null时显示错误。

如果您不喜欢命名参数(如我:/),另一种选择是:

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

括号中的参数是可选的。

资源

具有默认值的可选参数

要使用默认值指定可选参数,我们使用{}大括号。

在可选的位置参数和可选的命名参数中,如果我们没有在参数中指定值,那么它被设置为 NULL。

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

调用函数的语法

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

样本

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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