简体   繁体   English

Flutter-如何在继承的小部件中的已定义函数中使用参数

[英]Flutter - How to use parameters inside defined function inside inherited widget

I have function inside my Inherited widget, I would like to use this function with parameters, but I don't get how that can be done. 我的继承小部件内有函数,我想将此函数与参数一起使用,但是我不知道该怎么做。 for instance the function below works fine to have inside the inherited widget: 例如,下面的函数可以很好地在继承的小部件中使用:

final Function onTap;

However I would like to use something like below: 但是我想使用类似下面的东西:

final Function onTap(String name);

Anyone know if this can be done somehow and in that case how? 有人知道这是否可以通过某种方式完成吗? any help or input appreciated, thanks! 任何帮助或输入表示赞赏,谢谢!

Not sure if that's what you want to do: 不确定这是否是您要执行的操作:

You can use typedef to define a callback interface. 您可以使用typedef定义回调接口。

import 'package:flutter/material.dart';

typedef void NameTapCallback(String name);

class FooWidget extends StatelessWidget {
  final NameTapCallback onTap;

  const FooWidget({Key key, this.onTap}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        RaisedButton(
          onPressed: () {
            onTap('Peter');
          },
          child: Text('Peter'),
        ),
        RaisedButton(
          onPressed: () {
            onTap('Paul');
          },
          child: Text('Paul'),
        )
      ],
    );
  }
}

// Usage:
var myFooWidget = FooWidget(
  onTap: (name) {
    print('$name tapped');
  },
);

You can use 您可以使用

onTap: () => onTap('foo')

instead of 代替

onTap: onTap

if you want to pass additional parameters 如果您想传递其他参数

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

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