简体   繁体   English

将 function 作为参数传递给小部件

[英]Passing function as parameter to a widget

I have a button that calls a widget to open a custom dialog:我有一个按钮,它调用一个小部件来打开一个自定义对话框:

child: GestureDetector(
         onTap: () {
           showDialog(
             context: context,
             builder: (context){ 
                return MovMapAlert(
titulo: "yasiguiendo".tr(),
texto: "dejarsiguiendo".tr(),
aceptar: "dejardeseguir".tr(),
cancelar: "cancelar".tr(),
funcion: SeguidoresCrud().dejarDeSeguir(documentIdSeguidores),);
 });
},
 child: Image.asset('assets/profile/boton_follow_rojo.png'),
  )
)

As you may see,I am posting some parameters to the custom dialog widget, including a function that should be executed when the user taps on a dialog button.如您所见,我将一些参数发布到自定义对话框小部件,包括当用户点击对话框按钮时应执行的 function。

Here you have the custom dialog widget:这里有自定义对话框小部件:

class MovMapAlert extends StatelessWidget {

  final String titulo;
  final String texto;
  final String aceptar;
  final String cancelar;
  final Function funcion;

  const MovMapAlert({Key key, this.titulo, this.texto, this.aceptar, this.cancelar, this.funcion}) : super(key: key);
  @override
  Widget build(BuildContext context) {


    return Dialog(
      shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(16)
      ),
      elevation: 0,
      backgroundColor: Colors.transparent,
      child: _buildChild(context),
    );
  }

  _buildChild(BuildContext context) => Container(
    height: 350,
    decoration: BoxDecoration(
        color: Colors.redAccent,
        shape: BoxShape.rectangle,
        borderRadius: BorderRadius.all(Radius.circular(12))
    ),
    child: Column(
      children: <Widget>[
        Container(
          child: Padding(
            padding: const EdgeInsets.all(12.0),
            child: Image.asset('assets/images/movmap_transparente1024.png', height: 120, width: 120,),
          ),
          width: double.infinity,
          decoration: BoxDecoration(
              color: Colors.white,
              shape: BoxShape.rectangle,
              borderRadius: BorderRadius.only(topLeft: Radius.circular(12), topRight: Radius.circular(12))
          ),
        ),
        SizedBox(height: 24,),
        Padding(
          padding: const EdgeInsets.all(8.0),
          child: Text(this.titulo, style: TextStyle(fontSize: 20, color: Colors.white, fontWeight: FontWeight.bold),),
        ),
        SizedBox(height: 8,),
        Padding(
          padding: const EdgeInsets.only(right: 16, left: 16),
          child: Text(this.texto, style: TextStyle(fontSize: 18,color: Colors.white), textAlign: TextAlign.center,),
        ),
        SizedBox(height: 24,),
        Row(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            FlatButton(onPressed: (){
              Navigator.of(context).pop();
            }, child: Text(this.cancelar),textColor: Colors.white,),
            SizedBox(width: 8,),
            RaisedButton(onPressed: (){
              return Navigator.of(context).pop(true);
              this.funcion();
            }, child: Text(this.aceptar), color: Colors.white, textColor: Colors.redAccent,)
          ],
        )
      ],
    ),
  );
}

My issue is that the line of code that should include the function to be posted to the dialog widget:我的问题是应该包含要发布到对话框小部件的 function 的代码行:

  funcion: SeguidoresCrud().dejarDeSeguir(documentIdSeguidores),

is marked as warning in the editor, the warning message says: Convert to an if element在编辑器中被标记为警告,警告信息说: Convert to an if element

I guess I am not doing it well, I mean, what I need is to pass a function as parameter to another widget, if possible...我想我做得不好,我的意思是,如果可能的话,我需要将 function 作为参数传递给另一个小部件......

The problem is that you're calling the function, not passing it问题是您正在调用function,而不是通过

Change to:改成:

funcion: () => SeguidoresCrud().dejarDeSeguir(documentIdSeguidores),

This happens because when you pass the function using () , you're actually calling the function and passing it's return value as an argument not the function itself to be called elsewhere发生这种情况是因为当您使用()传递 function 时,您实际上是在调用 function 并将其返回值作为参数传递,而不是在其他地方调用 ZC1C425268E68385D1AB5074C17A94F1

Pass function with () invoking operator使用 () 调用运算符传递 function

funcion: (){
   SeguidoresCrud().dejarDeSeguir(documentIdSeguidores);
},

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

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