๐ŸŒฑSeSac Flutter ๊ฐ•์˜_7/31 : Data, LifeCycle

KiKi ManGoยท2026๋…„ 8์›” 10์ผ

SeSac_DartFlutter

๋ชฉ๋ก ๋ณด๊ธฐ
12/18

โœ”๏ธํ•™์Šต๋‚ด์šฉ ์š”์•ฝ

1. Data Class ๊ตฌ์„ฑ

Data Class

  • ์šด๋™ ๋ฐ์ดํ„ฐ๋ฅผ ๊ตฌ์กฐํ™”ํ•˜๊ธฐ ์œ„ํ•ด workout.dart ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ  Workout ํด๋ž˜์Šค ์ •์˜
  • ์šด๋™ ์ด๋ฆ„, ์‹œ๊ฐ„, ์ด๋ฏธ์ง€, ์˜ค๋””์˜ค, ์†Œ๋ชจ ์นผ๋กœ๋ฆฌ๋ฅผ ํ•˜๋‚˜์˜ ๊ฐ์ฒด๋กœ ๊ด€๋ฆฌ
// filename: workout.dart

class Workout {
  String name;
  int minutes;
  String imageName;
  String audioName;
  int kcal;

  Workout({
    required this.name,
    required this.minutes,
    required this.imageName,
    required this.audioName,
    required this.kcal,
  });
}

WorkoutListPage Workout class ์ ์šฉ

๐Ÿ”นWorkout ํด๋ž˜์Šค ์ ์šฉ

  • ์šด๋™ ์ •๋ณด๋ฅผ Workout ๊ฐ์ฒด๋กœ ์ƒ์„ฑํ•˜๊ณ  ๋ฆฌ์ŠคํŠธ๋กœ ๊ด€๋ฆฌ
  • ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด ๊ฐ์ฒด์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์šด๋™ ๋ชฉ๋ก์— ์ถœ๋ ฅ

๐Ÿ”น์šด๋™ ๋ชฉ๋ก UI ๊ตฌ์„ฑ

  • SingleChildScrollView์™€ Column์„ ์‚ฌ์šฉํ•˜์—ฌ ์„ธ๋กœ ์Šคํฌ๋กค ๋ชฉ๋ก ๊ตฌํ˜„
  • ์šด๋™ ์ด๋ฏธ์ง€, ์ด๋ฆ„, ์‹œ๊ฐ„์„ Row๋กœ ๋ฐฐ์น˜

๐Ÿ”นGestureDetector ์ ์šฉ

  • ๊ฐ ์šด๋™ ํ•ญ๋ชฉ์„ GestureDetector๋กœ ๊ฐ์‹ธ ํ„ฐ์น˜ํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ตฌ์„ฑ
  • onTap์„ ํ†ตํ•ด ์šด๋™ ํ•ญ๋ชฉ ์„ ํƒ ์‹œ ํ™”๋ฉด ์ด๋™ ๋“ฑ์˜ ๋™์ž‘์„ ์‹คํ–‰

WorkoutListPage ์ „์ฒด code


//workout_list_page.dart 
import 'package:flutter/material.dart';

import 'workout.dart';

class WorkoutListPage extends StatelessWidget {
  WorkoutListPage({super.key});

