首页 > 解决方案 > 如何更改 CupertinoAlertDialog 的背景颜色?

问题描述

我想创建一个深色背景的 CupertinoAlertDialog。

我尝试使用主题小部件来解决这个问题,但它不起作用。

这里有一些代码:

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

在此处输入图像描述

标签: flutterdialog

解决方案


而不是使用Colors.black,使用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'),
               ),
             ],
           ),
         );
       },
     );
  }

推荐阅读