在垂直列表内添加动态高度水平列表视图

时间:2019-02-01 13:50:40

标签: android ios listview dart flutter

我在垂直列表中添加了水平列表视图。它不显示任何内容

 @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: new ListView.builder(
            itemCount: 3,
            scrollDirection: Axis.vertical,
            itemBuilder: (context, position) {
              if (position == 0) {
                return Container(
                  child: Text("First rwo"),
                );
              } else if (position == 1) {
                return Container(
                  child: Text("second  rwo"),
                );
              } else if (position == 2) {
                return Container(
                    child: ListView.builder(
                        scrollDirection: Axis.horizontal,
                        itemCount: 4,
                        shrinkWrap: true,
                        itemBuilder: (context, pos) {
                          return Text("List ");
                        }));
              }
            }));
  }

当我将height属性添加到位置2 container时,它将起作用。 但我要动态高度。

SingleChildScrollView是另一种选择,但不适用于无限列表。

1 个答案:

答案 0 :(得分:1)

Row内切换到SingleChildScrollview

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: new ListView.builder(
        itemCount: 3,
        scrollDirection: Axis.vertical,
        itemBuilder: (context, position) {
          if (position == 0) {
            return Container(
              child: Text("First rwo"),
            );
          } else if (position == 1) {
            return Container(
              child: Text("second  rwo"),
            );
          } else if (position == 2) {
            return SingleChildScrollView(
              scrollDirection: Axis.horizontal,
              child: Row(
                children: [Text("List"), Text("List"), Text("List"), Text("List")],
              ),
            );
          }
        },
      ),
    );
  }