修复Flutter ListView.builder()溢出问题

时间:2019-06-01 02:25:29

标签: flutter flutter-layout

我正在使用将来的构建器和列表视图构建器,并且不断收到垂直溢出错误。我用ListView.builder()包围了Container,并给它设置了一个固定的高度,但仍然给我带来麻烦。如何使错误消失?

这是树:

class OperationsScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<OperationsProvider>(
      builder: (_) => OperationsProvider(),
      child: GenericScreen(
        title: 'Operations',
        endIcon: IconButton(
          icon: Icon(Icons.settings),
          iconSize: 28.0,
          onPressed: () {},
        ),
        child: EntityList(),
      ),
    );
  }
}

class GenericScreen extends StatelessWidget {
  final String title;
  final Widget child;
  final IconButton endIcon;

  GenericScreen({
    @required this.title,
    @required this.child,
    this.endIcon
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(8.0),
      child: Column(
        children: <Widget>[
          Flex(
            direction: Axis.horizontal,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Text(
                this.title,
                style: Theme.of(context).textTheme.title,
              ),
              this.endIcon ?? Container()
            ],
          ),
          this.child
        ],
      ),
    );
  }
}

class EntityList extends StatelessWidget {
  ////////////////////////////////////////
  // This is where the error is showing //
  ////////////////////////////////////////

  @override
  Widget build(BuildContext context) {
    var appProvider = Provider.of<AppProvider>(context);
    var opsProvider = Provider.of<OperationsProvider>(context);

    return FutureBuilder(
      future: opsProvider.getEntities(appProvider.apiHeaders),
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        return snapshot.hasData ? Container(
          height: double.maxFinite,
          child: ListView.builder(
            itemCount: snapshot.data['content'].length,
            itemBuilder: (BuildContext context, int index) {
              var entity = snapshot.data['content'][index];
              return Container(
                child: Text(entity['entityId'])
              );
            }
          )
        ) : CircularProgressIndicator();
      },
    );
  }
}

0 个答案:

没有答案