Flutter Flex Widget

1.2k views Asked by At

I have this class State:

class _ItemCardState extends State<ItemCard> {
  double imgSize = 30;
  Axis expanded = Axis.horizontal;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        setState(() {
          expanded =
              expanded == Axis.horizontal ? Axis.vertical : Axis.horizontal;
          imgSize = imgSize == 30 ? 200 : 30;
        });
      },
      child: Card(
        margin: const EdgeInsets.all(5.0),
        child: Padding(
          padding: const EdgeInsets.all(15),
          child: Flex(
            direction: expanded,
            children: [
              Image.network(
                'http://cdn.shopify.com/s/files/1/0565/0697/4379/articles/Cafe-expresso-sin-maquina_1200x1200.jpg?v=1621619617',
                width: imgSize,
              ),
              Column(
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(widget.product['name'],
                          style: const TextStyle(fontSize: 20)),
                      Text(widget.product['price'] + ' \$',
                          style: const TextStyle(fontSize: 15)),
                    ],
                  ),
                  Text(widget.product['ingredients'],
                      style: const TextStyle(fontSize: 15, color: Colors.grey)),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}

I want the Flex Widget change direction onTap, it works. But the column inside is not taking all space avaible in the crossAxis. If I put an Expanded Widget it stops working,... I've tried Flexibles, but somehow it didnt work. I used ListTiles also, but I couldnt make it work.

Any idea on how to do it?

screenshot

1

There are 1 answers

0
Lara Cesio On

well. I resolved it putting the Flex Widget inside a SizedBox and then I was able to use Flexible>fit:FlexFit.tigth:

SizedBox(
        height: 300,
        child: Flex(
          direction: expanded,
          children: [
            Image.network(
              'http://cdn.shopify.com/s/files/1/0565/0697/4379/articles/Cafe-expresso-sin-maquina_1200x1200.jpg?v=1621619617',
              width: imgSize,
            ),
            Flexible(
              fit: FlexFit.tight,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(widget.product['name'],
                          style: const TextStyle(fontSize: 20)),
                      Text(widget.product['price'] + ' \$',
                          style: const TextStyle(fontSize: 15)),
                    ],
                  ),
                  Text(widget.product['ingredients'],
                      style: const TextStyle(
                          fontSize: 15, color: Colors.grey)),
                ],
              ),
            )
          ],
        ),
      ),