Idesh
Idesh

Reputation: 373

How Can I set width of a card in list view in flutter?

I have a list of cards but when I have changed the width of card it still takes the same width as parent container. I have tried wrapping up listview, card, and even scrollView with sized box or container but nothing has worked for me.

  Container(
              height: MediaQuery.of(context).size.height * 0.6,
              width: MediaQuery.of(context).size.width * 0.8,
              decoration: BoxDecoration(
                color: kWhiteColor,
                borderRadius: BorderRadius.all(
                    Radius.circular(MediaQuery.of(context).size.height * 0.02)),
              ),
              child: Padding(
                padding:
                    EdgeInsets.all(MediaQuery.of(context).size.height * 0.015),
                child: SingleChildScrollView(
                  scrollDirection: Axis.vertical,
                  child: ListView.builder(
                    physics: const NeverScrollableScrollPhysics(),
                    shrinkWrap: true,
                    itemCount: 10,
                    itemBuilder: (BuildContext context, int index) {
                      return SizedBox(
                        width: MediaQuery.of(context).size.width*0.001,
                        height: MediaQuery.of(context).size.height*0.3,
                        child: Card(
                          color: Colors.black,
                        ),
                      );
                    },
                  ),
                ),
              ),
            ),

Problem I'm Facing

White Card Behind is the parent or we can say outermost container.

Upvotes: 2

Views: 5937

Answers (2)

Adelina
Adelina

Reputation: 11891

Could do this

          itemBuilder: (BuildContext context, int index) {
            return Align( // wrap card with Align
              child: SizedBox(
                width: MediaQuery.of(context).size.width * 0.1,
                child: Card(

Upvotes: 3

Er1
Er1

Reputation: 2758

wrap your children in an Align widget

return Align(
  alignment: Alignment.centerRight, //or choose another Alignment
  child: SizedBox(
    width: MediaQuery.of(context).size.width*0.001, //you sure it should be 0.001?
    height: MediaQuery.of(context).size.height*0.3,
    child: Card(
      color: Colors.black,
    ),
  ),
);

Upvotes: 11

Related Questions