如何将ListView.builder放入抽屉?

问题描述 投票:0回答:1

我正在尝试制作动态菜单(通过json文件)。当我把我的代码放在体内它工作正常。但当我把它放在我的抽屉里时,抽屉是空的,甚至我的DrawerHeader都消失了。

我的代码:

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('My App'),
        backgroundColor: Colors.green,
      ),
      body: ListView.builder(                       // <----------  WORKING
          itemCount: data == null ? 0 : data.length,
          itemBuilder: (BuildContext context, i) {
            return new ListTile(
              title: new Text(data[i]["title"]),
            );
          }),
      drawer: Drawer(
        child: ListView(
          // Important: Remove any padding from the ListView.
          padding: EdgeInsets.zero,
          children: <Widget>[
            Container(
              height: 85.0,
              child: DrawerHeader(
                child: Text(
                  'Categories',
                  style: new TextStyle(fontSize: 18.0, color: Colors.white),
                ),
                decoration: BoxDecoration(
                  color: Colors.green,
                ),
              ),
            ),
            ListView.builder(                       // <---------- NOT WORKING
                itemCount: data == null ? 0 : data.length,
                itemBuilder: (BuildContext context, i) {
                  return new ListTile(
                    title: new Text(data[i]["title"]),
                  );
                })
          ],
        ),
      ),
    );
  }

full code

flutter
1个回答
0
投票

您的ListView.builder小部件需要位于具有固定高度的小部件内。

你可以在Container中设置它:

Container(
            height: double.maxFinite,
            child: ListView.builder(
                itemCount: data == null ? 0 : data.length,
                itemBuilder: (BuildContext context, i) {
                  return new ListTile(
                    title: new Text(data[i]["title"]),
                  );
                }))
© www.soinside.com 2019 - 2024. All rights reserved.