如何在不违反Flutter的布局约束的情况下隐藏容器部件的一部分超出屏幕。

huangapple go评论80阅读模式
英文:

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

问题

以下是您要翻译的部分:

我是新手学习Flutter。我试图隐藏蓝色容器的底部红色部分(请看第一张截图),以便将白色边框的底部部分隐藏并将此容器用于动画。但出现了问题(请看第二张截图)。请帮忙解决。

第一张截图:
在此输入图像描述

第二张截图:
在此输入图像描述

这是包含两个容器的Stack的代码。我只是为了演示目的而使用了第二个容器。我只需要一个容器。

...
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 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

答案1

得分: 0

你需要使你的页面可以滚动(使用ListViewSingleChildScrollView小部件),以防止溢出。

然后,你可以使用Opacity小部件来动态改变其可见性。

你还可以使用布尔值来有条件地显示它。例如:

bool showContainer = false;
child: showContainer 
            ? Center(...)          //如果为true
            : SizedBox.shrink();   //如果为false

SizedBox.shrink()小部件返回一个空的框。

英文:

you need to make your page scrollable (ListView or SingleChildScrollView widget) so it doesn't overflow.

You can then use the Opacity widget to dynamically change its visibility.

You may also use a boolean value to make it appear conditionally. For example:

bool showContainer = false;
child: showContainer 
            ? Center(...)          //if true
            : SizedBox.shrink();   //if false

SizedBox.shrink() widget returns an empty box

答案2

得分: 0

如果我没有理解错,你正在尝试使一个小部件可以隐藏,所以只需像这样定义一个布尔变量:

bool isVisible = true;

之后,你的小部件将是这样的:

Column(
  children: [
    InkWell(
      onTap: () {
        isVisible = !isVisible;
        setState(() {});
      },
      child: Container(
        padding: const EdgeInsets.all(10),
        color: Colors.blue,
        child: const Center(
          child: Text('蓝色容器'),
        ),
      ),
    ),
    if (isVisible)
      Container(
        padding: const EdgeInsets.all(10),
        color: Colors.red,
        child: const Center(
          child: Text('红色容器'),
        ),
      ),
  ],
)

在这个示例中,单击第一个容器后,第二个容器将不可见。请注意,你的屏幕或父小部件应该是有状态的。

英文:

If I'm not mistaken your trying to make a widget hidable, so just define a bool var like this:

bool isVisible = true;

after that, your widgets would be something like this:

Column(
            children: [
              InkWell(
                onTap: () {
                  isVisible = !isVisible;
                  setState(() {});
                },
                child: Container(
                  padding: const EdgeInsets.all(10),
                  color: Colors.blue,
                  child: const Center(
                    child: Text('Blue Container'),
                  ),
                ),
              ),
              if (isVisible)
                Container(
                  padding: const EdgeInsets.all(10),
                  color: Colors.red,
                  child: const Center(
                    child: Text('Red Container'),
                  ),
                ),
            ],
          ),

in this example by clicking the first container, the second container will be invisible. consider that your screen or your parent widget should be stateful.

答案3

得分: 0

你可以尝试使用 OverflowBox 小部件,它允许其子元素超出其边界而不会导致布局错误。以下是代码:

Stack(
  children: [
    Container(
      alignment: Alignment.bottomCenter,
      color: Colors.blue,
      height: 300,
    ),
    Positioned(
      bottom: 0,
      left: 0,
      right: 0,
      child: OverflowBox(
        maxHeight: 300, // 设置最大高度以控制显示多少内容
        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),
            ),
          ),
        ),
      ),
    ),
  ],
)
英文:

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

huangapple
  • 本文由 发表于 2023年7月23日 19:07:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747911.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定