Search code examples
react-nativereact-native-elements

React-Native-Elements: Showing image component in the center of the rounded Avatar


I am using react-native-elements to render an Avatar , I have a image to be shown in a rounded circle Avatar & I would like the image to be in the center of the circle Avatar.

This is what I tried:

<Avatar
   size={60}
   containerStyle={{backgroundColor: 'black'}}
   rounded
   ImageComponent={MyImg}
   avatarStyle={{justifyContent: 'center', alignItems: 'center'}}
 />

MyImg is a tsx file that is converted from a SVG.

The above code results to MyImg showing on the top left position of the rounded Avatar.

Then I tried:

<Avatar
       size={60}
       containerStyle={{backgroundColor: 'black', justifyContent: 'center', alignItems: 'center'}}
       rounded
       ImageComponent={MyImg}
       avatarStyle={{justifyContent: 'center', alignItems: 'center'}}
     />

This code makes MyImg disappear. So, I get stuck now.

How to make the image component showing in the center ?

==== more info ====

Here is MyImg.tsx file:

function SvgMyImg(props) {
  return (
    <Svg width={24} height={24} fill="none" {...props}>
      <Path
        d="M7 7l10 10M17 7L7 17"
        stroke="#fff"
        strokeWidth={2}
        strokeLinecap="round"
      />
    </Svg>
  );
}

export default SvgMyImg;

I just import this file e.g. import MyImg from '../assets/images/MyImg'; and use it as the imageComponent of Avatar like my code shows.


Solution

  • I've tried something like what you want to do in snack.expo. The key is using position:absolute

     <View style={styles.container}>
          <Avatar
            size={60}
            containerStyle={{ backgroundColor: 'black' }}
            rounded
            ImageComponent={() => (
              <Image
                resizeMode="contain"
                style={{
                  height: 30,
                  width: 30,
                  borderRadius: 25,
                  position: 'absolute',
                }}
                source={{
                  uri: 'https://homepages.cae.wisc.edu/~ece533/images/boat.png',
                }}
              />
            )}
            overlayContainerStyle={{
              justifyContent: 'center',
              alignItems: 'center',
            }}
          />
        </View>