How to Change width of element inside ListView?
Asked Answered
D

5

6

I have a ListView with some items, but my problem is that the item inside the ListView will expand through the all-screen width, and I tried to use SizedBox and Container but it doesn't work for me. Here is my code:

  body: ListView.builder(
    itemCount: 10,
    itemBuilder: (ctx, index) {
      return SizedBox(
        width: 10,
        height: 10,
        child: Card(
          color: btnColor,
          child: Text(
            "Helo",
            // loremIpsum,
            style: TextStyle(color: Colors.black.withOpacity(0.6)),
          ),
        ),
      );
    },
  ),
Dichlorodifluoromethane answered 18/6, 2021 at 12:11 Comment(0)
D
3

I fixed my issue from this link:

body: ListView.builder(
        itemCount: 10,
        itemBuilder: (ctx, index) {
          return Row(
            children: [
              Container(height: 50, width: 50, color: Colors.black),
            ],
          );
        },
      ),

Just use Row inside the ListView

Dichlorodifluoromethane answered 18/6, 2021 at 12:11 Comment(0)
E
5

Try wrap it in Row like this:

  body: ListView.builder(
    itemCount: 10,
    itemBuilder: (ctx, index) {
      return Row(
       children:[
         SizedBox(
          width: 10,
          height: 10,
          child: Card(
           color: btnColor,
           child: Text(
            "Helo",
            // loremIpsum,
            style: TextStyle(color: Colors.black.withOpacity(0.6)),
          ),
        ),
      )]
     );
    },
  ),
Enterectomy answered 18/6, 2021 at 14:40 Comment(0)
D
3

I fixed my issue from this link:

body: ListView.builder(
        itemCount: 10,
        itemBuilder: (ctx, index) {
          return Row(
            children: [
              Container(height: 50, width: 50, color: Colors.black),
            ],
          );
        },
      ),

Just use Row inside the ListView

Dichlorodifluoromethane answered 18/6, 2021 at 12:11 Comment(0)
I
3

Alternatively you can wrap your child widget in a container and set alignment to Alignment.centerLeft

 body: ListView.builder(
        itemCount: 10,
        itemBuilder: (ctx, index) {
          return ListView.builder(
            itemCount: 10,
            itemBuilder: (ctx, index) {
              return Container(
                alignment: Alignment.centerLeft,
                child: Card(
                  color: Colors.green,
                  child: Text(
                    "Helo",
                    // loremIpsum,
                    style: TextStyle(color: Colors.black.withOpacity(0.6)),
                  ),
                ),
              );
            },
          );
        },
      ),
Intervene answered 18/6, 2021 at 14:10 Comment(0)
Q
1

Widget build(BuildContext context) { var screenWidth = MediaQuery.of(context).size.width;

return Scaffold(
  body: SingleChildScrollView(
    child: Container(
      width: screenWidth / 2, //Takes half of the screen size
      child: ListView.builder(
        physics: NeverScrollableScrollPhysics(),
        itemCount: 10,
        itemBuilder: (ctx, index) {
          return Text(index.toString());
        },
      ),
    ),
  ),
);

}

Quiescent answered 18/6, 2021 at 12:48 Comment(0)
F
0

You can make this with FittedBox in a Alignlike this:

ListView.builder(
  itemCount: 10,
  itemBuilder: (ctx, index) {
    return Align(
      alignment: Alignment.centerLeft,
      child: FittedBox(
        child: Container(height: 50, width: 50, color: Colors.black),
      ),
    );
  },
)
Formalin answered 9/10 at 14:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.