简体   繁体   English

如何在 Flutter 中的行小部件内的容器上添加边框?

[英]How to add border on a container inside a row widget in Flutter?

        Container(
    //            decoration: BoxDecoration(
    //              border: Border.all(color: Colors.black45),
    //              borderRadius: BorderRadius.circular(8.0),
    //            ),
                child: Row(
                  children: <Widget>[
                    Container(
                      child: Text("hi"),
                      margin : EdgeInsets.fromLTRB(20, 8, 8, 16),
                      width: MediaQuery.of(context).size.width *0.42,
                      height: 90,
                      color: Colors.black12,
                    ),

                    Container(
                      child: Text("Hi"),
                      margin: EdgeInsets.fromLTRB(16, 8, 8, 16),
                      width: MediaQuery.of(context).size.width * 0.42 ,
                      height: 90,
                      color: Colors.black12,
                    )
                  ],
                ),
              ),

I can add border using Box decoration on the outer container, but it's throwing me an error when I am trying to do the same on the inner containers.我可以在外部容器上使用 Box 装饰添加边框,但是当我尝试对内部容器执行相同操作时,它会抛出一个错误。 What is the problem and how to solve it?问题是什么以及如何解决?

In order to add border on container inside row widget , we have to use decoration for the inner containers.为了在行小部件内的容器上添加边框,我们必须对内部容器使用装饰。 Once you will post the error, we can answer you better but i think the below code will be helpful for you.一旦您发布错误,我们可以更好地回答您,但我认为以下代码将对您有所帮助。 If you are using decoration then you must not add colour attribute in container directly, it should be in decoration only.如果你使用的是装饰,那么你不能直接在容器中添加颜色属性,它应该只是在装饰中。

     Container(
          child: Row(
            children: <Widget>[
              Container(
                child: Text("hi"),
                margin: EdgeInsets.fromLTRB(20, 8, 8, 16),
                width: MediaQuery.of(context).size.width * 0.42,
                height: 90,
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.all(Radius.circular(4)),
                    shape: BoxShape.rectangle,
                    border: Border.all(
                      color: Colors.blue,
                      width: 4,
                    )),
              ),
              Container(
                child: Text("Hi"),
                margin: EdgeInsets.fromLTRB(16, 8, 8, 16),
                width: MediaQuery.of(context).size.width * 0.42,
                height: 90,
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.all(Radius.circular(4)),
                    shape: BoxShape.rectangle,
                    border: Border.all(
                      color: Colors.blue,
                      width: 4,
                    )),
              )
            ],
          ),
        ),

In container widgets you cannot use the color and decoration at the same time.在容器小部件中,您不能同时使用colordecoration Remove the color property from the Container and move it into the BoxDecoration widgetContainer移除color属性并将其移动到BoxDecoration小部件中

This should work:这应该有效:

Container(
  child: Row(
    children: <Widget>[
      Container(
        decoration: BoxDecoration(
          border: Border.all(color: Colors.black45),
          borderRadius: BorderRadius.circular(8.0),
          color: Colors.black12,  //add it here
        ),
        child: Text("hi"),
        margin : EdgeInsets.fromLTRB(20, 8, 8, 16),
        width: MediaQuery.of(context).size.width *0.42,
        height: 90,
        //color: Colors.black12,    //must be removed
      ),

      Container(
        decoration: BoxDecoration(
          border: Border.all(color: Colors.black45),
          borderRadius: BorderRadius.circular(8.0),
          color: Colors.black12,  //add it here
        ),
        child: Text("Hi"),
        margin: EdgeInsets.fromLTRB(16, 8, 8, 16),
        width: MediaQuery.of(context).size.width * 0.42 ,
        height: 90,
        //color: Colors.black12,      // must be removed
      )
    ],
  ),
),

Consider the humble Divider widget to keep things simple.考虑使用简陋的 Divider 小部件来使事情变得简单。 If you add it to the bottom of a Column in your row it will add a line that will act as a border.如果将它添加到行中列的底部,它将添加一条线作为边框。

const Divider(height: 1.0,),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM