如何关闭颤动的“获取库”对话框?

How to dismiss flutter Get library dialog?

我正在显示与 Get 图书馆的对话,我想在几秒钟后与 Future 自动关闭。但是我没有找到合适的功能。那么如何在显示 get 对话后关闭它呢?

代码:

Get.dialog(Center(
    child: Material(
      color: Colors.blue,
      child: Text('Hello'),
    ),
  )); // how to dismiss? Like Get.Dismiss().

我没有用过Get,但如果你真的想做那件事,那我可以建议我的做法。

因此,我们将使用 Flutter AlerDialog Class,它的工作原理相同,即弹出对话框,您可以随时编辑内容。

现在让我们做这些事情:

  • 创建对话框
  • 点击按钮时弹出
  • 自动关闭对话框

这将帮助您组织解决方案。我们将仅使用 Future

showAlertDialog(BuildContext context) {

  // set up the button
  Widget okButton = FlatButton(
    child: Text("OK"),
    onPressed: () { },
  );

  // set up the AlertDialog
  AlertDialog alert = AlertDialog(
    title: Text("My title"),
    content: Text("This is my message."),
    actions: [
      okButton,
    ],
  );

  // show the dialog
  showDialog(
    context: context,
    builder: (BuildContext context) {
      return alert;
    },
  );
}

该方法会弹出对话框,然后自动关闭,对话框:

void _ourAutoDismissDialog(BuildContext context){
    
   //Calling out showdialog method
   showAlertDialog(context);

   //Auto dismissing after the 2 seconds
   // You can set the time as per your requirements in Duration
   // This will dismiss the dialog automatically after the time you 
   // have mentioned
   Future.delayed(const Duration(seconds: 2), (){
      Navigator.of(context).pop();
   });
}
FlatButton(
  onPressed: () => _ourAutoDismissDialog(context)
  child: Text('Hello')
)

要关闭对话框,我们需要进行返回操作,我们通过Navigator.of(context).pop()

来完成

这是我们执行上述操作后得到的结果:

关闭对话框尝试使用:

Get.back();

navigator.pop();