Home > database >  Flutter widget that doesn't occupy any space on screen
Flutter widget that doesn't occupy any space on screen

Time:01-19

In flutter I want to use a widget in my application that if the condition is not satisfied a container is printed (or any other widget) that contain no space on the screen. Actually I want a widget that covers no space on screen in flutter.

CodePudding user response:

Use SizedBox.shrink().

For example:

SomeWidget(
  child: boolShow ? YourWidget() : SizedBox.shrink(),
)

CodePudding user response:

simply use Container() or SizedBox(). Don't give any height, width or child.

CodePudding user response:

There are 2 ways to do this.

Firstly you can show an empty SizedBox with height 0 in case the condition is met. In case the condition is not met show the desired widget. This will look like below:

child: if_condition_is_met ? Container(child: ...) : const SizedBox(),

If you are applying the condition in a column, you can try the below code.

Column(
  children: [
    if(the_condition_is_not_met)
      Container(child: ---- your widgets ---),
  ]
)

You can also use dialog box to show the message

showDialog(
    context: context,
    builder: (context) => AlertDialog(
      shape: RoundedRectangleBorder(
        borderRadius: const BorderRadius.all(
          Radius.circular(20),
        ),
        side: BorderSide(
          color: Colors.black,
          width: 3,
        ),
      ),
      title: Text(
        'give_a_title',
      ),
      content: Text(
        'some_text',
      ),
      actions: [
        SizedBox(
          child: TextButton(
            onPressed: (){},
            child: Text(
              'Ok',
            ),
          ),
        )
      ]
    ),
  );
  •  Tags:  
  • Related