Cours React Native · Module 1: Ta première application
PROJET : Carte de citoyen d'Atlantide
Dans cette leçon4
Il est temps de pratiquer ! Créons une carte de citoyen d'Atlantide interactive, en combinant tous les composants que nous avons appris.
Objectif du projet
Nous allons créer un écran de profil avec :
- Photo de profil
- Données du citoyen
- Statistiques
- Boutons d'action
Implémentation
import {
SafeAreaView,
ScrollView,
View,
Text,
Image,
Pressable,
StyleSheet,
Alert
} from 'react-native';
import { useState } from 'react';
interface Citizen {
name: string;
title: string;
avatar: string;
level: number;
guild: string;
stats: {
treasures: number;
expeditions: number;
reputation: number;
};
skills: string[];
}
export default function CitizenCard() {
const [citizen] = useState<Citizen>({
name: 'Nérée Océanborn',
title: 'Gardien des Profondeurs',
avatar: 'https://i.pravatar.cc/150?img=68',
level: 42,
guild: 'Ordre du Trident',
stats: {
treasures: 1247,
expeditions: 89,
reputation: 9450,
},
skills: ['Maîtrise du trident', 'Contrôle des courants', 'Langage des mers', 'Navigation'],
});
const [isFollowing, setIsFollowing] = useState(false);
const handleFollow = () => {
setIsFollowing(!isFollowing);
Alert.alert(
isFollowing ? 'Ne suit plus' : 'Suivi !',
isFollowing
? `Tu ne suis plus ${citizen.name}`
: `Tu suis désormais ${citizen.name}`
);
};
const handleMessage = () => {
Alert.alert('Message', `Envoyer un message à ${citizen.name}`);
};
return (
<SafeAreaView style={styles.container}>
<ScrollView contentContainerStyle={styles.scrollContent}>
{/* En-tête avec dégradé */}
<View style={styles.header}>
<View style={styles.levelBadge}>
<Text style={styles.levelText}>Niv {citizen.level}</Text>
</View>
</View>
{/* Avatar */}
<View style={styles.avatarContainer}>
<Image
source={{ uri: citizen.avatar }}
style={styles.avatar}
/>
<View style={styles.statusDot} />
</View>
{/* Informations de base */}
<View style={styles.infoSection}>
<Text style={styles.name}>{citizen.name}</Text>
<Text style={styles.title}>{citizen.title}</Text>
<View style={styles.guildBadge}>
<Text style={styles.guildText}>{citizen.guild}</Text>
</View>
</View>
{/* Statistiques */}
<View style={styles.statsContainer}>
<View style={styles.statItem}>
<Text style={styles.statValue}>{citizen.stats.treasures}</Text>
<Text style={styles.statLabel}>Trésors</Text>
</View>
<View style={styles.statDivider} />
<View style={styles.statItem}>
<Text style={styles.statValue}>{citizen.stats.expeditions}</Text>
<Text style={styles.statLabel}>Expéditions</Text>
</View>
<View style={styles.statDivider} />
<View style={styles.statItem}>
<Text style={styles.statValue}>{citizen.stats.reputation}</Text>
<Text style={styles.statLabel}>Réputation</Text>
</View>
</View>
{/* Compétences */}
<View style={styles.skillsSection}>
<Text style={styles.sectionTitle}>Compétences</Text>
<View style={styles.skillsGrid}>
{citizen.skills.map((skill, index) => (
<View key={index} style={styles.skillChip}>
<Text style={styles.skillText}>{skill}</Text>
</View>
))}
</View>
</View>
{/* Boutons d'action */}
<View style={styles.actionsContainer}>
<Pressable
style={({ pressed }) => [
styles.actionButton,
isFollowing ? styles.followingButton : styles.followButton,
pressed && styles.buttonPressed,
]}
onPress={handleFollow}
>
<Text style={[
styles.actionButtonText,
isFollowing && styles.followingButtonText
]}>
{isFollowing ? 'Suivi' : '+ Suivre'}
</Text>
</Pressable>
<Pressable
style={({ pressed }) => [
styles.actionButton,
styles.messageButton,
pressed && styles.buttonPressed,
]}
onPress={handleMessage}
>
<Text style={styles.actionButtonText}>Message</Text>
</Pressable>
</View>
{/* Bio */}
<View style={styles.bioSection}>
<Text style={styles.sectionTitle}>À propos de moi</Text>
<Text style={styles.bioText}>
Gardien des Profondeurs depuis 15 ans. Je me spécialise dans l'exploration
des ruines englouties et la protection des trésors de l'Atlantide. Je recherche des compagnons
pour les prochaines expéditions vers les recoins inconnus de l'océan.
</Text>
</View>
</ScrollView>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#1a5276',
},
scrollContent: {
paddingBottom: 30,
},
header: {
height: 120,
backgroundColor: '#2980b9',
borderBottomLeftRadius: 30,
borderBottomRightRadius: 30,
},
levelBadge: {
position: 'absolute',
top: 16,
right: 16,
backgroundColor: '#f39c12',
paddingHorizontal: 12,
paddingVertical: 6,
borderRadius: 12,
},
levelText: {
color: '#fff',
fontWeight: 'bold',
fontSize: 14,
},
avatarContainer: {
alignItems: 'center',
marginTop: -50,
},
avatar: {
width: 120,
height: 120,
borderRadius: 60,
borderWidth: 4,
borderColor: '#1a5276',
},
statusDot: {
position: 'absolute',
bottom: 8,
right: '35%',
width: 20,
height: 20,
borderRadius: 10,
backgroundColor: '#2ecc71',
borderWidth: 3,
borderColor: '#1a5276',
},
infoSection: {
alignItems: 'center',
marginTop: 12,
},
name: {
fontSize: 24,
fontWeight: 'bold',
color: '#fff',
},
title: {
fontSize: 16,
color: '#aed6f1',
marginTop: 4,
},
guildBadge: {
marginTop: 12,
backgroundColor: '#34495e',
paddingHorizontal: 16,
paddingVertical: 8,
borderRadius: 20,
},
guildText: {
color: '#f1c40f',
fontWeight: '600',
fontSize: 14,
},
statsContainer: {
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
marginTop: 24,
marginHorizontal: 20,
backgroundColor: '#2c3e50',
borderRadius: 16,
paddingVertical: 20,
},
statItem: {
flex: 1,
alignItems: 'center',
},
statValue: {
fontSize: 22,
fontWeight: 'bold',
color: '#fff',
},
statLabel: {
fontSize: 12,
color: '#95a5a6',
marginTop: 4,
},
statDivider: {
width: 1,
height: 40,
backgroundColor: '#34495e',
},
skillsSection: {
marginTop: 24,
paddingHorizontal: 20,
},
sectionTitle: {
fontSize: 18,
fontWeight: 'bold',
color: '#fff',
marginBottom: 12,
},
skillsGrid: {
flexDirection: 'row',
flexWrap: 'wrap',
gap: 8,
},
skillChip: {
backgroundColor: '#34495e',
paddingHorizontal: 12,
paddingVertical: 8,
borderRadius: 16,
},
skillText: {
color: '#fff',
fontSize: 14,
},
actionsContainer: {
flexDirection: 'row',
gap: 12,
marginTop: 24,
paddingHorizontal: 20,
},
actionButton: {
flex: 1,
paddingVertical: 14,
borderRadius: 12,
alignItems: 'center',
},
followButton: {
backgroundColor: '#3498db',
},
followingButton: {
backgroundColor: 'transparent',
borderWidth: 2,
borderColor: '#3498db',
},
messageButton: {
backgroundColor: '#27ae60',
},
buttonPressed: {
opacity: 0.8,
transform: [{ scale: 0.98 }],
},
actionButtonText: {
color: '#fff',
fontSize: 16,
fontWeight: '600',
},
followingButtonText: {
color: '#3498db',
},
bioSection: {
marginTop: 24,
paddingHorizontal: 20,
},
bioText: {
fontSize: 15,
color: '#bdc3c7',
lineHeight: 22,
},
});Qu'est-ce que le projet inclut ?
- Composition de composants - View, Text, Image, ScrollView, Pressable
- Gestion de l'état - useState pour gérer le suivi
- Styles conditionnels - modifier l'apparence du bouton de suivi
- Réactivité - flexDirection, flex, gap
- Interaction - Alert, onPress
- TypeScript - interface pour les données
Exercices à faire par toi-même
- Ajoute la possibilité de modifier la bio
- Implémente une animation lors du clic sur l'avatar
- Ajoute une section « Expéditions récentes » avec une liste
- Crée une modale avec les statistiques complètes
Félicitations ! Tu as terminé le premier module en Atlantide !
Entrées de la documentation utilisées dans cette leçon
Tu as repéré une erreur dans cette leçon ?
Teste tes connaissances
Réponds aux questions de cette leçon. Choisis une réponse pour voir tout de suite si elle est correcte.
1. Quel est le flexDirection par défaut en React Native ?
Tâches pratiques dans le jeu
- Éditeur de code
Complète les styles pour View
- Éditeur de code
Complète le composant Pressable