簡體   English   中英

Flutter 更改對話框背景顏色

[英]Flutter change dialog background color

我使用dialogBackgroundColor屬性仍然顏色沒有改變。 誰能告訴我如何更改對話框的背景顏色?

您現在可以使用AlertDialog backgroundColor屬性來更改顏色。

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

您可以在不使用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"),
  );
}

您需要像這樣將Dialog包裝在Builder 之后, dialogBackgroundColor將產生效果。

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

這個代碼塊對我有用。 在這里,您可以從此行數據更改顏色: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);
                },
              ),
            ],
          ),
        );
      },
    );
  }

您現在可以使用 AlertDialog 的 backgroundColor 屬性來更改顏色。

    showDialog(
   context: context,
   builder: (BuildContext context) {
       return AlertDialog(
         shape: RoundedRectangleBorder(
         borderRadius: BorderRadius.all(Radius.circular(20.0))),
         backgroundColor: Colors.green,
   content: Container(...)
),
}),
showDialog(
                      context: context,
                      barrierDismissible: false, // user must tap button!
                      builder: (BuildContext context) {
                        return AlertDialog(
                          title: Text('Are you sure?'),
                          content: SingleChildScrollView(
                            child: ListBody(
                              children: <Widget>[
                                Text("Another question will be passed"),
                              ],
                            ),
                          ),
                          actions: <Widget>[
                            TextButton(
                              child: Text('Yes'),
                              onPressed: () {
                                setState(() {
                                  if (sayac > 0 && sayac < nqSize - 1) {
                                    sayac++;
                                    _character=SingingCharacter.qsN;
                                  }
                                });
                                Navigator.of(context).pop();
                              },
                            ),
                            TextButton(
                              child: Text('No'),
                              onPressed: () {
                                Navigator.of(context).pop();
                              },
                            ),
                          ],
                         // elevation: 20.0,
                          backgroundColor: Colors.redAccent,
                            shape: new RoundedRectangleBorder(
                              borderRadius: new BorderRadius.circular(25.0),
                            ),
                        );
                      },
                    );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM