繁体   English   中英

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

[英]Passing function as parameter to a widget

我有一个按钮,它调用一个小部件来打开一个自定义对话框:

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'),
  )
)

如您所见,我将一些参数发布到自定义对话框小部件,包括当用户点击对话框按钮时应执行的 function。

这里有自定义对话框小部件:

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,)
          ],
        )
      ],
    ),
  );
}

我的问题是应该包含要发布到对话框小部件的 function 的代码行:

  funcion: SeguidoresCrud().dejarDeSeguir(documentIdSeguidores),

在编辑器中被标记为警告,警告信息说: Convert to an if element

我想我做得不好,我的意思是,如果可能的话,我需要将 function 作为参数传递给另一个小部件......

问题是您正在调用function,而不是通过

改成:

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

发生这种情况是因为当您使用()传递 function 时,您实际上是在调用 function 并将其返回值作为参数传递,而不是在其他地方调用 ZC1C425268E68385D1AB5074C17A94F1

使用 () 调用运算符传递 function

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

暂无
暂无

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

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