如何更改CupertinoAlertDialog的背景颜色?

问题描述 投票:0回答:4

我想创建一个带有深色背景的 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'),
               ),
             ],
           ),
         );
       },
     );
  }

enter image description here

flutter dialog
4个回答
9
投票

不要使用

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

3
投票

背景颜色是硬编码的:

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

enter image description here

但是您可以创建自己的小部件而不是默认小部件。


0
投票

对于您的深色模式

CupertinoAlertDialog
,可以执行以下操作:

bool isAppInDarkMode(BuildContext context) {
  final brightness =
      WidgetsBinding.instance.platformDispatcher.platformBrightness;
  return brightness == Brightness.dark;
}

 showDialog() {
    showCupertinoDialog(
        context: context,
        builder: (context) {
          final brightness = isAppInDarkMode(context) ? Brightness.dark : Brightness.light;

          return CupertinoTheme(
            data: CupertinoThemeData(
              brightness: brightness,
            ),
            child: CupertinoAlertDialog(
              title: Text(title),
              content: Text(subtitle),
              actions: actionsWidgets,
            ),
          );
       },
     );
  }

-1
投票

也许你可以使用adaptive_Dialog代替它 https://pub.dev/packages/adaptive_dialog

© www.soinside.com 2019 - 2024. All rights reserved.