List of horizontal list in Flutter
Asked Answered
R

3

38

I have followed this tutorial and fully implemented a horizontally scrolling list. Now, what I would like to do is to create a vertical list where each row is a horizontal list.

I tried different approaches, but I keep thinking that it should be possible to simply set the horizontal list as a child of the vertical, but it doesn't work.

My code is:

Widget build(BuildContext context) {
return new Scaffold(
  
  body: Container(
      margin: EdgeInsets.symmetric(vertical: 20.0),
      height: 160.0,
      child: ListView(
        children: <Widget>[
          Text("First line"),
          HorizontalList(),
          Text("Second line"),
          HorizontalList()
        ],
      )
  ),

  drawer: new MyNavigationDrawer(),
);

}

I also tried putting the various horizontal lists inside ListTiles but the result is the same.

Romanist answered 28/6, 2018 at 18:5 Comment(0)
H
74

I guess what you want is a list within a another list Here is the adaptation of the sample program that you have followed The build method is like:

 Widget build(BuildContext context) {
    Widget horizontalList1 = new Container(
      margin: EdgeInsets.symmetric(vertical: 20.0),
      height: 200.0,
      child: new ListView(
      scrollDirection: Axis.horizontal,
      children: <Widget>[
        Container(width: 160.0, color: Colors.red,),
        Container(width: 160.0, color: Colors.orange,),
        Container(width: 160.0, color: Colors.pink,),
        Container(width: 160.0, color: Colors.yellow,),
      ],
    )
    );
    Widget horizontalList2 = new Container(
        margin: EdgeInsets.symmetric(vertical: 20.0),
        height: 200.0,
        child: new ListView(
      scrollDirection: Axis.horizontal,
      children: <Widget>[
        Container(width: 160.0, color: Colors.blue,),
        Container(width: 160.0, color: Colors.green,),
        Container(width: 160.0, color: Colors.cyan,),
        Container(width: 160.0, color: Colors.black,),
      ],
    )
    );
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Center(
        child: new Container(
          child: ListView(
            scrollDirection: Axis.vertical,
            children: <Widget>[
              horizontalList1,
              horizontalList2,
            ],
          ),
        ),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );

The result is like:

enter image description here

Hope it helps

Henton answered 28/6, 2018 at 19:18 Comment(0)
C
7

Screenshot:

enter image description here


Let's say, this is your horizontal ListView:

ListView _horizontalList(int n) {
  return ListView(
    scrollDirection: Axis.horizontal,
    children: List.generate(
      n,
      (i) => Container(
        width: 50,
        color: Colors.accents[i % 16],
        alignment: Alignment.center,
        child: Text('$i'),
      ),
    ),
  );
}

You can put the it either in a ListView or a Column.

  • In a ListView:

    Wrap your horizontal list in a SizedBox and provide a fixed height.

    ListView(
      children: [
        SizedBox(
          height: 50,
          child: _horizontalList(8),
        ),
        SizedBox(
          height: 50,
          child: _horizontalList(12),
        ),
        SizedBox(
          height: 50,
          child: _horizontalList(16),
        ),
      ],
    )
    
  • In a Column:

    You also have advantage of using Expanded/Flexible widgets.

    Column(
      children: [
        SizedBox(
          height: 50,
          child: _horizontalList(8),
        ),
        Expanded(
          child: _horizontalList(12),
        ),
        Flexible(
          child: _horizontalList(16),
        ),
      ],
    )
    
Cryotherapy answered 17/8, 2021 at 15:28 Comment(0)
G
0

here is the horizontal list builder in flutter

          Expanded(
        child: ListView.builder(
          shrinkWrap: true,
          itemBuilder: (ctx,index){
            return Card(
              child: ListTile(
                  title: Text('Motivation $index'),
                  subtitle: Text('this is a description of the motivation')),
            );
          },
        ),
      ),
Glycogen answered 4/2, 2023 at 16:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.