首页 > 解决方案 > Flutter中调用方法的不同方式

问题描述

我想知道有什么区别:

onPressed: method

onPressed: method()

onPressed: () => method()

在FlutteronPressed中的 a 参数中。RaisedButton

标签: flutterdart

解决方案


  1. onPressed: myCall当 onPressed 属性动作发生时,这将触发方法 myCall。

  2. onPressed: myCall()这是方法的调用

  3. onPressed: ()=>mycall()这将创建一个匿名函数,该函数将方法 mycall()

  4. RaisedButton 的 onPressed 参数只是一个类似于任何其他对象的字段。该字段不是原始的(即布尔值或整数),而是一个函数。您可能已经注意到,它是 RaisedButton 的必填字段。这意味着,如果不提供 onPressed 属性的值,您就无法创建 RaisedButton 的实例。下面是 RaisedButton 的构造函数的代码片段:

     class RaisedButton extends MaterialButton {
     /// Create a filled button.
     ///
     /// The [elevation], [highlightElevation], [disabledElevation], and
     /// [clipBehavior] arguments must not be null. Additionally,  [elevation],
     /// [highlightElevation], and [disabledElevation] must be non-negative.
     const RaisedButton({
     Key key,
     @required VoidCallback onPressed,
     ValueChanged<bool> onHighlightChanged,
     ButtonTextTheme textTheme,
     Color textColor,
     Color disabledTextColor,
     Color color,
     Color disabledColor,
     Color highlightColor,
     Color splashColor,
     Brightness colorBrightness,
     double elevation,
     double highlightElevation,
     double disabledElevation,
     EdgeInsetsGeometry padding,
     ShapeBorder shape,
     Clip clipBehavior = Clip.none,
     MaterialTapTargetSize materialTapTargetSize,
     Duration animationDuration,
     Widget child,
     }) : assert(elevation == null || elevation >= 0.0),
          assert(highlightElevation == null || highlightElevation >= 0.0),
          assert(disabledElevation == null || disabledElevation >= 0.0),
          super(
                key: key,
                onPressed: onPressed,
                onHighlightChanged: onHighlightChanged,
                textTheme: textTheme,
                textColor: textColor,
                disabledTextColor: disabledTextColor,
                color: color,
                disabledColor: disabledColor,
                highlightColor: highlightColor,
                splashColor: splashColor,
                colorBrightness: colorBrightness,
                elevation: elevation,
                highlightElevation: highlightElevation,
                disabledElevation: disabledElevation,
                padding: padding,
                shape: shape,
                clipBehavior: clipBehavior,
                materialTapTargetSize: materialTapTargetSize,
                animationDuration: animationDuration,
                child: child,
               );
    

推荐阅读