【问题标题】:How can I fix RenderCustomMultiChildLayoutBox object was given an infinite size during layout?如何修复 RenderCustomMultiChildLayoutBox 对象在布局期间被赋予无限大小?
【发布时间】:2021-08-29 19:25:38
【问题描述】:

代码:

import 'dart:convert';

import 'package:flutter/material.dart';

import 'package:flutter/foundation.dart';

class BeerListPage extends StatefulWidget {
  BeerListPage({required Key key}) : super(key: key);

  @override
  BeerListPageState createState() => BeerListPageState();
}

class BeerListPageState extends State<BeerListPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: new Container(
          child: new Center(
            child: new FutureBuilder(
                future:
                DefaultAssetBundle.of(context).loadString('packages/beers/beers.json'),
                builder: (context, snapshot) {
                  var beers = json.decode(snapshot.data.toString());

                  return new ListView.builder(shrinkWrap: true,
                    itemBuilder: (BuildContext context, int index) {
                      var beer = beers[index];
                      return new Card(
                        child: new Column(
                          crossAxisAlignment: CrossAxisAlignment.stretch,
                          children: <Widget>[
                            new Text("Name: " + beer['name'],
                                style: TextStyle(
                                    fontWeight: FontWeight.bold, fontSize: 24)),
                            new Text("Country: " + beer['country'],
                                style: TextStyle(
                                    fontWeight: FontWeight.normal, fontSize: 20)),
                            new Text("ABV: " + beer['abv'],
                                style: TextStyle(
                                    fontWeight: FontWeight.normal, fontSize: 20)),
                            new Image.network(beer['image'], height: 200)
                          ],
                        ),
                      );
                    },
                    itemCount: beers == null ? 0 : beers.length,
                  );
                }),
          ),
        ));
  }
}

错误信息: 底部的无限像素溢出的 RenderFlex。 RenderCustomMultiChildLayoutBox 对象在布局期间被赋予了无限大小。 _RenderInkFeatures 对象在布局期间被赋予了无限大小。 RenderPhysicalModel 对象在布局期间被赋予了无限大小。 相关的导致错误的小部件是: 脚手架文件:///C:/Users/Miche/AndroidStudioProjects/untitled/lib/beer_list.dart:17:12

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    试试这个:

    import 'dart:convert';
    
    import 'package:flutter/material.dart';
    
    import 'package:flutter/foundation.dart';
    
    class BeerListPage extends StatefulWidget {
      BeerListPage({required Key key}) : super(key: key);
    
      @override
      BeerListPageState createState() => BeerListPageState();
    }
    
    class BeerListPageState extends State<BeerListPage> {
      final Size size = MediaQuery.of(context).size;
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            body: new Container(
              height: size.height,
              width: double.infinity,
              child: new Center(
                child: new FutureBuilder(
                    future:
                    DefaultAssetBundle.of(context).loadString('packages/beers/beers.json'),
                    builder: (context, snapshot) {
                      var beers = json.decode(snapshot.data.toString());
    
                      return new ListView.builder(shrinkWrap: true,
                        itemBuilder: (BuildContext context, int index) {
                          var beer = beers[index];
                          return new Card(
                            child: new Column(
                              crossAxisAlignment: CrossAxisAlignment.stretch,
                              children: <Widget>[
                                new Text("Name: " + beer['name'],
                                    style: TextStyle(
                                        fontWeight: FontWeight.bold, fontSize: 24)),
                                new Text("Country: " + beer['country'],
                                    style: TextStyle(
                                        fontWeight: FontWeight.normal, fontSize: 20)),
                                new Text("ABV: " + beer['abv'],
                                    style: TextStyle(
                                        fontWeight: FontWeight.normal, fontSize: 20)),
                                new Image.network(beer['image'], height: 200)
                              ],
                            ),
                          );
                        },
                        itemCount: beers == null ? 0 : beers.length,
                      );
                    }),
              ),
            );
        );
      }
    }
    

    【讨论】:

    • 我想在不给出高度的情况下解决这个问题。有什么可能吗?
    猜你喜欢
    • 2019-05-13
    • 2020-08-13
    • 2021-08-06
    • 2020-05-20
    • 2021-09-06
    • 2018-03-31
    • 1970-01-01
    • 2019-09-08
    • 2021-06-25
    相关资源
    最近更新 更多