边框和小部件之间的抖动填充

时间:2018-09-06 12:31:36

标签: flutter

我正在尝试使用Flutter创建带有图标和标题的卡片小部件。但是我无法在卡的边框和小部件之间添加一些边距。

这是我的卡代码:

class MyCard extends StatelessWidget{
  MyCard({this.title, this.icon});

  final Widget title;
  final Widget icon;

  @override
  Widget build(BuildContext context) {
  return new Container(
    padding: EdgeInsets.only(bottom: 20.0),
    child: new Card(
      child: new Row(
        children: <Widget>[
          this.icon,
          new Container(
            width: 20.0, //I also don't know if this is legal
          ),
          this.title
        ]
      )
    )
    );
  }
}

这是结果,但是我想在卡内添加更多填充物,以便获得更高的卡,并在右侧添加更多图标。

Result

3 个答案:

答案 0 :(得分:4)

您可以将小部件包装在Padding小部件内的卡片中,也可以使用容器的paddingmargin属性来实现所需的布局。

P.S。我在不同级别添加了填充。根据需要删除或添加更多填充。

代码:

class MyCard extends StatelessWidget{

 MyCard({this.title, this.icon});

  final Widget title;
  final Widget icon;

  @override
  Widget build(BuildContext context) {
    return new Container(
        padding: EdgeInsets.only(bottom: 20.0),
        child: new Card(
            child: Padding(
              padding: EdgeInsets.symmetric(vertical: 2.0),
              child: new Row(
                  children: <Widget>[
                    Padding(
                      padding: EdgeInsets.symmetric(horizontal: 5.0),
                      child: this.icon,
                    ),
                    new SizedBox(
                      width: 20.0,
                    ),
                    Container(
                      padding: EdgeInsets.symmetric(vertical: 0.5, horizontal: 1.0),
                      margin: EdgeInsets.all(2.0),
                      child: this.title,
                    )
                  ]
              ),
            )
        )
    );
  }
}

答案 1 :(得分:0)

<块引用>
Container(
                      padding: const EdgeInsets.all(1),
                      decoration: BoxDecoration(
                        border: Border.all(
                            color: Theme.of(context).accentColor, width: 3),
                        borderRadius: BorderRadius.circular(20),
                      ),
                      child: Container(
                        margin: const EdgeInsets.all(2),
                        padding: const EdgeInsets.all(15),
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(15),
                          color: Theme.of(context).accentColor,
                        ),
                        child: Text('Evcil Hayvanlar',
                            style: Theme.of(context)
                                .textTheme
                                .button
                                .copyWith(color: Colors.white, fontSize: 24)),
                      ))

你也可以试试这个方法

答案 2 :(得分:-1)

class MyCard extends StatelessWidget{
  MyCard({this.title, this.icon});

  final Widget title;
  final Widget icon;

  @override
  Widget build(BuildContext context) {
    return new Container(
        padding: EdgeInsets.only(bottom: 20.0),
        child: new Card(
            child: new Row(
                children: <Widget>[
//                  Change Your Code Here
                  new Padding(padding: new EdgeInsets.only(left: 8.0),child: this.icon,),
                  new Container(
                    width: 20.0, //I also don't know if this is legal
                  ),
                  this.title
                ]
            )
        )
    );
  }
}