  List<Workout> workouts=[
    Workout(name: '์Šค์ฟผํŠธ',imageName: 'squat.png', minutes: 30, audioName: 'squat.mp3', kcal: 200),
    Workout(name: '์œ—๋ชธ ์ผ์œผํ‚ค๊ธฐ', minutes: 20, imageName: 'sit_up.png', audioName: 'sit_up.mp3', kcal: 100),
    Workout(name: '๋งˆ์šดํ‹ด ํด๋ฆผ๋ฒ„', minutes: 15, imageName: 'mountain_climber.png', audioName: 'mountain_climber.mp3', kcal: 50),
    Workout(name: '์‚ฌ์ด๋“œ ๋Ÿฐ์ง€', minutes: 20, imageName: 'side_lunge.png', audioName: 'side_lunge.mp3', kcal: 100),
    Workout(name: 'ํ‘ธ์‹œ์—…', minutes: 15, imageName: 'push_up.png', audioName: 'push_up.mp3', kcal: 100),
    Workout(name: '๋ฉํ‚ค ํ‚ฅ', minutes: 30, imageName: 'donkey_kick.png', audioName: 'donkey_kick.mp3', kcal: 50),
    Workout(name: '์‚ฌ์ด๋“œ ํ”Œ๋žญํฌ', minutes: 25, imageName: 'side_plank.png', audioName: 'side_plank.mp3', kcal: 120),
    Workout(name: '๋ฆฌ๋ฒ„์Šค ํ”Œ๋žญํฌ', minutes: 25, imageName: 'reverse_plank.png', audioName: 'reverse_plank.mp3', kcal: 120),
    Workout(name: 'ํž™ ๋ธŒ๋ฆฟ์ง€', minutes: 20, imageName: 'hip_bridge.png', audioName: 'hip_brdige.mp3', kcal: 80),
    Workout(name: '์–ด๊นจ ์ŠคํŠธ๋ ˆ์นญ', minutes: 10, imageName: 'shoulder_stretch.png', audioName: 'shoulder_stretch.mp3', kcal: 30),
    Workout(name: 'ํ–„์ŠคํŠธ๋ง ์ŠคํŠธ๋ ˆ์นญ', minutes: 10, imageName: 'hamstring_stretch.png', audioName: 'hamstring_stretch.mp3', kcal: 30),

  ];

  List<Widget> getWorkoutList() {
    List<Widget> result=[];
    for(int i=0;i< workouts.length; i++){
      var name=workouts[i].name;
      var minutes=workouts[i].minutes;
      var image=workouts[i].imageName;
      Widget sample=GestureDetector(
        onTap:(){},
        child: Row(
          spacing: 20,
          children: [
            Container(
              width: 100,
              height: 100,
              decoration: BoxDecoration(
                image: DecorationImage(
                  image: AssetImage('assets/$image'),
                  fit: BoxFit.cover,
                ),
                shape: BoxShape.circle,
              ),
            ),
            Expanded(
              child: Text(
                '${i+1}.$name', style: TextStyle(fontSize:20),
              ),
            ),
            Text(
              '$minutes', style: TextStyle(fontSize:20, color:Colors.blue),
            ),
          ],
        ),
      );
      result.add(sample);
    }
    //###############################

    return result;
  }

  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar:AppBar(
        title: Text('WorkoutList'),
        centerTitle: true,
      ),
      body: SingleChildScrollView(
        child: Column(
          children:getWorkoutList(),
        ),
      ),
    );
  }


}

List data ์ •์˜ - late ํ™œ์šฉํ•œ ์ดˆ๊ธฐํ™”

๐Ÿ”น late ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

  • late๋Š” ๋ณ€์ˆ˜๋ฅผ ๋จผ์ € ์„ ์–ธํ•˜๊ณ  ๊ฐ’์„ ๋‚˜์ค‘์— ์ดˆ๊ธฐํ™”ํ•  ๋•Œ ์‚ฌ์šฉ
  • ํ˜„์žฌ ์šด๋™ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๋Š” currentWorkout์€ workouts ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ค€๋น„๋œ ์ดํ›„์— ์ดˆ๊ธฐํ™”ํ•ด์•ผ ํ•˜๋ฏ€๋กœ late๋กœ ์„ ์–ธ
    ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์˜ ์ดˆ๊ธฐํ™” ๋‹จ๊ณ„์—์„œ๋Š” ๋‹ค๋ฅธ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์ธ workouts๋ฅผ ์ง์ ‘ ์ฐธ์กฐํ•  ์ˆ˜ ์—†์Œ
late Workout currentWorkout;
int workoutIndex = 0;
List<Workout> workouts = WorkoutManager.workouts;

๐Ÿ”น initState ํ•จ์ˆ˜์—์„œ ์ดˆ๊ธฐํ™”

  • initState()๋Š” State ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ ์ตœ์ดˆ ํ•œ ๋ฒˆ ์‹คํ–‰๋˜๋Š” ์ƒ๋ช…์ฃผ๊ธฐ ๋ฉ”์„œ๋“œ
  • ์ด์ „ ํ™”๋ฉด์—์„œ ์ „๋‹ฌ๋ฐ›์€ widget.workoutsIndex๋ฅผ ํ˜„์žฌ ์šด๋™์˜ ์ธ๋ฑ์Šค๋กœ ์„ค์ •
  • ํ•ด๋‹น ์ธ๋ฑ์Šค์˜ ์šด๋™ ์ •๋ณด๋ฅผ currentWorkout์— ์ €์žฅ

