簡體   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