【问题标题】:How to create a custom Dialog in flutter如何在颤振中创建自定义对话框
【发布时间】:2020-11-01 01:11:44
【问题描述】:

我想创建一个自定义对话框,如下所示。我可以创建一个带有两个按钮(正按钮和负按钮)的普通对话框。但是我搜索了很多关于创建如下所示的自定义对话框但徒劳无功。

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 初学者。

【问题讨论】:

标签: flutter dialog


【解决方案1】:

为此,我们创建了一个自定义对话框

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

【讨论】:

  • @SM Vaidhyanathan:如果它适合你,你能接受答案吗?以便其他人可以参考。
猜你喜欢
  • 2020-01-27
  • 2020-02-18
  • 1970-01-01
  • 1970-01-01
  • 2021-02-13
  • 2020-09-28
  • 2020-02-27
  • 1970-01-01
  • 2017-01-21
相关资源
最近更新 更多