本系列可能会伴随你们很长时间,这里我会从0开始搭建一个「网易云音乐」的APP出来。git
下面是该APP 功能的思惟导图:github
前期回顾:微信
每日推荐 | 推荐歌单 |
---|---|
![]() |
![]() |
本篇为第三篇,在这里咱们会搭建每日推荐、推荐歌单。网络
首先仍是再来看一下「每日推荐」的UI效果:less
看到这个效果,有经验的同窗可能直接就会喊出:CustomScrollView
!!ide
没错,当前页一共分为三部分:flex
整个页面就是用 CustomScrollView
来作的,可是有一点不一样:ui
平时咱们在使用 SliverAppBar
作这种折叠效果的时候,折叠起来是会变成主题色的,this
因此这里我找了别人写好的一个组件:FlexibleDetailBar
,用它之后的效果就是上面图片那样。spa
滑上去的时候「播放所有」那一行还停留在上方,是使用了 SliverAppBar 的 bottom参数。
这样一个页面的UI其实就分析完了。
然而!咱们回过头看一下两个页面的UI,是否是感受很是类似!咱们来捋一下。
so,咱们从上往下来封装。
肯定一下需求,看看须要传入哪些参数:
bottom 须要的是一个 PreferredSizeWidget
,因此咱们的代码是这样:
class MusicListHeader extends StatelessWidget implements PreferredSizeWidget {
MusicListHeader({this.count, this.tail, this.onTap});
final int count;
final Widget tail;
final VoidCallback onTap;
@override
Widget build(BuildContext context) {
return ClipRRect(
borderRadius: BorderRadius.vertical(
top: Radius.circular(ScreenUtil().setWidth(30))),
child: Container(
color: Colors.white,
child: InkWell(
onTap: onTap,
child: SizedBox.fromSize(
size: preferredSize,
child: Row(
children: <Widget>[
HEmptyView(20),
Icon(
Icons.play_circle_outline,
size: ScreenUtil().setWidth(50),
),
HEmptyView(10),
Padding(
padding: const EdgeInsets.only(top: 3.0),
child: Text(
"播放所有",
style: mCommonTextStyle,
),
),
HEmptyView(5),
Padding(
padding: const EdgeInsets.only(top: 3.0),
child: count == null
? Container()
: Text(
"(共$count首)",
style: smallGrayTextStyle,
),
),
Spacer(),
tail ?? Container(),
],
),
),
),
),
);
}
@override
Size get preferredSize => Size.fromHeight(ScreenUtil().setWidth(100));
}
复制代码
仍是先肯定一下需求,看看须要传入什么:
肯定好就以后,代码以下:
class PlayListAppBarWidget extends StatelessWidget {
final double expandedHeight;
final Widget content;
final String backgroundImg;
final String title;
final double sigma;
final VoidCallback playOnTap;
final int count;
PlayListAppBarWidget({
@required this.expandedHeight,
@required this.content,
@required this.title,
@required this.backgroundImg,
this.sigma = 5,
this.playOnTap,
this.count,
});
@override
Widget build(BuildContext context) {
return SliverAppBar(
centerTitle: true,
expandedHeight: expandedHeight,
pinned: true,
elevation: 0,
brightness: Brightness.dark,
iconTheme: IconThemeData(color: Colors.white),
title: Text(
title,
style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
),
bottom: MusicListHeader(
onTap: playOnTap,
count: count,
),
flexibleSpace: FlexibleDetailBar(
content: content,
background: Stack(
children: <Widget>[
backgroundImg.startsWith('http')
? Image.network(
backgroundImg,
width: double.infinity,
height: double.infinity,
fit: BoxFit.cover,
)
: Image.asset(backgroundImg),
BackdropFilter(
filter: ImageFilter.blur(
sigmaY: sigma,
sigmaX: sigma,
),
child: Container(
color: Colors.black38,
width: double.infinity,
height: double.infinity,
),
),
],
),
),
);
}
}
复制代码
这里有两个地方须要注意一下:
startsWith('http')
Colors.black38
,这样省的后续有白色图片所致使文字看不清。这个item就比较简单了,传入一个实体类,根据参数来填值就行了,大体代码以下:
class WidgetMusicListItem extends StatelessWidget {
final MusicData _data;
WidgetMusicListItem(this._data);
@override
Widget build(BuildContext context) {
return Container(
width: Application.screenWidth,
height: ScreenUtil().setWidth(120),
child: Row(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
// xxx
],
),
);
}
}
复制代码
通过前两次基础页面的搭建,咱们后续再来写页面的时候能够说是简单了百倍不止。
并且根本不用管网络请求之类的逻辑,只需管好咱们的页面就行了。
而在写UI时,也必定要多看,多想,这个能不能封装出来?那个能不能提取?
这样之后再开发的话,真的是很是简单。
该系列文章代码已传至 GitHub:github.com/wanglu1209/…
另我我的建立了一个「Flutter 交流群」,能够添加我我的微信 「17610912320」来入群。