How to set container width inside Expanded ListView

Viewed 246

I am unable to set width for the Container when I wrap ListView inside Expanded widget.

Widget build(BuildContext context) {
return Center(
  child: Column(
    children: [
      Padding(
        padding: EdgeInsets.only(top: 30, bottom: 30),
        child: Text(
          'Transactions History',
          style: TextStyle(
            fontWeight: FontWeight.bold,
            fontSize: 25,
          ),
        ),
      ),
      Expanded(
        child: ListView.builder(
          physics: BouncingScrollPhysics(),
          itemCount: 30,
          itemBuilder: (BuildContext context, int index) {
            return Container(
              width: 100,
              color: Colors.red,
              child: Card(
                child: Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 25, vertical: 15),
                ),
              ),
            );
          },
        ),
      ),
    ],
  ),
);
}
1 Answers

You need to add the Container of your listItem in the itemBuilder inside an Align widget.

So your code in the itemBuilder will look like this:

return Align(
  alignment: Alignment.centerLeft,
  child: Container(
    width: 100,
    color: Colors.red,
    child: Card(
      child: Padding(
        padding: const EdgeInsets.symmetric(
           horizontal: 25,
           vertical: 15,
        ),
      ),
    ),
  ),
);
Related