【问题标题】:Flutter: Clip a Column or Row to prevent overflowFlutter:剪辑列或行以防止溢出
【发布时间】:2019-09-16 22:29:00
【问题描述】:

我在 Flutter 中有这样的布局结构:

Inkwell
  Card
    ScopedModelDescendant
      Column
        Container[]

列中容器的数量是可变的。

目标是它应该看起来像这样:

但是,它最终会这样做:

我尝试将clipBehavior 属性添加到Card,并尝试在结构中的任何位置混合ClipRects,但似乎没有任何效果。我的最佳猜测是Column 上方的ClipRect 没有帮助,因为溢出发生在列

这是我得到的错误:

flutter: ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
flutter: The following message was thrown during layout:
flutter: A RenderFlex overflowed by 15 pixels on the bottom.
flutter:
flutter: The overflowing RenderFlex has an orientation of Axis.vertical.
flutter: The edge of the RenderFlex that is overflowing has been marked in the rendering with a yellow and
flutter: black striped pattern. This is usually caused by the contents being too big for the RenderFlex.
flutter: Consider applying a flex factor (e.g. using an Expanded widget) to force the children of the
flutter: RenderFlex to fit within the available space instead of being sized to their natural size.
flutter: This is considered an error condition because it indicates that there is content that cannot be
flutter: seen. If the content is legitimately bigger than the available space, consider clipping it with a
flutter: ClipRect widget before putting it in the flex, or using a scrollable container rather than a Flex,
flutter: like a ListView.
flutter: The specific RenderFlex in question is:
flutter:   RenderFlex#094c9 OVERFLOWING
flutter:   creator: Column ← ScopedModelDescendant<EventModel> ← Semantics ← DefaultTextStyle ←
flutter:   AnimatedDefaultTextStyle ← _InkFeatures-[GlobalKey#5fe8b ink renderer] ←
flutter:   NotificationListener<LayoutChangedNotification> ← CustomPaint ← _ShapeBorderPaint ← PhysicalShape
flutter:   ← _MaterialInterior ← Material ← ⋯
flutter:   parentData: <none> (can use size)
flutter:   constraints: BoxConstraints(w=56.0, h=104.3)
flutter:   size: Size(56.0, 104.3)
flutter:   direction: vertical
flutter:   mainAxisAlignment: start
flutter:   mainAxisSize: max
flutter:   crossAxisAlignment: stretch
flutter:   verticalDirection: down
flutter: ◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤
flutter: ════════════════════════════════════════════════════════════════════════════════════════════════════

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    Wrap 小部件可以解决您的问题。如果您的列或行中没有空间,只需使用 Wrap,它也具有对齐和间距属性。

    这是一个简单的例子:

    class WrapExample extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return SizedBox(
          width: 200,
          height: 180,
          child: Card(
            child: Wrap(
              direction: Axis.horizontal,
              spacing: 8.0, // gap between adjacent chips
              runSpacing: 4.0, // gap between lines
              children: <Widget>[
                Chip(
                  avatar: CircleAvatar(
                      backgroundColor: Colors.blue.shade900, child: Text('AH')),
                  label: Text('Hamilton'),
                ),
                Chip(
                  avatar: CircleAvatar(
                      backgroundColor: Colors.blue.shade900, child: Text('ML')),
                  label: Text('Lafayette'),
                ),
                Chip(
                  avatar: CircleAvatar(
                      backgroundColor: Colors.blue.shade900, child: Text('HM')),
                  label: Text('Mulligan'),
                ),
                Chip(
                  avatar: CircleAvatar(
                      backgroundColor: Colors.blue.shade900, child: Text('JL')),
                  label: Text('Laurens'),
                ),
              ],
            ),
          ),
        );
      }
    }
    

    这是输出:

    【讨论】:

    • 这很好用!我什至不需要指定 Wrap 属性,因为其中的元素已经有填充/边距。只需Wrap(children: ...)。我将等待一天,然后将其标记为已接受,但这解决了我的问题 - 谢谢!
    【解决方案2】:

    您也可以将要允许溢出的小部件包装在 Positioned 中的 Stack 小部件中,并设置 Positioned 参数 top: 0

    对我来说就像魅力一样

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-01
      • 2017-10-24
      • 2021-02-12
      • 2019-08-01
      相关资源
      最近更新 更多