Флаттер, как установить прозрачный цвет фона контейнера - PullRequest
0 голосов
/ 10 января 2019

Я нашел этот вопрос, но у меня не работает.

Я также играю с Opacity виджетом и украшением цвета Container. Но не нашел решения. Всегда отображается белый цвет фона, когда я устанавливаю его прозрачным.

Посмотрите на изображение ниже, вместо красного цвета оно должно быть прозрачным.

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

1 Ответ

0 голосов
/ 10 января 2019

Виджет AlertDialog имеет свойство backgroundColor, просто установите его как прозрачное.

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

И удалить BoxDecoration

Обновление Похоже, что backgroundColor пока не доступен на Flutter 1.0.0. (Я на канале разработчиков)

стабильно: 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

Проверяя исходный код 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,
                      ...
...