颤振如何将容器背景设置为透明颜色

iPa*_*tel 4 android-alertdialog dart flutter

我发现了这个问题,但对我不起作用。

我还玩的装饰Opacity装饰颜色Container。但是没有找到解决方法当我将其设置为透明时,它始终显示白色背景色。

看下面的图像,它应该是透明的而不是红色。

在此处输入图片说明

下面是我的代码:

_showAlertDialog(String strTitle, String strDetails) {
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            contentPadding: EdgeInsets.zero,
            content: Stack(
              children: <Widget>[
                Opacity(
                  opacity: 1, // Also tried to set 0
                  child: Container(
                    padding: EdgeInsets.all(8.0),
                    decoration: BoxDecoration(
                      color: Color.fromRGBO(255, 0, 0, 0.5) // I played with different colors code for get transparency of color but Alway display White. 
                    ),
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: <Widget>[
                        Container(
                          height: 50,
                          padding: EdgeInsets.only(left: 10, right: 10, top: 2),
                          color: Theme.of(context).primaryColor,
                          child: Center(
                            child: Text(
                              strTitle,
                              style: TextStyle(
                                  color: Colors.white,
                                  fontWeight: FontWeight.w500,
                                  fontSize: 14),
                              maxLines: 2,
                            ),
                          ),
                        ),
                        Flexible(
                          child: Container(
                            color: Colors.white,
                            padding: EdgeInsets.all(10),
                            child: SingleChildScrollView(
                              child: Text(
                                strDetails,
                                style: TextStyle(color: Colors.black87, fontSize: 12, fontWeight: FontWeight.w400),
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
                Positioned(
                    top: 0,
                    right: 0,
                    child:
                    Container(
                      height: 24,
                      width: 24,
                      child: DecoratedBox(
                        child: IconButton(
                            padding: EdgeInsets.zero,
                            icon: Icon(Icons.close, color: Theme.of(context).primaryColor, size: 18,), onPressed: () {
                          Navigator.of(context).pop();
                        }),
                        decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.circular(12)
                        ),
                      ),
                    )
                )
              ],
            ),
          );
        });
  }
}
Run Code Online (Sandbox Code Playgroud)

Lig*_*axx 11

你也可以做如下

backgroundColor: Color.fromRGBO(r, g, b, 0)
Run Code Online (Sandbox Code Playgroud)


Bas*_*yak 7

您可以通过以下方式轻松实现这一目标:

...
   color: Colors.red.withOpacity(0),
                            ...
Run Code Online (Sandbox Code Playgroud)

您可以使用从 0 到 1 的十进制值来指定所需的不透明度,0 表示完全透明,而 1 表示完全不透明。


die*_*per 5

AlertDialog部件有一个backgroundColor属性,只需将其设置为透明。

  AlertDialog(
              contentPadding: EdgeInsets.zero,
              backgroundColor: Colors.transparent,
Run Code Online (Sandbox Code Playgroud)

并删除 BoxDecoration

“更新 外观” backgroundColor在Flutter 1.0.0上尚不可用。(我在开发频道上)

稳定的:https : //github.com/flutter/flutter/blob/stable/packages/flutter/lib/src/material/dialog.dart

开发人员:https//github.com/flutter/flutter/blob/dev/packages/flutter/lib/src/material/dialog.dart

检查Dialog的源代码,我发现它使用的是dialogBackgroundColorfrom主题。试试这个简单的方法:

  showDialog(
          context: context,
          builder: (BuildContext context) {
            return Theme(
              data: Theme.of(context).copyWith(dialogBackgroundColor: Colors.transparent),
              child: AlertDialog(
                contentPadding: EdgeInsets.zero,
                content: Stack(
                  children: <Widget>[
                    Container(
                      margin: EdgeInsets.all(8.0),
                      color: Colors.white,
                      ...
Run Code Online (Sandbox Code Playgroud)