void initState() {
  super.initState();

  workoutIndex = widget.workoutsIndex;
  currentWorkout = workouts[workoutIndex];
}

Prev / Next ๊ธฐ๋Šฅ๊ตฌํ˜„

  • ํ˜„์žฌ ์šด๋™์˜ ์œ„์น˜๋Š” workoutIndex, ํ˜„์žฌ ํ™”๋ฉด์— ํ‘œ์‹œํ•  ์šด๋™์€ currentWorkout์œผ๋กœ ๊ด€๋ฆฌ
  • ๋ชฉ๋ก์˜ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋ฉด ๋ฐ˜๋Œ€์ชฝ ๋์œผ๋กœ ์ด๋™ํ•˜๋„๋ก ์ˆœํ™˜ ๊ตฌ์กฐ๋กœ ๊ตฌํ˜„
//workout_guide_page.dart 
void prevWorkout() {
  workoutIndex--;

  // ์ฒซ ๋ฒˆ์งธ ์šด๋™์—์„œ ์ด์ „ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋งˆ์ง€๋ง‰ ์šด๋™์œผ๋กœ ์ด๋™
  if (workoutIndex < 0) {
    workoutIndex = workouts.length - 1;
  }

  currentWorkout = workouts[workoutIndex];
}

void nextWorkout() {
  workoutIndex++;

  // ๋งˆ์ง€๋ง‰ ์šด๋™์—์„œ ๋‹ค์Œ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์ฒซ ๋ฒˆ์งธ ์šด๋™์œผ๋กœ ์ด๋™
  if (workoutIndex >= workouts.length) {
    workoutIndex = 0;
  }

  currentWorkout = workouts[workoutIndex];
}

๐Ÿ”น ๋ฒ„ํŠผ์— Prev / Next ํ•จ์ˆ˜ ์—ฐ๊ฒฐ

  • ์ด์ „ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด prevWorkout() ์‹คํ–‰, ๋‹ค์Œ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด nextWorkout() ์‹คํ–‰
  • setState()๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋ณ€๊ฒฝ๋œ currentWorkout์„ ํ™”๋ฉด์— ๋ฐ˜์˜
//workout_guide_page.dart 
Row(
  children: [
    IconButton(
      onPressed: () {
        setState(() {
          prevWorkout();
        });
      },
      icon: const Icon(Icons.arrow_back_ios),
      iconSize: 70,
    ),
    Expanded(
      child: Image.asset(
        'assets/${currentWorkout.imageName}',
      ),
    ),
    IconButton(
      onPressed: () {
        setState(() {
          nextWorkout();
        });
      },
      icon: const Icon(Icons.arrow_forward_ios),
      iconSize: 70,
    ),
  ],
)

2. StatefulWidget LifeCycle

  • StatefulWidget์€ ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋˜๋Š” ํ™”๋ฉด์„ ๋งŒ๋“ค ๋•Œ ์‚ฌ์šฉ
  • WorkoutGuidePage์—์„œ๋Š” ํ˜„์žฌ ์šด๋™ ๋ณ€๊ฒฝ, ํ™”๋ฉด ๊ฐฑ์‹ , ์˜ค๋””์˜ค ๋ฆฌ์†Œ์Šค ํ•ด์ œ ๊ณผ์ •์—์„œ ์ƒ๋ช…์ฃผ๊ธฐ ๋ฉ”์„œ๋“œ ํ™œ์šฉ

