简体   繁体   English

如何将 function 作为小部件中的参数传递?

[英]How to pass a function as a parameter in widget?

I created a widget and I want to pass a function to this widget, but when I try to do this I get this error.我创建了一个小部件,我想将 function 传递给这个小部件,但是当我尝试这样做时,我得到了这个错误。 在此处输入图像描述

Moreover when this function is passed to a widget it is automatically used, because it debugPrint "MONEY CHANGED".此外,当将此 function 传递给小部件时,它会自动使用,因为它调试打印“MONEY CHANGED”。

This is the function code:这是 function 代码:

class Functions {
  changeMoney(int money) {
    Boxes.getCharacter().get(0)!.money =
        Boxes.getCharacter().get(0)!.money + money;
    debugPrint("MONEY CHANGED");
  }
}

And this is widget's code:这是小部件的代码:

class DialogButton extends StatefulWidget {
  const DialogButton(
      {Key? key,
      required this.answer,
      required this.function,
      required this.possible})
      : super(key: key);
  final String answer;
  final Function function;
  final bool possible;

  @override
  State<DialogButton> createState() => _DialogButtonState();
}

class _DialogButtonState extends State<DialogButton> {
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 6),
      child: ElevatedButton(
          onPressed: () => {
                Navigator.of(context, rootNavigator: true).pop(),
                widget.function
              },
          child: Padding(
            padding: const EdgeInsets.symmetric(vertical: 18.0),
            child: Row(children: [
              Expanded(child: Container()),
              Text(
                widget.answer,
                style: rajdhaniStyle(weight: FontWeight.w600, size: 18),
              ),
              Expanded(child: Container()),
            ]),
          )),
    );
  }
}

So the question is how should i correctly pass a function inside widget without automatically make it turn on the function and without getting an error?所以问题是我应该如何在小部件内部正确传递 function 而不会自动打开 function 并且不会出错?

If the function have parameter, then the received widget (here is DialogButton ) also need to declare parameter type.如果 function 有参数,那么接收到的小部件(这里是DialogButton )也需要声明参数类型。 Example, the function is: void doSomething(String hehe, int yolo) {} .例如,function 是: void doSomething(String hehe, int yolo) {} Then your DialogButton should be declare as Function(String, int) onTap .然后你的DialogButton应该被声明为Function(String, int) onTap

More details, in first screen, you can call DialogButton like this:更多详细信息,在第一个屏幕中,您可以像这样调用DialogButton

class Screen1 extends StatelessWidget {
  @override
  build(...) {
    // do something
      DialogButton(
        answer: "your answer",
        function: (double money) {},
        possible: true,
      )
  }
}

Otherwise, if you want to split/separate the function (not write-in-line like above), you could do as follow:否则,如果您想拆分/分离 function(不是像上面那样直接写入),您可以执行以下操作:

class Screen1 extends StatelessWidget {
  @override
  build(...) {
    // do something
      DialogButton(
        answer: "your answer",
        function: myFunction,
        possible: true,
      )
  }
  
  myFunction(double money) {}
}

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

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