Flutter引发异常BoxConstraints强制无限宽度

时间:2018-07-12 19:09:10

标签: dart flutter

我正在尝试使用复选框实现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)

1 个答案:

答案 0 :(得分:1)

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