如何在Flutter中创建自定义对话框

4

我希望能够创建一个自定义对话框,如下所示。我能够创建具有两个按钮(积极和消极按钮)的普通对话框。但是,我搜索了很多有关创建自定义对话框的信息,但无果。

enter image description here

showAlertDialog(BuildContext context) {

    // set up the buttons
    Widget cancelButton = FlatButton(
      child: Text("Cancel"),
      onPressed:  () {},
    );
    Widget continueButton = FlatButton(
      child: Text("Continue"),
      onPressed:  () {},
    );

    // set up the AlertDialog
    AlertDialog alert = AlertDialog(
      title: Text("Action"),
      content: Text("Would you like to continue learning how to use Flutter alerts?"),
      actions: [
        cancelButton,
        continueButton,
      ],
    );

    // show the dialog
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return alert;
      },
    );
  }

现在我希望将这些按钮和图像作为对话框的子项,并在底部放置图标按钮“X”以关闭对话框。感谢任何帮助。我是Flutter的完全新手。

2
请查看https://dev59.com/t1QJ5IYBdhLWcg3w05ZM。 - Yauhen Sampir
1个回答

7

关于it,我们创建一个自定义对话框

1. 自定义对话框内容类

class CustomDialog extends StatelessWidget {

  dialogContent(BuildContext context) {
    return Container(
      decoration: new BoxDecoration(
        color: Colors.white,
        shape: BoxShape.rectangle,
        borderRadius: BorderRadius.circular(10),
        boxShadow: [
          BoxShadow(
            color: Colors.black26,
            blurRadius: 10.0,
            offset: const Offset(0.0, 10.0),
          ),
        ],
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min, // To make the card compact
        children: <Widget>[
          Image.asset('assets/images/image.jpg', height: 100),
          Text(
            "Text 1",
            style: TextStyle(
              fontSize: 24.0,
              fontWeight: FontWeight.w700,
            ),
          ),
          SizedBox(height: 16.0),
          Text(
            "Text 1",
            style: TextStyle(
              fontSize: 24.0,
              fontWeight: FontWeight.w700,
            ),
          ),
          SizedBox(height: 24.0),
          Align(
            alignment: Alignment.bottomCenter,
            child: Icon(Icons.cancel),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Dialog(
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(10),
      ),
      elevation: 0.0,
      backgroundColor: Colors.transparent,
      child: dialogContent(context),
    );
  }
}

2. 点击时调用自定义对话框:

RaisedButton(
            color: Colors.redAccent,
            textColor: Colors.white,
            onPressed: () {
              showDialog(
                  context: context,
                  builder: (BuildContext context) {
                    return CustomDialog();
                  });
              ;
            },
            child: Text("PressMe"),
          ),

2
@S M Vaidhyanathan:如果这个答案对您有用,您能否接受它?这样其他人就可以参考它了。 - Jitesh Mohite

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接