Flutter showDialog,AlertDialog 背景渐变。

4

对于颜色,我可以使用dialogBackgroundColor属性为AlertDialog设置自己想要的背景颜色。

我希望使用Gradient作为我的背景。我该怎么做?需要使用DecoratedBox,但不知道该将什么内容包含在其中。是否有人能给我建议或相关链接?


如果您在AlertDialog中仅使用content而没有title,那么您可以将内容包装在具有渐变背景的Container中。 - Andrii Turkovskyi
我也在使用 Title。所以,在我的情况下这不起作用。现在我可以改变内容并在那里加入标题。但是我还在寻找更好的解决方案。 - user6274128
2个回答

5
您可以添加一个容器,在其中以渐变方式装饰。例如:
class GradientDialog extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new _GradientDialogState();
  }
}

class _GradientDialogState extends State<GradientDialog> {

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      content: Container(
        padding: const EdgeInsets.all(8.0),
        decoration: new BoxDecoration(
            gradient: new LinearGradient(
                colors: AppColors.BG_GRADIENT,
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter)),
        child: YourContentInside(),

      ),
      contentPadding: EdgeInsets.all(0.0),
    );
  }
}

使用以下方法打开

showDialog(
    context: context,
    barrierDismissible: true,
    builder: (BuildContext context) {
      return GradientDialog();
    });

4
AlertDialogbuild方法中,有return Dialog(child: dialogChild, shape: shape);。在Dialog.build()中,它返回Material(color: _getColor(context), ...。没有办法在不进行自定义的情况下为AlertDialog设置渐变背景。
如果需要,我可以提供示例。
或者您可以调用showDialog并发送另一个小部件,而不是AlertDialog

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接