dart - Flutter如何将容器背景设置为透明色

标签 dart flutter android-alertdialog

I found this question but doesn't work for me.

我还使用 Opacity 小部件和 Containerdecoration 颜色。但是没找到解决办法 我设置透明的时候总是显示white背景色。

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

enter image description here

下面是我的代码:

_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)
                        ),
                      ),
                    )
                )
              ],
            ),
          );
        });
  }
}

最佳答案

AlertDialog Widget 有一个 backgroundColor 属性,只需将其设置为透明即可。

  AlertDialog(
              contentPadding: EdgeInsets.zero,
              backgroundColor: Colors.transparent,

并删除 BoxDecoration

更新 看起来 backgroundColor 在 Flutter 1.0.0 上尚不可用。 (我在开发 channel )

稳定: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 的源代码,我发现它使用了主题中的 dialogBackgroundColor。试试这个简单的方法:

  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,
                      ...

关于dart - Flutter如何将容器背景设置为透明色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54123097/

相关文章:

android - ListView flutter 中的问题开关值

Flutter - 避免 ListView 重建

flutter - 如何去除ios上的过度滚动?

dart - 使用 Flutter 获取 Firestore 文档 key

android - 当设备的操作栏/状态栏向下滚动时,警报对话框消失

android - onContextItemSelected 没有被调用

android - AlertDialog - DialogInterface onClick 或 ListView onClick 未在项目单击时被调用

android - 我如何将 Flutter (Dart) 连接到我的 Oracle 服务器?有人试过这个吗?

android - 应用程序不在 Release模式下构建

flutter - 如何在 flutter 中制作高度 = 130 和宽度 = 300 的以下形状的容器