Search code examples
dartflutterandroid-alertdialog

Flutter how to set container background as transparent color


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

I also play with Opacity widget and decoration color of Container. But didn't find solution It always display white background color when I set it transparent.

Look at below image, Instead of red color it should be transparent.

enter image description here

Below are my code:

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

Solution

  • The AlertDialog Widget has a backgroundColor property , just set it to transparent.

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

    And remove the BoxDecoration

    Update Looks like backgroundColor is not available on Flutter 1.0.0 yet. (I'm on dev channel)

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

    dev: https://github.com/flutter/flutter/blob/dev/packages/flutter/lib/src/material/dialog.dart

    Checking the source code of Dialog , I found it was using the dialogBackgroundColor from theme. Try this easy way:

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