Search code examples
androidiosreact-nativenative

How to show multiple user avatar in react native


Demo

How to show this type of view in react native i have tried this code but not getting anywhere.

import React from 'react';
import { View, Image, Text, StyleSheet } from 'react-native';
const Avatar = ({ imageUrl }) => {
  return (
    <View style={styles.avatarContainer}>
      <Image source={{ uri: imageUrl }} style={styles.avatarImage} />
      {/* Add conditional logic to display count badge */}
      {/* For example, if more than 3 users, display count */}
    </View>
  );
};
const AvatarList = ({ users }) => {
  return (
    <View style={styles.avatarListContainer}>
      {users.map((user, index) => (
        <Avatar key={index} imageUrl={user.avatarUrl} />
      ))}
    </View>
  );
};
const styles = StyleSheet.create({
  avatarListContainer: {
    flexDirection: 'row',
    alignItems: 'center',
  },
  avatarContainer: {
    marginRight: 10,
  },
  avatarImage: {
    width: 50,
    height: 50,
    borderRadius: 25,
  },
  countBadge: {
    backgroundColor: 'blue',
    borderRadius: 12,
    padding: 4,
    position: 'absolute',
    bottom: 0,
    right: 0,
  },
});
export default AvatarList;

The last avatar should the count of rest users Can anyone help me any package or code can help me to achieve this


Solution

  • This is the code I've found:

    import { Text, SafeAreaView, StyleSheet,View } from 'react-native';
    
    export default function App() {
      return (
        <SafeAreaView style={styles.container}>
          <AvatarList/>
        </SafeAreaView>
      );
    }
    const Avatar = () => {
      return <View style={styles.avatar}></View>;
    };
    
    const AvatarList = () => {
      return (
        <View style={styles.avatarList}>
          <Avatar />
          <Avatar />
          <Avatar />
          <Avatar />
          <Avatar />
        </View>
      );
    };
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        justifyContent: 'center',
        backgroundColor: '#ecf0f1',
        padding: 8,
      },
    avatarList: {
        flexDirection: 'row',
        alignItems: 'center',
      },
      avatar: {
        height: 50,
        width: 50,
        marginRight: -20,
        backgroundColor: 'gray',
        borderRadius: 25,
        borderWidth: 2,
        borderColor: 'white',
        shadowColor: '#999',
        shadowOffset: { width: 0, height: 8 },
        shadowOpacity: 0.3,
        shadowRadius: 15,
      },
    });
    

    You can make the last avatar a number of remaining array