Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the image width to fill the card and the height to be flexible?

I'm trying to set the dimensions of the image so that the width will always take up the full width of the card (minus the horizontal margin) and with a flexible height, so that it can scale as it needs to.

How can I do this? It seems that the style needs a height, it won't accept null.

I have tried a LOT of combinations using various resizeModes, heights, widths etc.

Here is the component:

enter image description here

And here is my current code:

 renderImage(image) {
    const { width, height } = Dimensions.get('window');

    if (image) {
      return (
        <View style={styles.imageContainer}>
          <Image
            style={{ width, resizeMode: 'contain', height: 300 }}
            source={{ uri: image }}
          />
        </View>
      );
    }
  }
like image 330
bloppit Avatar asked Jan 21 '26 05:01

bloppit


1 Answers

The aspectRatio property solves this problem nicely.

<Image
  style={{ width: '100%', aspectRatio: 1 }}
  source={{uri: 'https://online.picture/dynamic_size'}}/>
like image 127
Nikolai Hegelstad Avatar answered Jan 23 '26 20:01

Nikolai Hegelstad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!