简体   繁体   English

更改 Flutter alertDialog 中操作部分的背景颜色

[英]Change background color for the action section in Flutter alertDialog

I'm new to Flutter and trying to customize theAlertDialog widget of the material dart.我是 Flutter 的新手,并尝试自定义材料dart的 AlertDialog 小部件。

There are ways to set the background color for the whole dialog, is there a way to set the background color only to certain part of the dialog, from the attached picture the background color for the actions section of dialog should be different.有办法为整个对话框设置背景色,有没有办法只为对话框的某些部分设置背景色,从附图看,对话框动作部分的背景色应该是不同的。

动作部分的背景颜色变化

Try below code hope its helpful to you.试试下面的代码希望它对你有帮助。

Your Widget to call alrtDialog您要调用 alrtDialog 的小部件

    TextButton(
            onPressed: () {
              showDataAlert();
            },
            child: Text(
              'Pressed',
            ),
          ),

Your Alert Dialog function您的警报对话框 function

showDataAlert() {
    showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.all(
                Radius.circular(
                  20.0,
                ),
              ),
            ),
            contentPadding: EdgeInsets.only(
              top: 10.0,
            ),
            title: Text(
              "Your Title Here",
              style: TextStyle(fontSize: 24.0),
            ),
            content: Container(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  Text(
                    "Your Contents Here",
                    style: TextStyle(fontSize: 24.0),
                  ),
                  SizedBox(
                    height: 5.0,
                  ),
                  Container(
                      decoration: BoxDecoration(
                        color: Colors.grey.shade500,
                        borderRadius: BorderRadius.only(
                            bottomLeft: Radius.circular(20.0),
                            bottomRight: Radius.circular(20.0)),
                      ),
                      child: Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.end,
                          children: [
                            ElevatedButton(
                              onPressed: () {
                                Navigator.of(context).pop();
                              },
                              style: ElevatedButton.styleFrom(
                                primary: Colors.white,
                              ),
                              child: Text(
                                "Cancel",
                                style: TextStyle(
                                  color: Colors.black,
                                ),
                              ),
                            ),
                            SizedBox(
                              width: 10,
                            ),
                            ElevatedButton(
                              onPressed: () {
                                Navigator.of(context).pop();
                              },
                              style: ElevatedButton.styleFrom(
                                primary: Colors.black,
                              ),
                              child: Text(
                                "Confirm",
                              ),
                            ),
                          ],
                        ),
                      )),
                ],
              ),
            ),
          );
        });
  }

Your Result Screen->您的结果屏幕-> 图片

You can create custom dialog.您可以创建自定义对话框。

like this.像这样。

 Dialog errorDialog = Dialog(
  shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12.0)), //this right here
  child: Container(
    height: 200.0,
    width: 300.0,
   
    child: Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Padding(
          padding:  EdgeInsets.all(15.0),
          child: Text('Cool', style: TextStyle(color: Colors.red),),
        ),
        Padding(
          padding: EdgeInsets.all(15.0),
          child: Text('Awesome', style: TextStyle(color: Colors.red),),
        ),
        Padding(padding: EdgeInsets.only(top: 50.0)),
        TextButton(onPressed: () {
          Navigator.of(context).pop();
        },
            child: Text('Done!', style: TextStyle(color: Colors.purple, fontSize: 18.0),))
      ],
    ),
  ),
);

and show dialog并显示对话框

 showDialog(context: context, builder: (BuildContext context) => errorDialog);}

https://i.stack.imgur.com/Mz3YL.png https://i.stack.imgur.com/Mz3YL.png

    shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12.0)),
    //this right here
    child: Column(
      mainAxisAlignment: MainAxisAlignment.center,
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        Column(
          children: [
            Padding(
              padding: EdgeInsets.all(15.0),
              child: Text(
                'Cool',
                style: TextStyle(color: Colors.red),
              ),
            ),
            Padding(
              padding: EdgeInsets.all(15.0),
              child: Text(
                'Awesome',
                style: TextStyle(color: Colors.red),
              ),
            ),
            Padding(padding: EdgeInsets.only(top: 50.0)),
          ],
        ),
        Container(
          decoration: BoxDecoration(
              color: Colors.red,
              borderRadius: BorderRadius.only(bottomLeft: Radius.circular(12), bottomRight: Radius.circular(12))
          ),
          width: double.maxFinite,
          child: TextButton(
              onPressed: () {},
              child: Text(
                'Done!',
                style: TextStyle(color: Colors.purple, fontSize: 18.0),
              )),
        )
      ],
    ),
  ); ```


[1]: https://i.stack.imgur.com/Mz3YL.png

AlertDialog has backgroundColor parameter and takes Color that will apply to the full background. AlertDialog具有backgroundColor参数并采用将应用于整个背景的Color

title , actions takes widget can be configured the way you want. titleactions采取小部件可以按照您想要的方式进行配置。

AlertDialog(
          backgroundColor: Colors.pink,
          content: Text("Message"),
          buttonPadding: EdgeInsets.all(13),
          actions: [
            ElevatedButton(
              onPressed: () {},
              style: ButtonStyle(
                backgroundColor: MaterialStateProperty.all<Color>(Colors.green),
              ),
              child: Text("Cancel"),
            ),
            ElevatedButton(
              onPressed: () {},
              child: Text("Confirm"),
            ),
          ],
        );

在此处输入图像描述

I'm using ElevatedButton as action button, and you can choose anything and configure it.我使用ElevatedButton作为操作按钮,您可以选择任何内容并对其进行配置。 While everything is widget, you can place the way you want.虽然一切都是小部件,但您可以按照自己的方式放置。 You can also override the themeData.您还可以覆盖主题数据。

More about更多关于

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM