簡體   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