gpt4 book ai didi

flutter - 如何在Flutter中关闭警报对话框?

转载 作者:行者123 更新时间:2023-12-03 03:35:20 24 4
gpt4 key购买 nike

我正在为我的应用程序中的弹出窗口使用警报对话框。当触发onTap时,会调用弹出窗口,但是当我按下“取消”按钮时,弹出窗口不会消失,并且弹出窗口后面的整个屏幕都变黑。
这是我的弹出代码。

import 'package:flutter/material.dart';

class FancyAlertDialog {

static showFancyAlertDialog(
BuildContext context,
String title,
String message,
{
bool dismissable = true,
Icon icon,
@required String labelPositiveButton,
@required String labelNegativeButton,
@required VoidCallback onTapPositiveButton,
@required VoidCallback onTapNegativeButton,
}) {
assert(context != null, 'context is null!!!');
assert(title != null, 'title is null!!!');
assert(message != null, 'message is null!!!');
assert(labelPositiveButton != null, 'labelPositiveButton is null');
assert(labelNegativeButton != null, 'labelNegativeButton is null');
assert(onTapPositiveButton != null, 'onTapPositiveButton is null');
assert(onTapNegativeButton != null, 'onTapNegativeButton is null');
return showDialog(
context: context,
barrierDismissible: dismissable,
child: Dialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(4.0),
),
),
child: Wrap(
children: <Widget>[
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(4.0),
topRight: Radius.circular(4.0),
),
color:Colors.red,
),
padding: EdgeInsets.symmetric(vertical: 5.0),
child: Stack(
children: <Widget>[
Align(
child: icon ?? Container(height:0),
alignment: Alignment.topRight,
)
],
),
),
Padding(
padding: EdgeInsets.only(
left: 16.0,
top: 2.0,
right: 16.0,
bottom: 8.0,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Center(
child: Text(
title,
style: Theme.of(context).textTheme.subtitle,
),
),
SizedBox(height: 8.0),
Text(
message,
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.caption,
),
SizedBox(height: 16.0),
Row(
children: <Widget>[
Expanded(
child: RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(16.0),
),
),
color: Colors.grey,
child: Text(
labelNegativeButton.toUpperCase(),
style: TextStyle(
color: Colors.white,
),
),
onPressed: onTapNegativeButton,
),
),
SizedBox(width: 16.0),
Expanded(
child: RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(16.0),
),
),
color: Colors.red,
child: Text(
labelPositiveButton.toUpperCase(),
style: TextStyle(
color: Colors.white,
),
),
onPressed: onTapPositiveButton,
),
),
],
)
],
),
),
],
),
),
);
}
}

这就是我所说的弹出窗口。
 FancyAlertDialog.showFancyAlertDialog(
context,
'Info Fancy Alert Dialog Box',
'This is a info alert dialog box. This plugin is used to help you easily create fancy dialog',
icon: Icon(
Icons.clear,
color: Colors.black,
),
labelPositiveButton: 'OKAY',
onTapPositiveButton: () {
Navigator.pop(context);
print('tap positive button');
},
labelNegativeButton: 'Cancel',
onTapNegativeButton: () {
Navigator.pop(context);
print('tap negative button');
},
);

这是我按下“取消”按钮时的屏幕外观:
enter image description here

最佳答案

我假设您在调用context时使用了错误的Navigator.pop(context)对象。
那时,导航器尚未意识到该对话框。

首先,在BuildContext中提供一个新的showDialog。有两种方法可以做到这一点:

  • child函数中为Dialog参数(现在为showDialog)创建一个新的小部件。
  • 用提供新child Dialog 包裹Builder(BuildContext)

  • 然后,您应该将该新的 context放入 Navigator.pop(context)调用。同样,有两种方法可以做到这一点:
  • 从对话框内部弹出
  • 将上下文对象作为参数传递给onTapPositiveButtononTapNegativeButton

  • 也可以在这里找到有关 Builder的更多信息: https://www.youtube.com/watch?v=xXNOkIuSYuA

    关于flutter - 如何在Flutter中关闭警报对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61421634/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com