【问题标题】:How to add circular border to dialog in flutter?如何在颤动中为对话框添加圆形边框?
【发布时间】:2020-08-26 08:35:08
【问题描述】:

如何在颤动中为对话框添加圆形边框?,我尝试了以下代码但无法获得所需的输出,我已经添加了圆形边框但它不起作用,我需要对话框的圆形边框,详情参考预期输出,请指导

我的代码: `

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

`

我的期望:

当前输出:

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    问题在于您用于包装其他小部件的Container,您可以为每个容器添加特定的边框半径来修复。

    我添加了一个演示和代码来获得您希望输出的样子:

    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
    【解决方案2】:

    只需在对话框中添加ClipBehavior即可。

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

    【讨论】:

      【解决方案3】:

      您添加了 RoundedRectangleBorder(), 导入'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"),
              ),
            ),
          ],
        ),
      ),
          );
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2011-05-19
        • 1970-01-01
        • 2014-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-04
        • 2019-04-08
        • 1970-01-01
        相关资源
        最近更新 更多