1

hi I have Row in flutter and I want add some widget on row with listview.builder, listview sort all item vertically. but I want show them horizontally. in the image below you can see my code and the result.so how can i change the listview.builder to horizontal? enter image description here

1
  • 1
    could you add your code instead of screen shot? Commented Nov 5, 2022 at 9:07

3 Answers 3

2

You need set height for your ListView if you want to use it horizontally, and set scrollDirection to Axis.horizontal:

Row(
        children: [
          IconButton(onPressed: () {}, icon: Icon(Icons.chevron_left)),
          Expanded(
              child: SizedBox(
            height: 10,
            child: ListView.builder(
              scrollDirection: Axis.horizontal,
              itemBuilder: (context, index) {
                return Container(
                  margin: EdgeInsets.symmetric(horizontal: 12),
                  height: 10,
                  width: 10,
                  color: Colors.red,
                );
              },
              itemCount: 10,
            ),
          )),
          IconButton(onPressed: () {}, icon: Icon(Icons.chevron_right)),
        ],
      ),
Sign up to request clarification or add additional context in comments.

Comments

1

There is a property in listview scrollDirection you need to set for horizontal scrolling

  scrollDirection: Axis.horizontal,
      

Comments

0
Widget build(BuildContext context) {
Widget horizontalList = 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,),
  ],
)
);
);
return new Scaffold(
  appBar: new AppBar(
    title: new Text(widget.title),
  ),
  body: new Center(
      child: horizontalList,
  ), 
);

1 Comment

thx but your code is static and only set for container. But my Container is different each time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.