Search code examples
flutterlayoutwidgetborderflutter-layout

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. What is the problem and how to solve it?


Solution

  • 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,
                        )),
                  )
                ],
              ),
            ),