如何在Flutter中为对话框添加圆形边框?

4
如何在Flutter中为对话框添加圆形边框?我尝试了下面的代码,但无法得到所需的输出。我已经添加了圆形边框,但它不起作用。我需要对话框的圆形边框,请参考预期输出以获取详细信息,谢谢指导。
我的代码:
class CustomDialog extends StatelessWidget {                                                          
  @override
  Widget build(BuildContext context) {                                                                  
    const double padding = 1.0;                                                                                
    return Dialog(
        backgroundColor: Colors.green,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(8.0),
        ),
        child: Container(
            decoration: BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(20.0))),
            child: Column(mainAxisSize: MainAxisSize.min, children: [
              Container(
                margin: EdgeInsets.all(1),
                width: double.infinity,
                child: Text('title',
                    style: TextStyle(fontSize: 30, color: Colors.white)),
                color: Colors.green,
              ),
              Container(
                color: Colors.white,
                padding: EdgeInsets.all(10),
                child: ListView(
                  shrinkWrap: true,
                  children: [
                    Container(
                        margin: EdgeInsets.only(left: 10, bottom: 10),
                        height: 30,
                        child: Text('one',
                            style: TextStyle(
                              fontSize: 20,
                            ))),
                    Container(
                        margin: EdgeInsets.only(left: 10, bottom: 10),
                        height: 30,
                        child: Text('one',
                            style: TextStyle(
                              fontSize: 20,
                            ))),
                    Container(
                        margin: EdgeInsets.only(left: 10, bottom: 10),
                        height: 30,
                        child: Text('one',
                            style: TextStyle(
                              fontSize: 20,
                            ))),
                  ],
                ),
              ),
              Divider(
                color: Colors.white,
              ),
              Container(
                  color: Colors.white,
                  height: 50,
                  padding: EdgeInsets.all(5),
                  alignment: Alignment.centerRight,
                  child: Text(
                    'CANCEL',
                    style: TextStyle(fontSize: 20),
                  )),
            ])));
  }
}

我的期望:expected output

当前输出:my output

3个回答

6
问题出在你使用的 容器 上,你可以给每个容器添加特定的边框半径来解决问题。
我添加了一个演示和代码,以获取你想要的输出效果:
class CustomDialog extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Dialog(
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(15.0),
      ),
      child: Container(
        height: 340,
        child: Column(
          children: [
            Container(
              height: 60,
              width: double.infinity,
              padding: EdgeInsets.all(
                15.0,
              ),
              decoration: BoxDecoration(
                color: Colors.green,
                borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(
                    15.0,
                  ),
                  topRight: Radius.circular(
                    15.0,
                  ),
                ),
              ),
              child: Text(
                'Baby Names',
                style: TextStyle(
                  fontSize: 20,
                  color: Colors.white,
                ),
              ),
            ),
            ...List.generate(
              5,
              (index) => Padding(
                padding: const EdgeInsets.all(10.0),
                child: Align(
                  alignment: Alignment.centerLeft,
                  child: Text(
                    'List Names',
                    style: TextStyle(
                      fontSize: 18,
                    ),
                  ),
                ),
              ),
            ),
            Divider(
              color: Colors.grey[200],
              thickness: 1.5,
            ),
            Padding(
              padding: const EdgeInsets.all(10.0),
              child: Align(
                alignment: Alignment.centerRight,
                child: Text(
                  'CANCEL',
                  style: TextStyle(
                    fontSize: 18,
                    color: Colors.green,
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

结果:

在这里输入图片描述


如果这个答案解决了你的问题,请接受它。@Tester12 - void

5

只需要将 ClipBehavior 添加到 Dialog 中即可。

import 'package:flutter/material.dart';

class CustomDialog extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    const double padding = 1.0;
    return Dialog(
      backgroundColor: Colors.green,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(20.0),
      ),
      clipBehavior: Clip.antiAlias, //  add clipBehavior 
      child: Column(mainAxisSize: MainAxisSize.min, children: [
        Container(
          margin: EdgeInsets.all(1),
          width: double.infinity,
          child: Text('title',
              style: TextStyle(fontSize: 30, color: Colors.white)),
          color: Colors.green,
        ),
        Container(
          color: Colors.white,
          padding: EdgeInsets.all(10),
          child: ListView(
            shrinkWrap: true,
            children: [
              Container(
                  margin: EdgeInsets.only(left: 10, bottom: 10),
                  height: 30,
                  child: Text('one',
                      style: TextStyle(
                        fontSize: 20,
                      ))),
              Container(
                  margin: EdgeInsets.only(left: 10, bottom: 10),
                  height: 30,
                  child: Text('one',
                      style: TextStyle(
                        fontSize: 20,
                      ))),
              Container(
                  margin: EdgeInsets.only(left: 10, bottom: 10),
                  height: 30,
                  child: Text('one',
                      style: TextStyle(
                        fontSize: 20,
                      ))),
            ],
          ),
        ),
        Divider(
          color: Colors.white,
        ),
        Container(
            color: Colors.white,
            height: 50,
            padding: EdgeInsets.all(5),
            alignment: Alignment.centerRight,
            child: Text(
              'CANCEL',
              style: TextStyle(fontSize: 20),
            )),
      ]),
    );
  }
}

enter image description here


0

你添加了RoundedRectangleBorder(), import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      home: MyWidget(),
    ),
  );
}

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
                                   
    return Dialog(
        backgroundColor: Colors.green,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(8.0),
        ),
        child: Container(
  padding: EdgeInsets.only(
    top: 10.0,
    bottom: 5,
    left: 5,
    right: 5,
  ),
  margin: EdgeInsets.only(top: 5),
  decoration: new BoxDecoration(
    color: Colors.white,
    shape: BoxShape.rectangle,
    borderRadius: BorderRadius.circular(5),
    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>[
      Text(
        "Baby",
        style: TextStyle(
          fontSize: 24.0,
          fontWeight: FontWeight.w700,
        ),
       
      ),
      Divider(color: Colors.grey,),
      SizedBox(height: 16.0),
      Text(
        "text",
        textAlign: TextAlign.center,
        style: TextStyle(
          fontSize: 16.0,
        ),
      ),
      SizedBox(height: 24.0),
      Align(
        alignment: Alignment.bottomRight,
        child: FlatButton(
          onPressed: () {
            Navigator.of(context).pop(); // To close the dialog
          },
          child: Text("buttonText"),
        ),
      ),
    ],
  ),
),
    );
  }
}

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