Kurs React Native · Moduł 1: Pierwsza aplikacja

PROJEKT: Karta Mieszkańca Atlantydy

4 min czytania ·
W tej lekcji4

Czas na praktykę! Stwórzmy interaktywną kartę mieszkańca Atlantydy, łącząc wszystkie poznane komponenty.

Cel projektu

Stworzymy ekran profilu z:

  • Zdjęciem profilowym
  • Danymi mieszkańca
  • Statystykami
  • Przyciskami akcji

Implementacja

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: 'Nereus Oceanborn',
    title: 'Strażnik Głębin',
    avatar: 'https://i.pravatar.cc/150?img=68',
    level: 42,
    guild: 'Zakon Trójzęba',
    stats: {
      treasures: 1247,
      expeditions: 89,
      reputation: 9450,
    },
    skills: ['Władanie trójzębem', 'Kontrola prądów', 'Mowa morska', 'Nawigacja'],
  });

  const [isFollowing, setIsFollowing] = useState(false);

  const handleFollow = () => {
    setIsFollowing(!isFollowing);
    Alert.alert(
      isFollowing ? 'Przestałeś obserwować' : 'Obserwujesz!',
      isFollowing
        ? `Nie obserwujesz już ${citizen.name}`
        : `Teraz obserwujesz ${citizen.name}`
    );
  };

  const handleMessage = () => {
    Alert.alert('Wiadomość', `Wyślij wiadomość do ${citizen.name}`);
  };

  return (
    <SafeAreaView style={styles.container}>
      <ScrollView contentContainerStyle={styles.scrollContent}>
        {/* Header z gradientem */}
        <View style={styles.header}>
          <View style={styles.levelBadge}>
            <Text style={styles.levelText}>Lvl {citizen.level}</Text>
          </View>
        </View>

        {/* Avatar */}
        <View style={styles.avatarContainer}>
          <Image
            source={{ uri: citizen.avatar }}
            style={styles.avatar}
          />
          <View style={styles.statusDot} />
        </View>

        {/* Informacje podstawowe */}
        <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>

        {/* Statystyki */}
        <View style={styles.statsContainer}>
          <View style={styles.statItem}>
            <Text style={styles.statValue}>{citizen.stats.treasures}</Text>
            <Text style={styles.statLabel}>Skarby</Text>
          </View>
          <View style={styles.statDivider} />
          <View style={styles.statItem}>
            <Text style={styles.statValue}>{citizen.stats.expeditions}</Text>
            <Text style={styles.statLabel}>Wyprawy</Text>
          </View>
          <View style={styles.statDivider} />
          <View style={styles.statItem}>
            <Text style={styles.statValue}>{citizen.stats.reputation}</Text>
            <Text style={styles.statLabel}>Reputacja</Text>
          </View>
        </View>

        {/* Umiejętności */}
        <View style={styles.skillsSection}>
          <Text style={styles.sectionTitle}>Umiejętności</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>

        {/* Przyciski akcji */}
        <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 ? 'Obserwujesz' : '+ Obserwuj'}
            </Text>
          </Pressable>

          <Pressable
            style={({ pressed }) => [
              styles.actionButton,
              styles.messageButton,
              pressed && styles.buttonPressed,
            ]}
            onPress={handleMessage}
          >
            <Text style={styles.actionButtonText}>Wiadomość</Text>
          </Pressable>
        </View>

        {/* Bio */}
        <View style={styles.bioSection}>
          <Text style={styles.sectionTitle}>O mnie</Text>
          <Text style={styles.bioText}>
            Strażnik Głębin od 15 lat. Specjalizuję się w eksploracji
            zatopionychruinn i ochronie skarbów Atlantydy. Szukam towarzyszy
            do kolejnych wypraw w nieznane zakątki oceanu.
          </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,
  },
});

Co zawiera projekt?

  1. Kompozycja komponentów - View, Text, Image, ScrollView, Pressable
  2. State management - useState dla obsługi follow
  3. Warunkowe style - zmiana wyglądu przycisku follow
  4. Responsywność - flexDirection, flex, gap
  5. Interakcja - Alert, onPress
  6. TypeScript - interface dla danych

Zadania do samodzielnego wykonania

  1. Dodaj możliwość edycji bio
  2. Zaimplementuj animację przy kliknięciu w avatar
  3. Dodaj sekcję "Ostatnie wyprawy" z listą
  4. Stwórz modal z pełnymi statystykami

Gratulacje! Ukończyłeś/aś pierwszy moduł w Atlantydzie!

Hasła z dokumentacji w tej lekcji

Widzisz błąd w tej lekcji?

Sprawdź się

Odpowiedz na pytania z tej lekcji. Wybierz odpowiedź, a od razu zobaczysz, czy jest poprawna.

  1. 1. Jaki jest domyślny flexDirection w React Native?

Zadania praktyczne w grze

  • Edytor kodu

    Uzupełnij style dla View

  • Edytor kodu

    Uzupełnij komponent Pressable

  1. Zadanie z kodemUzupełnij: Komponent View ze stylami
  2. Zadanie z kodemUzupełnij: Przycisk Pressable
  3. PytanieQuiz podsumowujący: Flexbox w RN
  4. Projekt do wysłaniaWyślij projekt do mentora
  5. WideoPodsumowanie modułu

Przydatne artykuły