Menu
CoddyTech

뮤직 플레이어: 라이트 및 다크 모드

Coddy Flutter 여정의 화면 크기에 맞는 레이아웃 섹션에 포함된 레슨. 30개 중 30번째.

밤에도 음악을 듣기 때문에 플레이어에 다크 모드를 적용하고, 상단 바에 모드를 전환하는 버튼을 둡니다.

AppBar는 오른쪽 끝에 표시할 버튼 목록인 actions:를 받습니다. 그 안에 있는 IconButton 하나가 상태의 bool 값을 바꾸고, themeMode:는 그 값을 따릅니다. 버튼 아이콘은 버튼의 기능을 나타냅니다. 플레이어가 밝은 모드일 때는 달 모양인 Icons.dark_mode를, 어두운 모드일 때는 해 모양인 Icons.light_mode를 표시합니다.

테마에 따라 화면에서 테마로부터 가져오는 모든 색상이 바뀝니다. 배경, 텍스트, 슬라이더, 버튼의 색상이 바뀝니다. 앨범 아트는 사진처럼 고유한 색상을 유지합니다.

challenge icon

챌린지

쉬움

다크 모드를 추가하세요:

  • bool _dark = false; 필드
  • 시드 Colors.deepPurple를 사용하고 brightness: Brightness.dark를 지정한 darkTheme:, 그리고 _dark에 따라 선택되는 themeMode:
  • AppBar의 actions:에 IconButton을 추가하세요. 다크 모드일 때는 Icons.light_mode를, 라이트 모드일 때는 Icons.dark_mode를 표시하고, setState에서 _dark 값을 반전합니다.

직접 해보기

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  double _position = 0.3;
  bool _playing = false;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
      ),
      home: Scaffold(
        appBar: AppBar(title: const Text('Now Playing')),
        body: Padding(
          padding: const EdgeInsets.all(24),
          child: Column(
            children: [
              Stack(
                children: [
                  Container(
                    width: double.infinity,
                    height: 300,
                    decoration: BoxDecoration(
                      color: Colors.deepPurple,
                      borderRadius: BorderRadius.circular(24),
                    ),
                    child: const Center(
                      child: Icon(Icons.album, size: 120, color: Colors.white),
                    ),
                  ),
                  Positioned(
                    top: 12,
                    right: 12,
                    child: Container(
                      padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
                      decoration: BoxDecoration(
                        color: Colors.black54,
                        borderRadius: BorderRadius.circular(8),
                      ),
                      child: const Text(
                        'HD',
                        style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
                      ),
                    ),
                  ),
                ],
              ),
              const SizedBox(height: 24),
              const Text(
                'Midnight Drive',
                style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
              ),
              const SizedBox(height: 4),
              const Text('The Night Owls', style: TextStyle(color: Colors.grey)),
              const SizedBox(height: 16),
              Slider(
                value: _position,
                onChanged: (value) {
                  setState(() {
                    _position = value;
                  });
                },
              ),
              const Row(
                children: [
                  Text('1:12'),
                  Spacer(),
                  Text('3:45'),
                ],
              ),
              const SizedBox(height: 16),
              Row(
                children: [
                  Expanded(
                    child: IconButton(
                      iconSize: 40,
                      icon: const Icon(Icons.skip_previous),
                      onPressed: () {},
                    ),
                  ),
                  Expanded(
                    child: IconButton(
                      iconSize: 64,
                      icon: Icon(_playing ? Icons.pause : Icons.play_arrow),
                      onPressed: () {
                        setState(() {
                          _playing = !_playing;
                        });
                      },
                    ),
                  ),
                  Expanded(
                    child: IconButton(
                      iconSize: 40,
                      icon: const Icon(Icons.skip_next),
                      onPressed: () {},
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}
quiz icon실력 점검

이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.

화면 크기에 맞는 레이아웃의 모든 레슨

직접 연습해 보세요: 온라인 Flutter 컴파일러