Flutter change dialog background color

六月ゝ 毕业季﹏ 提交于 2021-01-05 04:51:50

问题


I was using dialogBackgroundColor property still the color was not changing. Can anyone tell me how to change the background color of the dialog?


回答1:


You can now use backgroundColor property of AlertDialog to change the color.

AlertDialog(
  backgroundColor: Colors.orange,
  ...
)



回答2:


You need to wrap your Dialog in a Builder like this. After that dialogBackgroundColor will have an effect.

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



回答3:


You can do that without using Builder.

Here is the example.

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



回答4:


This code block work's for me. Here you can change color from this line data:Theme.of(context).copyWith(dialogBackgroundColor: Colors.white)

void openDialog(BuildContext context) {
    showDialog(
      context: context,
      barrierDismissible: true,
      builder: (context) {
        return Theme(
          data:
              Theme.of(context).copyWith(dialogBackgroundColor: Colors.white),
          child: new SimpleDialog(
            title: new Text("Title Here...."),
            children: <Widget>[
              new SimpleDialogOption(
                child: Text('Demo Text One'),
                onPressed: () {
                  Navigator.pop(context);
                },
              ),
              new SimpleDialogOption(
                child: Text('Demo Text Two'),
                onPressed: () {
                  Navigator.pop(context);
                },
              ),
              new SimpleDialogOption(
                child: Text('Close'),
                onPressed: () {
                  Navigator.pop(context);
                },
              ),
            ],
          ),
        );
      },
    );
  }


来源:https://stackoverflow.com/questions/53683429/flutter-change-dialog-background-color

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