How to change background color of CupertinoAlertDialog?

时光毁灭记忆、已成空白 提交于 2021-01-02 07:49:40

问题


I want create a CupertinoAlertDialog with dark background.

And I try to use Theme widget to solve this problem, but it doesn't work.

Some code here:

showDialog() {
    showCupertinoDialog(
        context: context,
        builder: (context) {
          return Theme(
            data: ThemeData(
                dialogBackgroundColor: Colors.black,
                dialogTheme: DialogTheme(backgroundColor: Colors.black)),
            child: CupertinoAlertDialog(
            title: Text('Title'),
            content: Text('Some message here'),
            actions: <Widget>[
               FlatButton(
                 onPressed: () {
                   Navigator.of(context).pop();
                 },
                 child: Text('OK'),
               ),
             ],
           ),
         );
       },
     );
  }


回答1:


Instead of using Colors.black, use ThemeData.dark()

showDialog() {
    showCupertinoDialog(
        context: context,
        builder: (context) {
          return Theme(
            data: ThemeData.dark(),
            child: CupertinoAlertDialog(
            title: Text('Title'),
            content: Text('Some message here'),
            actions: <Widget>[
               FlatButton(
                 onPressed: () {
                   Navigator.of(context).pop();
                 },
                 child: Text('OK'),
               ),
             ],
           ),
         );
       },
     );
  }



回答2:


The background color is hardcoded:

https://github.com/flutter/flutter/blob/20e59316b8b8474554b38493b8ca888794b0234a/packages/flutter/lib/src/cupertino/dialog.dart#L198

But you can create your own widget instead of default one.



来源:https://stackoverflow.com/questions/57676581/how-to-change-background-color-of-cupertinoalertdialog

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!