如何将ListView.builder放在抽屉中?

时间:2019-01-16 14:54:03

标签: flutter

我正在尝试制作动态菜单(通过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

1 个答案:

答案 0 :(得分: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"]),
                  );
                }))