Dart 颤振更改对话框背景色

Dart 颤振更改对话框背景色,dart,flutter,Dart,Flutter,我使用的是dialogBackgroundColor属性,但颜色没有改变。谁能告诉我如何更改对话框的背景色吗?您需要像这样将对话框包装在生成器中。之后,对话框backgroundColor将产生效果 Theme( data: ThemeData(dialogBackgroundColor: Colors.orange), child: Builder( builder: (context) { return RaisedButton( onPresse

我使用的是
dialogBackgroundColor
属性,但颜色没有改变。谁能告诉我如何更改对话框的背景色吗?

您需要像这样将
对话框
包装在
生成器中。之后,
对话框backgroundColor
将产生效果

Theme(
  data: ThemeData(dialogBackgroundColor: Colors.orange),
  child: Builder(
    builder: (context) {
      return RaisedButton(
        onPressed: () {
          showDialog(
            context: context,
            builder: (context) {
              return AlertDialog(
                title: Text("Dialog title"),
              );
            },
          );
        },
        child: Text("Show dialog"),
      );
    },
  ),
)

无需使用
Builder
即可完成此操作

这是一个例子

@override
Widget build(BuildContext context) {
  return RaisedButton(
    onPressed: () {
      showDialog(
        context: context,
        builder: (context) {
          return Theme(
            data: Theme.of(context).copyWith(dialogBackgroundColor: Colors.orange),
            child: AlertDialog(
              title: Text("Dialog Title"),
            ),
          );
        },
      );
    },
    child: Text("Show dialog"),
  );
}

现在可以使用
AlertDialog
backgroundColor
属性更改颜色

AlertDialog(
  backgroundColor: Colors.orange,
  ...
)
    showDialog(
   context: context,
   builder: (BuildContext context) {
       return AlertDialog(
         shape: RoundedRectangleBorder(
         borderRadius: BorderRadius.all(Radius.circular(20.0))),
         backgroundColor: Colors.green,
   content: Container(...)
),
}),
这个代码块是给我的。 在这里你可以从这条线改变颜色 数据:Theme.of(context).copyWith(dialogBackgroundColor:Colors.white)

void openDialog(BuildContext){
显示对话框(
上下文:上下文,
是的,
生成器:(上下文){
返回主题(
数据:
Theme.of(context).copyWith(dialogBackgroundColor:Colors.white),
孩子:新SimpleDialog(
标题:新文本(“此处标题…”),
儿童:[
新的SimpleDialogOption(
子:文本(“演示文本一”),
已按下:(){
Navigator.pop(上下文);
},
),
新的SimpleDialogOption(
child:Text('Demo Text Two'),
已按下:(){
Navigator.pop(上下文);
},
),
新的SimpleDialogOption(
子项:文本('Close'),
已按下:(){
Navigator.pop(上下文);
},
),
],
),
);
},
);
}

您现在可以使用AlertDialog的backgroundColor属性更改颜色

AlertDialog(
  backgroundColor: Colors.orange,
  ...
)
    showDialog(
   context: context,
   builder: (BuildContext context) {
       return AlertDialog(
         shape: RoundedRectangleBorder(
         borderRadius: BorderRadius.all(Radius.circular(20.0))),
         backgroundColor: Colors.green,
   content: Container(...)
),
}),
showDialog(
上下文:上下文,
barrierDismissible:false,//用户必须点击按钮!
生成器:(BuildContext上下文){
返回警报对话框(
标题:文本(“你确定吗?”),
内容:SingleChildScrollView(
子:列表体(
儿童:[
文本(“将通过另一个问题”),
],
),
),
行动:[
文本按钮(
子项:文本('Yes'),
已按下:(){
设置状态(){
如果(sayac>0&&sayac
副作用是,它会将RaisedButton的主题更改为Hi,我已经在中介绍过了。添加有用的文本,而不仅仅是代码,只添加最少量的代码,使其真正有用。