Menu
CoddyTech

Müzik Çalar: Açık ve Koyu Tema

Coddy'nin Flutter Journey'sinin Her Ekrana Uyan Düzenler bölümünün bir parçası. Ders 30 / 30.

İnsanlar geceleri müzik dinler, bu yüzden oynatıcıya koyu mod eklenir ve bu modu değiştirmek için üst çubukta bir düğme bulunur.

Bir AppBar, sağ ucunda gösterilen düğmelerin listesi olan actions: parametresini alır. Buradaki bir IconButton, durumdaki bir bool değerini değiştirir ve themeMode: bu değeri izler. Düğmenin simgesi ne yaptığını gösterir: oynatıcı açık temadayken bir ay, Icons.dark_mode; koyu temadayken ise bir güneş, Icons.light_mode.

Temadan gelen ekrandaki tüm renkler temayla birlikte değişir: arka plan, metin, kaydırıcı ve düğmeler. Albüm kapağıysa bir resim gibi kendi rengini korur.

challenge icon

Görev

Kolay

Koyu modu ekle:

  • bir alan: bool _dark = false;
  • Colors.deepPurple başlangıç değerini kullanan bir darkTheme: ve brightness: Brightness.dark; ayrıca _dark tarafından seçilen bir themeMode:
  • AppBar içindeki actions: bölümüne, koyu moddayken Icons.light_mode, açık moddayken Icons.dark_mode gösteren ve setState içinde _dark değerini tersine çeviren bir IconButton ekle

Kendin dene

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 iconKendini test et

Bu ders kısa bir quiz içerir. Soruları yanıtlamak ve ilerlemeni kaydetmek için derse başla.

Her Ekrana Uyan Düzenler bölümündeki tüm dersler

Kendi başına pratik yap: Online Flutter derleyicisi