使用Flutter ListView构建器抛出错误

时间:2019-07-17 07:32:30

标签: flutter dart

所以我试图创建布局,但我遇到的错误是我的脚本

  Column(
      children: <Widget>[
        Container(
          decoration: new BoxDecoration(boxShadow: [
            new BoxShadow(
              color: Colors.black,
              blurRadius: 10.0,
            ),
          ]),
          child:
          Card(
              child: CarouselSlider(
                autoPlay: true,
                height: 120.0,
                items: [1, 2, 3, 4, 5].map((i) {
                  return Builder(
                    builder: (BuildContext context) {
                      return Container(
                          width: MediaQuery.of(context).size.width,
                          margin: EdgeInsets.all(1),
                          decoration: BoxDecoration(color: Colors.brown),
                          child: Text(
                            'text $i',
                            style: TextStyle(fontSize: 16.0),
                          ));
                    },
                  );
                }).toList(),
              )),
        ),
        Row(
          children: <Widget>[
            Container(
              width: MediaQuery.of(context).size.width * 0.70,
              child: Card(
                color: null,
                child: Padding(
                    padding: const EdgeInsets.all(10.0),
                    child: Text(
                      "Your Points : ",
                      style: TextStyle(fontSize: 15),
                    )),
              ),
            ),
          ],
        ),
        Expanded(child: populateHotProduct(),),
        Expanded(child: populateHotProduct(),),
      ],

  );

运行它时出现此错误

  

我/聊天(5867):uid = 10080(com.tukangaplikasi.member_lf)线程2   相同的187行I / flutter(5867):引发了另一个异常:   未布置RenderBox:RenderRepaintBoundary#46776   relayoutBoundary = up3 NEEDS-PAINT I / flutter(5867):══╡例外   渲染库的问题   ╞═════════════════════════════════════════════════ ════════我/颤振(   5867):在performLayout()期间引发了以下断言:   I / flutter(5867):BoxConstraints强制无限宽度。我/扑   (5867):这些无效约束已提供给   RenderPositionedBox的layout()函数由以下I / flutter(   5867):函数,该函数可能会计算出   问题:我/颤振(5867):_RenderListTile._layoutBox   (包:flutter / src / material / list_tile.dart:1300:9)I / flutter(   5867):令人讨厌的约束条件是:I / flutter(5867):
  BoxConstraints(w = Infinity,0.0 <= h <= Infinity)

我已经尝试将Expanded更改为Flexible,但仍然没有帮助。

因此,在populateHotProduct内是ListView.Builder

return  ListView.builder(
        itemCount: hotlist.length,
        shrinkWrap: true,
        scrollDirection: Axis.horizontal,
        itemBuilder: (BuildContext context, int index) {
          return GestureDetector(
              onTap: () {
                print("Product detail");
              },
              child: Card(
                child: Container(
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      Image.network(
                          Configuration.url +
                              "assets/app_assets/" +
                              hotlist[index].productImage,
                          width: 100,
                          height: 100,
                          filterQuality: FilterQuality.low),
                      ListTile(
                        title: Center(
                            child: Text(
                          hotlist[index].productName,
                          style: TextStyle(fontSize: 23),
                        )),
                        subtitle: Center(
                            child: Text(
                          formatCurrency
                              .format(int.parse(hotlist[index].productPrice)),
                          style: TextStyle(color: Colors.red, fontSize: 15),
                        )),
                      ),
                    ],
                  ),
                ),
              ));
        });

我又两次故意打populateHotProduct()。我要如何解决它,谢谢。

1 个答案:

答案 0 :(得分:0)

为卡的子容器指定宽度时,此问题已解决。我的猜测是,列表视图水平滑动,并且当您未设置容器的宽度时,容器会适应最大。

enter image description here

Column(
        children: <Widget>[
            Container(
                decoration: new BoxDecoration(boxShadow: [
                    new BoxShadow(
                        color: Colors.black,
                        blurRadius: 10.0,
                    ),
                ]),
                child:
                Card(
                    child: Text('kakaka')),
            ),
            Row(
                children: <Widget>[
                    Container(
                        width: MediaQuery.of(context).size.width * 0.70,
                        child: Card(
                            color: null,
                            child: Padding(
                                padding: const EdgeInsets.all(10.0),
                                child: Text(
                                    "Your Points : ",
                                    style: TextStyle(fontSize: 15),
                                )),
                        ),
                    ),
                ],
            ),
            Expanded(
                child: ListView.builder(
                    itemCount: 3,
                    shrinkWrap: true,
                    scrollDirection: Axis.horizontal,
                    itemBuilder: (BuildContext context, int index) {
                        return GestureDetector(
                            onTap: () {
                                print("Product detail");
                            },
                            child: Card(
                                child: Container(
                                    width: 300,
                                    child: Column(
                                        mainAxisSize: MainAxisSize.min,
                                        children: <Widget>[
                                            Image.network(
                                                'http://img1.juimg.com/151014/330555-151014213H140.jpg',
                                                width: 100,
                                                height: 100,
                                                filterQuality: FilterQuality.low),
                                            ListTile(
                                                title: Center(
                                                    child: Text(
                                                        '11111',
                                                        style: TextStyle(fontSize: 23),
                                                    )),
                                                subtitle: Center(
                                                    child: Text(
                                                        'jjjj',
                                                        style: TextStyle(color: Colors.red, fontSize: 15),
                                                    )),
                                            ),
                                        ],
                                    ),
                                ),
                            ));
                    }),),

            Expanded(
                child: ListView.builder(
                    itemCount: 3,
                    shrinkWrap: true,
                    scrollDirection: Axis.horizontal,
                    itemBuilder: (BuildContext context, int index) {
                        return GestureDetector(
                            onTap: () {
                                print("Product detail");
                            },
                            child: Card(
                                child: Container(
                                    width: 300,
                                    child: Column(
                                        mainAxisSize: MainAxisSize.min,
                                        children: <Widget>[
                                            Image.network(
                                                'http://img1.juimg.com/151014/330555-151014213H140.jpg',
                                                width: 100,
                                                height: 100,
                                                filterQuality: FilterQuality.low),
                                            ListTile(
                                                title: Center(
                                                    child: Text(
                                                        '11111',
                                                        style: TextStyle(fontSize: 23),
                                                    )),
                                                subtitle: Center(
                                                    child: Text(
                                                        'jjjj',
                                                        style: TextStyle(color: Colors.red, fontSize: 15),
                                                    )),
                                            ),
                                        ],
                                    ),
                                ),
                            ));
                    }),),
        ],

    )