我有一个对话框的图像,并试图设计与下面的图像相同.

enter image description here

我试过了,但与上图不同

代码:

 customDialogBox(BuildContext context) {
    return showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            backgroundColor: Colors.red,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.all(Radius.circular(32.0))),
            contentPadding: EdgeInsets.only(top: 10.0),
            content: Stack(
              children: <Widget>[
                Container(
              width: MediaQuery.of(context).size.width,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.stretch,
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  SizedBox(
                    height: 20.0,
                  ),
                  Center(
                    child: Padding(
                      padding: const EdgeInsets.all(10.0),
                      child: new Text("Sorry please try \n again tomorrow", style:TextStyle(fontSize: 30.0,color: Colors.white)),
                    )//
                  ),

                  SizedBox(
                    height: 20.0,
                    width: 5.0,
                  ),
                  Divider(
                    color: Colors.grey,
                    height: 4.0,
                  ),

                 InkWell(
                    child: Container(
                      padding: EdgeInsets.only(top: 15.0, bottom: 15.0),
                      decoration: BoxDecoration(
                        color:Colors.white,
                        borderRadius: BorderRadius.only(
                        bottomLeft: Radius.circular(32.0),
                        bottomRight: Radius.circular(32.0)),
                      ),
                      child:  Text(
                       "OK",
                        style: TextStyle(color: Colors.blue,fontSize: 25.0),
                        textAlign: TextAlign.center,
                      ),
                    ),
                    onTap:(){
                      Navigator.pop(context);
                    },
                  ),
                ],
              ),
            ),
            Positioned(
              top: 0.0,
              right: 0.0,
              child: FloatingActionButton(
                child: Image.asset("assets/red_cross.png"),
                onPressed: (){
                Navigator.pop(context);
                },
                shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(80)),
                backgroundColor: Colors.white,
                mini: true,
                elevation: 5.0,
              ),
            ),
          ],
        )
      );
    });
  }

以下是我的对话框: enter image description here

推荐答案

试试这个会很好用的.

    import 'package:flutter/material.dart';

    import 'custom_dialog.dart';

    void main() {
    runApp(MyApp());
    }

    class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
        return MaterialApp(
        title: 'Flutter Demo',
        home: HomePage(),
        );
    }
    }

    class HomePage extends StatefulWidget {
    @override
    _HomePageState createState() => _HomePageState();
    }

    class _HomePageState extends State<HomePage> {
    @override
    Widget build(BuildContext context) {
        return Scaffold(
        appBar: AppBar(),
        body: Center(
            child: RaisedButton(
            onPressed: () {
                showDialog(context: context, builder: (BuildContext context) => CustomDialog());
            },
            child: Text('show custom dialog'),
            ),
        ),
        );
    }
    }

对话框小部件:

    import 'package:flutter/material.dart';

    class CustomDialog extends StatelessWidget {

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

    Widget dialogContent(BuildContext context) {
        return Container(
        margin: EdgeInsets.only(left: 0.0,right: 0.0),
        child: Stack(
            children: <Widget>[
            Container(
                padding: EdgeInsets.only(
                top: 18.0,
                ),
                margin: EdgeInsets.only(top: 13.0,right: 8.0),
                decoration: BoxDecoration(
                    color: Colors.red,
                    shape: BoxShape.rectangle,
                    borderRadius: BorderRadius.circular(16.0),
                    boxShadow: <BoxShadow>[
                    BoxShadow(
                        color: Colors.black26,
                        blurRadius: 0.0,
                        offset: Offset(0.0, 0.0),
                    ),
                    ]),
                child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: <Widget>[
                    SizedBox(
                    height: 20.0,
                    ),
                    Center(
                        child: Padding(
                        padding: const EdgeInsets.all(10.0),
                        child: new Text("Sorry please try \n again tomorrow", style:TextStyle(fontSize: 30.0,color: Colors.white)),
                        )//
                    ),
                    SizedBox(height: 24.0),
                    InkWell(
                    child: Container(
                        padding: EdgeInsets.only(top: 15.0,bottom:15.0),
                        decoration: BoxDecoration(
                        color:Colors.white,
                        borderRadius: BorderRadius.only(
                            bottomLeft: Radius.circular(16.0),
                            bottomRight: Radius.circular(16.0)),
                        ),
                        child:  Text(
                        "OK",
                        style: TextStyle(color: Colors.blue,fontSize: 25.0),
                        textAlign: TextAlign.center,
                        ),
                    ),
                    onTap:(){
                        Navigator.pop(context);
                    },
                    )
                ],
                ),
            ),
            Positioned(
                right: 0.0,
                child: GestureDetector(
                onTap: (){
                    Navigator.of(context).pop();
                },
                child: Align(
                    alignment: Alignment.topRight,
                    child: CircleAvatar(
                    radius: 14.0,
                    backgroundColor: Colors.white,
                    child: Icon(Icons.close, color: Colors.red),
                    ),
                ),
                ),
            ),
            ],
        ),
        );
    }
    }

