Flutter:有没有办法将参数“ID”从页面传递给 Dialog?

问题描述

我在开发我的应用程序时遇到问题,我知道页面导航可以使用“Navigation.of(context)”并传递一个带 ID 的参数,但是我想对页面做同样的事情但找不到任何解决方案.如果有人知道如何制作它,请为我提供一些线索。太感谢了!!漏掉的东西会更新

对话框

还有另一个“自定义对话框”类,用于定义对话框并在用户输入信息后执行 onSaved 操作。保存的每个链接都会自动生成一个 ID,因此想传递“ID”以进行更新,但坚持如何传递“ID”参数。

  Future<dynamic> callDialog(BuildContext context) {
    String label;

    if (appTitle == "Instagram") {
      label = "Please Enter Your Username";
    } else if (appTitle == "Whatsapp") {
      label = "Please Enter Your Phone Number";
    } else if (appTitle == "LinkedIn") {
      label = "Please Enter Your URL";
    } else if (appTitle == "Mail") {
      label = "Please Enter Your E-mail";
    }

    return showDialog(
      context: context,barrierDismissible: false,builder: (BuildContext context) {
        return CustomDialogBox(
          title: appTitle,descriptions: "$label",button1: "Cancel",button2: "Save",img: appImage,);
      },);
  }

导航

在部分导航中已尝试执行类似传递参数“ID”的操作,但它不起作用,那么有没有办法将参数从页面传递到对话框?

Widget build(BuildContext context) {
        return Card(
          child: Hero(
            tag: appTitle,child: InkWell(
              onTap: () {
                callDialog(context,);
              },child: GridTile(
                footer: GridTileBar(
                  backgroundColor: Colors.black54,title: Text(
                    appTitle,textAlign: TextAlign.center,),child: Image.asset(
                  appImage,fit: BoxFit.cover,);
      }

didChangeDependencies 对话框

  @override
  void didChangeDependencies() {
    if (_isInit) {
      final appId = ModalRoute.of(context).settings.arguments as String;
      if (appId != null) {
        //check product exist
        _editedApp = Provider.of<UserProfileLogic>(context,listen: false)
            .findByAppId(appId);
        _initValues = {
          "title": _editedApp.title,"urlLink": _editedApp.appLink,"imageUrl": _editedApp.appImage,};
      }
    }
    _isInit = false;
    super.didChangeDependencies();
  }

解决方法

只需在 calldialogue 函数中将 id 作为参数传递

示例代码:-

Future<dynamic> callDialog(BuildContext context,String id) {
String label;

if (appTitle == "Instagram") {
  label = "Please Enter Your Username";
} else if (appTitle == "Whatsapp") {
  label = "Please Enter Your Phone Number";
} else if (appTitle == "LinkedIn") {
  label = "Please Enter Your URL";
} else if (appTitle == "Mail") {
  label = "Please Enter Your E-mail";
}

return showDialog(
  context: context,barrierDismissible: false,builder: (BuildContext context) {
    return CustomDialogBox(
      title: appTitle,descriptions: "$label",button1: "Cancel",button2: "Save",img: appImage,);
  },);

}

导航代码:-

Widget build(BuildContext context) {
    return Card(
      child: Hero(
        tag: appTitle,child: InkWell(
          onTap: () {
            callDialog(context,id); //pass your id here
          },child: GridTile(
            footer: GridTileBar(
              backgroundColor: Colors.black54,title: Text(
                appTitle,textAlign: TextAlign.center,),child: Image.asset(
              appImage,fit: BoxFit.cover,);
  }

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...