Flutter-将函数传递给StatelessWidget不起作用 [英] Flutter - Passing Function to StatelessWidget not working

查看:62
本文介绍了Flutter-将函数传递给StatelessWidget不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个通用的ConfirmWidget,但是它不起作用.这是我的代码:我的想法是在调用自定义窗口小部件时传递任何函数".

I'm trying to make a Generic ConfirmWidget, but it's not working. This is my code: My idea is to pass an "Any Function" when my custom widget is invoked.

class ConfirmWidget extends StatelessWidget {
  final BuildContext context;
  final String mensaje;
  final Function btnSi;

  ConfirmWidget({this.context, this.mensaje, this.btnSi});

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new AlertDialog(
      title: new Text(mensaje),
      actions: <Widget>[
        new FlatButton(
          onPressed: () => Navigator.of(context).pop(false),
          child: new Text("No"),
        ),
        new FlatButton(
          onPressed: () {
            btnSi == null ? Navigator.of(context).pop(true):btnSi;
          },
          child: new Text("Si"),
        ),
      ],
    );
  }
}

我的小部件正在使用:

Future<bool> confirmCancelar(BuildContext context) {
    return showDialog(
      barrierDismissible: false,
      context: context,
      builder: (_) => new ConfirmWidget(
        mensaje: "¿Está seguro de cancelar la operación?",
        context: context,
        btnSi:()=>Navigator.pop(context),
      ),
    );
  }

有什么建议吗?预先感谢.

Any suggestions? thanks in advance.

推荐答案

由于您没有将其直接绑定到onPressed事件,因此需要调用该函数.试试这个:

Since you are not binding it directly to the onPressed event, you need to call the function. Try this:

    new FlatButton(
      onPressed: () {
        btnSi == null ? Navigator.of(context).pop(true):btnSi();
      },
      child: new Text("Si"),
    )

这篇关于Flutter-将函数传递给StatelessWidget不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