flutter - Flutter中调用方法的不同方式
问题描述
我想知道有什么区别:
onPressed: method
onPressed: method()
onPressed: () => method()
在FlutteronPressed
中的 a 参数中。RaisedButton
解决方案
onPressed: myCall
当 onPressed 属性动作发生时,这将触发方法 myCall。onPressed: myCall()
这是方法的调用onPressed: ()=>mycall()
这将创建一个匿名函数,该函数将方法 mycall()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, );
推荐阅读
- python-3.x - 在 matplotlib boxplot 中使用自定义边界创建调整后的箱线图
- plugins - Microsoft Dynamics CRM 插件 - 通过托管解决方案停用或禁用插件 SDK 消息处理步骤
- rest - 当未提供或不匹配时,适用于额外身份验证步骤的 http 状态代码
- php - 如果启用了 Doctrine 的缓存 (Redis),则查询数更高 (x2)
- java - PackProtocolException:将 jgitflow-maven-plugin 与詹金斯作业一起使用时广告无效
- spring-boot - Kafka批处理侦听器不正确的反序列化消息
- sql - 如果另一列为 NULL,则将 NULL 值替换为未设置
- phpmailer - 如何在没有附件的情况下发送嵌入图像?
- javascript - 移动导航将英雄部分向下推
- excel - VBA 脚本似乎并没有在第一次尝试时一直运行