enter image description here

Approach 2:

    void showFancyCustomDialog(BuildContext context) {
            Dialog fancyDialog = Dialog(
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(12.0),
            ),
            child: Container(
                decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(20.0),
                ),
                height: 300.0,
                width: 300.0,
                child: Stack(
                children: <Widget>[
                    Container(
                    width: double.infinity,
                    height: 300,
                    decoration: BoxDecoration(
                        color: Colors.grey[100],
                        borderRadius: BorderRadius.circular(12.0),
                    ),
                    ),
                    Container(
                    width: double.infinity,
                    height: 50,
                    alignment: Alignment.bottomCenter,
                    decoration: BoxDecoration(
                        color: Colors.red,
                        borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(12),
                        topRight: Radius.circular(12),
                        ),
                    ),
                    child: Align(
                        alignment: Alignment.center,
                        child: Text(
                        "Dialog Title!",
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: 20,
                            fontWeight: FontWeight.w600),
                        ),
                    ),
                    ),
                    Align(
                    alignment: Alignment.bottomCenter,
                    child: InkWell(
                        onTap: () {
                        Navigator.pop(context);
                        },
                        child: Container(
                        width: double.infinity,
                        height: 50,
                        decoration: BoxDecoration(
                            color: Colors.blue[300],
                            borderRadius: BorderRadius.only(
                            bottomLeft: Radius.circular(12),
                            bottomRight: Radius.circular(12),
                            ),
                        ),
                        child: Align(
                            alignment: Alignment.center,
                            child: Text(
                            "Okay let's go!",
                            style: TextStyle(
                                color: Colors.white,
                                fontSize: 20,
                                fontWeight: FontWeight.w600),
                            ),
                        ),
                        ),
                    ),
                    ),
                    Align(
                    // These values are based on trial & error method
                    alignment: Alignment(1.05, -1.05),
                    child: InkWell(
                        onTap: () {
                        Navigator.pop(context);
                        },
                        child: Container(
                        decoration: BoxDecoration(
                            color: Colors.grey[200],
                            borderRadius: BorderRadius.circular(12),
                        ),
                        child: Icon(
                            Icons.close,
                            color: Colors.black,
                        ),
                        ),
                    ),
                    ),
                ],
                ),
            ),
            );
    showDialog(
        context: context, builder: (BuildContext context) => fancyDialog);
}

enter image description here

Flutter相关问答推荐

Flutter bloc -如何使用BlocBuilder?

Flutter Go路由深度链接不按预期工作

从Flutter应用程序解释蓝牙BLE数据:了解Sylvania压力值

如何在应用程序启动时解析和加载JSON文件?

如何在扩展其他小部件时访问Ref

如何将Xpriter代码页更改为代码页27?

参数类型';List<;Dynamic&>不能分配给参数类型';List<;SingleChildWidget&>';

如何画三角形的圆角?

请求已在 flutter 中发送到 django 服务器,并且已返回响应,但条件仍然为 false

在我使用 flutter_riverpod stateprovider 重新保存代码之前,UI 主题状态不会更新

我在手机上找不到我的 Flutter 应用缓存数据

为什么 ref.watch(otherProvider.stream) 在 Riverpod 2.x 中被弃用并且将在 3.x 中被删除?

java.lang.IncompatibleClassChangeError:找到接口 com.google.android.gms.location.SettingsClient,

Android Electric eel Mac M1:运行 flutter doctor -v 时无法找到Bundle 的 java 版本

如何更改中心的圆形进度指示器

如何在 Flutter 中滚动时为多个小部件设置动画

如何使用 Rrect 或类似工具在 flutter 中绘制梯形线?

在 Flutter 中,如何使用文本字段和方形图像进行行布局

在自动完成中 Select 值后保持键盘焦点

Firebase Firestore 查询