Flutter:通知侦听器,如何使onNotification动态化

我是飞镖新手,因此扑扑。我正在这样使用NotificationListener

   String x = a; // or could be x=b,x=c,etc,comes as function parameter 
    return NotificationListener(
          onNotification: onNotificationHandler,child: new ListView.builder(
              scrollDirection: Axis.horizontal,shrinkWrap: true,physics: const BouncingScrollPhysics(),// other codes comes here

问题

我希望根据变量onNotification的值来动态调整x的值 有人可以帮我吗?

topten800 回答:Flutter:通知侦听器,如何使onNotification动态化

在Dart中,Function是一等公民,这意味着可以从方法中返回函数。

然后,您可以创建一个以x作为参数并返回一个NotificationListenerCallback函数的方法。返回的函数称为Closure,这意味着即使在外部执行,也可以访问其词法范围内的变量(在这种情况下为x)。

在您的示例中,它可以是:

    String x = a; // or could be x=b,x=c,etc,comes as function parameter 
    return NotificationListener(
      onNotification: _notificationHandler(x),// The return the appropriate function with x scoped to the function to be used later
      child: new ListView.builder(
      // other codes comes here

def _notificationHandler(String value) => (T notification) {
  // Note that the returned function has access to `value`
  // even if it will be executed elsewhere
  return (value == 'Hey); 
}
本文链接:https://www.f2er.com/3129040.html

大家都在问