โ๏ธํ์ต๋ด์ฉ ์์ฝ
1. Data Class ๊ตฌ์ฑ
Data Class
- ์ด๋ ๋ฐ์ดํฐ๋ฅผ ๊ตฌ์กฐํํ๊ธฐ ์ํด
workout.dart ํ์ผ์ ์์ฑํ๊ณ Workout ํด๋์ค ์ ์
- ์ด๋ ์ด๋ฆ, ์๊ฐ, ์ด๋ฏธ์ง, ์ค๋์ค, ์๋ชจ ์นผ๋ก๋ฆฌ๋ฅผ ํ๋์ ๊ฐ์ฒด๋ก ๊ด๋ฆฌ
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
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;
}
@override
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์ ์ ์ฅ
@override
void initState() {
super.initState();
workoutIndex = widget.workoutsIndex;
currentWorkout = workouts[workoutIndex];
}
Prev / Next ๊ธฐ๋ฅ๊ตฌํ
- ํ์ฌ ์ด๋์ ์์น๋
workoutIndex, ํ์ฌ ํ๋ฉด์ ํ์ํ ์ด๋์ currentWorkout์ผ๋ก ๊ด๋ฆฌ
- ๋ชฉ๋ก์ ๋ฒ์๋ฅผ ๋ฒ์ด๋๋ฉด ๋ฐ๋์ชฝ ๋์ผ๋ก ์ด๋ํ๋๋ก ์ํ ๊ตฌ์กฐ๋ก ๊ตฌํ
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์ ํ๋ฉด์ ๋ฐ์
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,
),
],
)
StatefulWidget์ ์ํ๊ฐ ๋ณ๊ฒฝ๋๋ ํ๋ฉด์ ๋ง๋ค ๋ ์ฌ์ฉ
WorkoutGuidePage์์๋ ํ์ฌ ์ด๋ ๋ณ๊ฒฝ, ํ๋ฉด ๊ฐฑ์ , ์ค๋์ค ๋ฆฌ์์ค ํด์ ๊ณผ์ ์์ ์๋ช
์ฃผ๊ธฐ ๋ฉ์๋ ํ์ฉ
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() ํธ์ถ
@override
void dispose() {
player.dispose();
super.dispose();
}
๐์๊ฐํ๊ธฐ
StatefulWidget์ ์๋ช
์ฃผ๊ธฐ๋ฅผ ์ค์ ์ฝ๋๋ ์ฐ๊ฒฐํ๋ฉด์ ๊ฐ ์ญํ ์ ์ดํดํ ์ ์์๋ค.
late๋ฅผ ํ์ฉํ์ฌ ๋ณ์์ ์ ์ธ๊ณผ ์ด๊ธฐํ ์์ ์ ๋ถ๋ฆฌํ๋ ๋ฐฉ๋ฒ์ ๋ฐฐ์ ๋ค.
- ์ด๋ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ฒด์ ๋ฆฌ์คํธ๋ก ๊ด๋ฆฌํ๋ฉด์ ๋ฐ์ดํฐ ๊ตฌ์กฐํ์ ํ์์ฑ์ ์๊ฒ ๋์๋ค.
- ์ด์ ยท๋ค์ ๊ธฐ๋ฅ์ ๊ตฌํํ๋ฉฐ ์ธ๋ฑ์ค๋ฅผ ํ์ฉํ ๋ฐ์ดํฐ ์ ํ๊ณผ ํ๋ฉด ๊ฐฑ์ ๊ณผ์ ์ ์ดํดํ ์ ์์๋ค.
AudioPlayer์ ๊ฐ์ ๊ฐ์ฒด๋ ์ฌ์ฉ์ด ๋๋ ํ dispose()๋ก ๋ฆฌ์์ค๋ฅผ ์ ๋ฆฌํด์ผ ํ๋ค๋ ์ ์ ๋ฐฐ์ ๋ค.
- ์ฝ๋๊ฐ ์ ์ฐจ ๊ตฌ์กฐ์ ์ผ๋ก ๋ณํํ๊ณ ์์ด์ ๋ฟ๋ฏํ๊ณ , flutter์ dart๋ฅผ ๊น์ด ์ดํดํ๋ ๊ฑฐ ๊ฐ์์ ๊ธฐ์๋ค.