Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add dynamic height horizontal list-view inside vertical list in Flutter

I am adding horizontal listview inside the vertical list.its not showing any content

 @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 ");
                        }));
              }
            }));
  }

when I add the height attribute to position 2 container it works. but I want dynamic height.

SingleChildScrollView is another option but it won't be suitable for an infinite list.

like image 624
Rahul Devanavar Avatar asked Feb 01 '19 13:02

Rahul Devanavar


1 Answers

Switch to a Row inside a 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")],
              ),
            );
          }
        },
      ),
    );
  }
like image 137
Jordan Davies Avatar answered Oct 16 '22 05:10

Jordan Davies