Search code examples
flutteruser-interfaceflutter-container

How to hide a part of the container widget beyond the screen without violating Flutter's layout constraints


I am new to flutter. And i am trying to hide the bottom red part (see the first screenshot) of the blue container under the screen, in order to hide the buttom part of a white border and use this container for the animation. But it generates a problem (see see the second screenshot). Please help)

First screenshot: enter image description here

Second screenshot: enter image description here

This is the code of this Stack with two containers. I used the second one just for demonstration purposes. I need only one container.

...
 Stack(
              children: [
                
                Container(
                  alignment: Alignment.bottomCenter,
                  color: Colors.blue, 
                  height: 300, 
                ),
                Positioned(
                  bottom: 0,
                  left: 0,
                  right: 0,
                  child: ClipRect(
                    child: Align(
                      alignment: Alignment.bottomCenter,
                      heightFactor:
                          1, 
                      child: Container(
                        decoration: BoxDecoration(
                          color: Colors.red,
                          border: Border.all(
                              color: Color.fromARGB(255, 255, 255, 255)),
                        ),
                        height:
                            150,

                        
                        child: Center(
                          child: Text(
                            'Bottom Hidden Container',
                            style: TextStyle(fontSize: 24),
                          ),
                        ),
                      ),
                    ),
                  ),
                ),
              ],
            ),

...

I have tried to remove this problem, but nothing seems to work. enter image description here


Solution

  • You can try OverflowBox widget allows its child to overflow its bounds without causing layout errors. Here's code:

     Stack(
      children: [
        Container(
          alignment: Alignment.bottomCenter,
          color: Colors.blue,
          height: 300,
        ),
        Positioned(
          bottom: 0,
          left: 0,
          right: 0,
          child: OverflowBox(
            maxHeight: 300, // Set the maximum height to control how much is shown
            alignment: Alignment.bottomCenter,
            child: Container(
              decoration: BoxDecoration(
                color: Colors.red,
                border: Border.all(color: Color.fromARGB(255, 255, 255, 255)),
              ),
              height: 150,
              child: Center(
                child: Text(
                  'Bottom Hidden Container',
                  style: TextStyle(fontSize: 24),
                ),
              ),
            ),
          ),
        ),
      ],
    )