StatefulWidget์˜ ์ „์ฒด lifecycle

  • createState(): StatefulWidget๊ณผ ์—ฐ๊ฒฐํ•  State ๊ฐ์ฒด ์ƒ์„ฑ
  • initState(): State ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋œ ํ›„ ์ตœ์ดˆ ํ•œ ๋ฒˆ ์‹คํ–‰
  • didChangeDependencies(): ์ƒ์œ„ ์œ„์ ฏ์˜ ์˜์กด์„ฑ์ด ๋ณ€๊ฒฝ๋  ๋•Œ ์‹คํ–‰
  • build(): ํ˜„์žฌ ์ƒํƒœ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ™”๋ฉด ๊ตฌ์„ฑ
  • setState(): ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ์Œ์„ ์•Œ๋ฆฌ๊ณ  build()๋ฅผ ๋‹ค์‹œ ์‹คํ–‰
  • deactivate(): ์œ„์ ฏ์ด ์œ„์ ฏ ํŠธ๋ฆฌ์—์„œ ์ผ์‹œ์ ์œผ๋กœ ์ œ๊ฑฐ๋  ๋•Œ ์‹คํ–‰
  • dispose(): ์œ„์ ฏ์ด ์™„์ „ํžˆ ์ œ๊ฑฐ๋  ๋•Œ ์‚ฌ์šฉํ•˜๋˜ ๋ฆฌ์†Œ์Šค ์ •๋ฆฌ
createState()
    โ†“
initState()
    โ†“
didChangeDependencies()
    โ†“
build()
    โ†“
setState() โ†’ build() ์žฌ์‹คํ–‰
    โ†“
deactivate()
    โ†“
dispose()

controller dispose

  • player.dispose()๋กœ ์˜ค๋””์˜ค ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ์‚ฌ์šฉํ•˜๋˜ ๋ฆฌ์†Œ์Šค ํ•ด์ œ
  • ๋ถˆํ•„์š”ํ•œ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๊ณผ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ๋™์ž‘ ๋ฐฉ์ง€
  • ๋ฆฌ์†Œ์Šค๋ฅผ ๋จผ์ € ์ •๋ฆฌํ•œ ํ›„ ๋งˆ์ง€๋ง‰์— super.dispose() ํ˜ธ์ถœ

void dispose() {
  player.dispose();
  super.dispose();
}

๐Ÿ“์ˆ˜๊ฐ•ํ›„๊ธฐ

  • StatefulWidget์˜ ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ์‹ค์ œ ์ฝ”๋“œ๋ž‘ ์—ฐ๊ฒฐํ•˜๋ฉด์„œ ๊ฐ ์—ญํ• ์„ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.
  • late๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ณ€์ˆ˜์˜ ์„ ์–ธ๊ณผ ์ดˆ๊ธฐํ™” ์‹œ์ ์„ ๋ถ„๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์› ๋‹ค.
  • ์šด๋™ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ์ฒด์™€ ๋ฆฌ์ŠคํŠธ๋กœ ๊ด€๋ฆฌํ•˜๋ฉด์„œ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐํ™”์˜ ํ•„์š”์„ฑ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค.
  • ์ด์ „ยท๋‹ค์Œ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๋ฉฐ ์ธ๋ฑ์Šค๋ฅผ ํ™œ์šฉํ•œ ๋ฐ์ดํ„ฐ ์ „ํ™˜๊ณผ ํ™”๋ฉด ๊ฐฑ์‹  ๊ณผ์ •์„ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.
  • AudioPlayer์™€ ๊ฐ™์€ ๊ฐ์ฒด๋Š” ์‚ฌ์šฉ์ด ๋๋‚œ ํ›„ dispose()๋กœ ๋ฆฌ์†Œ์Šค๋ฅผ ์ •๋ฆฌํ•ด์•ผ ํ•œ๋‹ค๋Š” ์ ์„ ๋ฐฐ์› ๋‹ค.
  • ์ฝ”๋“œ๊ฐ€ ์ ์ฐจ ๊ตฌ์กฐ์ ์œผ๋กœ ๋ณ€ํ™”ํ•˜๊ณ  ์žˆ์–ด์„œ ๋ฟŒ๋“ฏํ•˜๊ณ , flutter์™€ dart๋ฅผ ๊นŠ์ด ์ดํ•ดํ•˜๋Š” ๊ฑฐ ๊ฐ™์•„์„œ ๊ธฐ์˜๋‹ค.
profile
๐Ÿฅญ ํญ๋„“์€ ๊ฒฝํ—˜์„ ์”จ์•— ์‚ผ์•„, ๊นŠ์ด๋ฅผ ๋‹ค์ง€๋ฉฐ ์ง€๊ธˆ ์ต์–ด๊ฐ€๋Š” ์ค‘

0๊ฐœ์˜ ๋Œ“๊ธ€