【问题标题】:ReorderableListView: Exception: BoxConstraints forces an infinite heightReorderableListView:异常:BoxConstraints 强制无限高度
【发布时间】:2019-11-04 22:20:12
【问题描述】:

TL;DR - 如何在ReorderableListView 中实现shrinkWrap = true

我试图以Column<-Container 作为父级创建ReoderableListView,发生了这个错误。

I/flutter (32618): The following assertion was thrown during performLayout():
I/flutter (32618): BoxConstraints forces an infinite height.


I/flutter (32618): The following RenderObject was being processed when the exception was fired: RenderStack#23840 relayoutBoundary=up17 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE:
I/flutter (32618):   creator: Stack ← _Theatre ← Overlay-[GlobalKey#dc153 ReorderableListView overlay key] ←
I/flutter (32618):     ReorderableListView ← StreamBuilder<QuerySnapshot> ← Column ← Padding ← Padding ← Container ←
I/flutter (32618):     BoardCard ← Column ← _SingleChildViewport ← ⋯
I/flutter (32618):   parentData: not positioned; offset=Offset(0.0, 0.0) (can use size)
I/flutter (32618):   constraints: BoxConstraints(0.0<=w<=328.0, 0.0<=h<=Infinity)
I/flutter (32618):   size: MISSING
I/flutter (32618):   alignment: AlignmentDirectional.topStart
I/flutter (32618):   textDirection: ltr
I/flutter (32618):   fit: expand
I/flutter (32618):   overflow: clip
I/flutter (32618): This This RenderObject had the following child:
I/flutter (32618):     child 1: _RenderLayoutBuilder#55d3d NEEDS-LAYOUT NEEDS-PAINT

如果它是ListView,使用shrinkWrap = true 很容易解决此问题,但ReorderableListView 没有此属性。

这里是代码

@override
  Widget build(BuildContext context) {
    final _listTitleStyle =
        TextStyle(fontSize: 20, fontWeight: FontWeight.w500);
    final _listSubTitleStyle = TextStyle(
      fontSize: 14,
      fontWeight: FontWeight.w400,
      color: Color(0xff7D7373),
    );
    return Container(
      padding: EdgeInsets.all(8),
      margin: EdgeInsets.all(8),
      child: Column(
        children: <Widget>[
          Container(
            child: Row(
              children: <Widget>[
                Expanded(
                  child: Row(
                    textBaseline: TextBaseline.alphabetic,
                    crossAxisAlignment: CrossAxisAlignment.baseline,
                    children: <Widget>[
                      Text(
                        widget.cardTitle,
                        style: _listTitleStyle,
                      ),
                      SizedBox(
                        width: 2,
                      ),
                      Text(
                        widget.cardSubTitle,
                        style: _listSubTitleStyle,
                      ),
                    ],
                  ),
                ),
                Icon(Icons.keyboard_arrow_down),
              ],
            ),
          ),
          SizedBox(
            height: 8,
          ),
          StreamBuilder<QuerySnapshot>(
              stream: widget.query,
              builder: (context, snapshot) {
                if (!snapshot.hasData) return LinearProgressIndicator();
                return _buildList(context, snapshot.data.documents);
              }),
        ],
      ),
    );
  }

  Widget _buildList(BuildContext context, List<DocumentSnapshot> snapshot) {
    snapshot.forEach((snap) {
      print("debug 1 ${snap.data}");
      print(TodoItem.fromSnapshot(snap, snap.reference).toString());
    });
    return ReorderableListView(
      onReorder: (oldIndex, newIndex){},
      children: snapshot
          .map((data) => TodoTile(TodoItem.fromSnapshot(data, data.reference), key: UniqueKey(),))
          .toList(),
    );
  }

【问题讨论】:

  • ReorderableListView 包裹在一个容器中并给它一个高度
  • @Abdulrahman 这样做不是一个选项,因为我希望高度是孩子们的身高之和,类似于 WrapContent
  • 这可能是因为它在 Column 内 -- 请参阅 stackoverflow.com/questions/52801201/…

标签: flutter flutter-layout


【解决方案1】:

我已经通过用包flutter_reorderable_list 替换ReorderableListView 解决了这个问题,它接受一个孩子(Widget),不像ReorderableListView 接受孩子(列表)

它的实现有点复杂。您可以查看example 以了解正确实现它。

【讨论】:

  • 如果你有一个简化的例子,如果你在这里发布它会很棒:)
【解决方案2】:

我同意 Abdulrahman Falyoun 的观点。您只需要动态调整高度并根据列表中的项目数进行调整。

 Container(
      height: (list.length * 40).toDouble(),
      padding: const EdgeInsets.all(10),
      child: ReorderableListView(
        children: list.map((item) {
          return getItem(item);
        }).toList(),
        onReorder: _onReorder,
      ),
    )

或者如果它在一个列中,那么用Expanded()包裹它

【讨论】:

    【解决方案3】:

    使用 Columnto listview 并应用 shrinkWrap = true

    @override
      Widget build(BuildContext context) {
        final _listTitleStyle =
            TextStyle(fontSize: 20, fontWeight: FontWeight.w500);
        final _listSubTitleStyle = TextStyle(
          fontSize: 14,
          fontWeight: FontWeight.w400,
          color: Color(0xff7D7373),
        );
        return Container(
          padding: EdgeInsets.all(8),
          margin: EdgeInsets.all(8),
          child: Listview(
            shrinkWrap = true
            children: <Widget>[
              Container(
                child: Row(
                  children: <Widget>[
                    Expanded(
                      child: Row(
                        textBaseline: TextBaseline.alphabetic,
                        crossAxisAlignment: CrossAxisAlignment.baseline,
                        children: <Widget>[
                          Text(
                            widget.cardTitle,
                            style: _listTitleStyle,
                          ),
                          SizedBox(
                            width: 2,
                          ),
                          Text(
                            widget.cardSubTitle,
                            style: _listSubTitleStyle,
                          ),
                        ],
                      ),
                    ),
                    Icon(Icons.keyboard_arrow_down),
                  ],
                ),
              ),
              SizedBox(
                height: 8,
              ),
              StreamBuilder<QuerySnapshot>(
                  stream: widget.query,
                  builder: (context, snapshot) {
                    if (!snapshot.hasData) return LinearProgressIndicator();
                    return _buildList(context, snapshot.data.documents);
                  }),
            ],
          ),
        );
      }
    
      Widget _buildList(BuildContext context, List<DocumentSnapshot> snapshot) {
        snapshot.forEach((snap) {
          print("debug 1 ${snap.data}");
          print(TodoItem.fromSnapshot(snap, snap.reference).toString());
        });
        return ReorderableListView(
          onReorder: (oldIndex, newIndex){},
          children: snapshot
              .map((data) => TodoTile(TodoItem.fromSnapshot(data, data.reference), key: UniqueKey(),))
              .toList(),
        );
      }
    

    【讨论】:

      猜你喜欢
      • 2020-12-22
      • 2020-04-27
      • 2023-04-09
      • 2019-02-25
      • 2022-01-04
      • 1970-01-01
      • 2021-10-15
      • 2020-09-21
      • 1970-01-01
      相关资源
      最近更新 更多