Menu
CoddyTech

音乐播放器:控制按钮

本课属于 Coddy Flutter 学习旅程的“适配不同屏幕的布局”单元。第 29 课,共 30 课。

最后一行放置按钮:上一首、播放和下一首。无论在什么手机上,它们都应均匀分配宽度,播放按钮正好位于中间。

将每个 IconButton 包在 Expanded 中,就能让每个按钮占据行宽的三分之一,并位于各自区域的中间。把播放按钮的 iconSize: 设得大一些,让它更醒目。

播放按钮会切换显示内容:歌曲播放时显示 Icons.pause,未播放时显示 Icons.play_arrow。这需要在状态中使用一个 bool,并通过 ? : 来选择图标。

challenge icon

挑战

简单

在时间信息下方,添加一个 SizedBox(height: 16),然后添加一个包含三个 Expanded IconButton 控件的 Row:

  • Icons.skip_previous,iconSize: 40
  • 播放按钮,iconSize: 64:当新增字段 bool _playing = false; 为 true 时显示 Icons.pause,为 false 时显示 Icons.play_arrow;点击按钮时,在 setState 中切换 _playing 的值
  • Icons.skip_next,iconSize: 40

亲手试试

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;

  @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'),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}
quiz icon自我测试

这节课包含一个小测验。开始学习即可作答,并记录你的进度。

适配不同屏幕的布局中的所有课

自己练习: Flutter 在线编译器