【问题标题】:alertDialog won't appear in flutteralertDialog 不会出现在颤动中
【发布时间】:2021-03-02 23:11:40
【问题描述】:

当长按执行_selectionAlert 时,我有一个列表项,其中有两个按钮,一个用于删除,一个用于编辑(参考图片)。删除工作正常,但是当我按下编辑时,它应该执行_editAlert,它应该显示另一个 alertDialog 但是当我点击它时什么也没发生这里有什么问题?
这负责在_selectionAlert中显示警报对话框

TextButton( // refer code bellow
    child: Text('Edit', style: TextStyle(color: Colors.grey)),
        onPressed: (){
            _editAlert();
            Navigator.of(context).pop();
        },
)

当项目被长按时运行下面的代码:

void _selectionAlert(){
      var selItem = AlertDialog(
        title: Text('What you want to do?'),
        content: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            TextButton(       // ignore this it is for deletion this works fine
              child: Text('Delete', style: TextStyle(color: Colors.red)),
              onPressed: (){
                _deleteItem();
                Navigator.of(context).pop();
              },
            ),
            TextButton( 
              child: Text('Edit', style: TextStyle(color: Colors.grey)),
              onPressed: (){
                _editAlert();      // this line should call _editAlert
                Navigator.of(context).pop();
              },
            )
          ],
        ),
      );
      showDialog(
        context: context,
        builder: (BuildContext context){
          return selItem;
      }
    );
  }  

上述代码的表示:

void _editAlert(){   // edit alertDialog
    var editItem = AlertDialog(
      title: Text('Edit'),
      content: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          TextField(
            decoration: InputDecoration(
              hintText: 'Edit Item',
            ),
            controller: addeditem,
          ),
          TextButton(
            child: Text('Done'),
            onPressed: (){
              _editItem();
              Navigator.of(context).pop();
            }
          )
        ],
      ),
    );
    showDialog(
      context: context,
      builder: (BuildContext context){
        return editItem;
      }
    );
  }

PS:编辑按钮是灰色的,该按钮肯定是可点击的,我正在谷歌浏览器上进行测试。
问候

【问题讨论】:

  • 我只是想知道我在单击编辑时弹出 _selectionAlert 但如果它打开并且我按下完成时它会在弹出时返回哪里?我认为必须有一种不同的方式来调用另一个 alertdialog 中的 alertdialog

标签: flutter dart button dialog flutter-web


【解决方案1】:

尝试在您的 _editAlert 方法中添加 return 语句

_editAlert(){   // edit alertDialog
    var editItem = AlertDialog(
      title: Text('Edit'),
      content: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          TextField(
            decoration: InputDecoration(
              hintText: 'Edit Item',
            ),
            controller: addeditem,
          ),
          TextButton(
            child: Text('Done'),
            onPressed: (){
              _editItem();
              Navigator.of(context).pop();
            }
          )
        ],
      ),
    );
    return showDialog(                //like that
      context: context,
      builder: (BuildContext context){
        return editItem;
      }
    );
  }


同样你的方式是对的,你可以使用 poping 关闭上一个弹出窗口。

【讨论】:

  • 我的类型为 void,因此无法返回:(我尝试将返回类型更改为 Future 不起作用,我需要对返回值做什么?
  • 您可以为您的方法使用 'dynamic' 返回类型。或者使用 Future showDialog 但我不确定,您可以简单地使用 'dynamic' 。 (动态_editAlert(){....})
  • 是的,工作!,我可以获取一些资源来阅读更多关于它的信息吗?
  • StatefullBuilder 可以帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-12
  • 2022-01-21
  • 2019-06-27
  • 2020-01-28
  • 2021-03-05
  • 2023-03-26
  • 2020-12-31
相关资源
最近更新 更多