【问题标题】:Wrap class not wrapping widgets包装类不包装小部件
【发布时间】:2021-04-12 13:47:15
【问题描述】:

我正在动态生成 Chip 小部件,但 Wrap 类没有将溢出的小部件包装到下一行。

Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        physics: ClampingScrollPhysics(),
        padding: const EdgeInsets.symmetric(
          horizontal: 12,
        ),
        children: [
          Row(
            children: [
              Wrap(
                spacing: 5,
                children: _getChips(), // gets a list of chips
              ),
            ],
          ),
        ],
      ),
    );

但我在控制台中收到此错误

The following assertion was thrown during layout:
A RenderFlex overflowed by 94 pixels on the right.

The overflowing RenderFlex has an orientation of Axis.horizontal.
The edge of the RenderFlex that is overflowing has been marked in the rendering with a yellow and black striped pattern. This is usually caused by the contents being too big for the RenderFlex.

Consider applying a flex factor (e.g. using an Expanded widget) to force the children of the RenderFlex to fit within the available space instead of being sized to their natural size.
This is considered an error condition because it indicates that there is content that cannot be seen. If the content is legitimately bigger than the available space, consider clipping it with a ClipRect widget before putting it in the flex, or using a scrollable container rather than a Flex, like a ListView.

我做错了吗?

【问题讨论】:

    标签: flutter dart flutter-layout


    【解决方案1】:

    尝试删除 Row 小部件。如果没有足够的空间,使用默认方向换行是 Row 的替代方法,它将子小部件移动到下一行。

    另外,我建议使用 SingleChildScrollView 作为容器而不是 ListView 来获得滚动行为。

    return Scaffold(
      body: SingleChildScrollView(
        physics: ClampingScrollPhysics(),
        child: Padding(
          padding: const EdgeInsets.symmetric(
            horizontal: 12,
          ),
          child: Wrap(
            spacing: 5,
            children: _getChips(), // gets a list of chips
          ),
        ),
      ),
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-11
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      相关资源
      最近更新 更多