rahul  Kushwaha
rahul Kushwaha

Reputation: 2819

How to represent the lack of widget in flutter?

I have a container where I want to display border with the condition, If the condition is true then display the border else do not display.

something like this:-

bool condition1;
bool condition2;

content: Container(
    decoration: BoxDecoration(
      border: Border(
        top: condition1 ? BorderSide(
          color: Colors.grey[300],
          style: BorderStyle.solid,
          width: 1,
        ) : null,
        bottom: condition1 ? BorderSide(
          color: Colors.grey[300],
          style: BorderStyle.solid,
          width: 1,
        ) : null,
      ),
    ),
   child: Container(),
   ),

but i cant pass the null, its giving error.

another way I could do is to make width 0.0, but the border does not go completely. so I want to remove the border widget.

so, if we cant use null then how to represent the lack of widget in a flutter.

Upvotes: 1

Views: 60

Answers (1)

Midhun MP
Midhun MP

Reputation: 107191

You can use BorderSide.none instead of null.

Container(
   decoration: BoxDecoration(
      border: Border(
         top: condition1 ? BorderSide(
            color: Colors.grey[300],
            style: BorderStyle.solid,
            width: 1,
          ) : BorderSide.none,
         bottom: condition1 ? BorderSide(
            color: Colors.grey[300],
            style: BorderStyle.solid,
            width: 1,
         ) : BorderSide.none,
       ),
    ),
   child: Container(),
),

Refer BorderSide.none for more details.

Upvotes: 1

Related Questions