音乐播放器:歌曲信息
本课属于 Coddy Flutter 学习旅程的“适配不同屏幕的布局”单元。第 27 课,共 30 课。
封面图下面是歌曲名和艺术家。就像名片上的名字一样,歌曲名应该是视线最先找到的那一行:字号大且醒目。艺术家的名字则是下面一行颜色较浅的灰色文字。
封面图面积大,标题面积小,所以它们之间需要留出空隙。这个空隙要比标题和艺术家名字之间的空隙更大,因为标题和艺术家名字属于一组。用间距把内容分组,屏幕就会更容易阅读。
挑战
简单在封面图下方添加:
- 一个
SizedBox(height: 24) Midnight Drive,字号为24并设为粗体- 一个
SizedBox(height: 4) - 使用
Colors.grey显示The Night Owls
亲手试试
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> {
@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),
),
),
),
],
),
],
),
),
),
);
}
}
这节课包含一个小测验。开始学习即可作答,并记录你的进度。
适配不同屏幕的布局中的所有课
自己练习: Flutter 在线编译器