我正在尝试使用复选框来实现 ListView,但我收到一个错误,提示 BoxConstraints 强制无限宽度。这是我的代码:

body: ListView.builder(itemCount: _items.length,
      itemBuilder: (BuildContext context, int index) {
    bool _isLastClickedIndex = index == _lastClickedPosition;
    return Container(
      child: Card(
        margin: EdgeInsets.all(8.0),
        color: index % 2 == 0 ? Colors.grey : Colors.white,
        child: Center(
          child: Column(
            children: <Widget>[
              CheckboxListTile(
                title: Text('Title Text'),
                subtitle: Text('Subtitle text'),
                controlAffinity: ListTileControlAffinity.leading,
                value: _isLastClickedIndex,
                onChanged: (bool value) {
                  _onCheckBoxChanged(value, index);
                },
                secondary: Icon(Icons.people),
              ),
            ],
          ),
        ),
      ),
    );
  }),
这是错误:
I/flutter (27333): The following assertion was thrown during performLayout():
I/flutter (27333): BoxConstraints forces an infinite width.
I/flutter (27333): These invalid constraints were provided to RenderConstrainedBox's layout() function by the following
I/flutter (27333): function, which probably computed the invalid constraints in question:
I/flutter (27333):   _RenderListTile._layoutBox (package:flutter/src/material/list_tile.dart:726:9)
I/flutter (27333): The offending constraints were:
I/flutter (27333):   BoxConstraints(w=Infinity, 0.0<=h<=Infinity)
I/flutter (27333):
I/flutter (27333): When the exception was thrown, this was the stack:
I/flutter (27333): #0      BoxConstraints.debugAssertIsValid.<anonymous closure>.throwError (package:flutter/src/rendering/box.dart:468:9)
I/flutter (27333): #1      BoxConstraints.debugAssertIsValid.<anonymous closure> (package:flutter/src/rendering/box.dart:509:21)

最佳答案

尝试将 mainAxisSize: MainAxisSize.min 添加到您的 Column 小部件。

关于Flutter 抛出异常 BoxConstraints 强制无限宽度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51312847/

10-10 20:16