Music Player: Light and Dark
Part of the Layout That Fits section of Coddy's Flutter journey. Lesson 30 of 30.
People listen to music at night, so the player gets a dark mode, with a button in the top bar to switch it.
An AppBar takes actions:, a list of buttons shown at its right end. One IconButton there flips a bool in state, and themeMode: follows it. The button's icon says what it does: a moon, Icons.dark_mode, while the player is light, and a sun, Icons.light_mode, while it is dark.
Every color on the screen that comes from the theme changes with it: the background, the text, the slider and the buttons. The album art keeps its own color, as a picture would.
Challenge
EasyAdd dark mode:
- a field
bool _dark = false; - a
darkTheme:from the seedColors.deepPurplewithbrightness: Brightness.dark, andthemeMode:chosen by_dark - in the
AppBar'sactions:, anIconButtonshowingIcons.light_modewhile dark andIcons.dark_modewhile light, which flips_darkinsetState
Try it yourself
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: () {},
),
),
],
),
],
),
),
),
);
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Layout That Fits
1Sharing a Row
Expanded Fills the RowSharing Space With flexFlexible Hugs Its ChildSpacer Pushes Apart4Sizes and Breakpoints
Full Width: double.infinityA Maximum WidthScreen Size: MediaQueryLayoutBuilder Breakpoints7Project: Music Player
Music Player: Album ArtMusic Player: Song InfoPractice on your own: Online Flutter compiler