本系列可能會伴隨你們很長時間,這裏我會從0開始搭建一個「網易雲音樂」的APP出來。git
下面是該APP 功能的思惟導圖:github
前期回顧:json
本篇爲第二篇,在這裏咱們會搭建閃屏頁、登陸頁、發現頁的UI及邏輯。微信
咱們如今的APP都有一個初始頁面,在這個頁面當中作一些插件和邏輯的初始化工做,因此咱們首先就來作一個這個頁面。markdown
先來看一下效果:網絡
很是簡單,就是一個網易雲音樂的 Logo 從小到大。架構
大體代碼以下:less
return Scaffold(
backgroundColor: Colors.white,
body: Container(
height: double.infinity,
width: double.infinity,
child: ScaleTransition(
scale: _logoAnimation,
child: Hero(
tag: 'logo',
child: Image.asset('images/icon_logo.png'),
),
),
),
);
複製代碼
可是,這個頁面遠不止只有一個動畫這麼簡單。async
首先在查看過API 以後瞭解到,因不少接口都須要登陸以後才能使用,因此在當前頁面要判斷是否已經登陸,ide
若是沒有登陸,那麼則跳轉到登陸頁,若是已經登陸,那麼則跳轉到APP首頁。
邏輯代碼以下:
UserModel userModel = Provider.of<UserModel>(context);
userModel.initUser();
if (userModel.user != null) {
NetUtils.refreshLogin(context);
NavigatorUtil.goHomePage(context);
} else
NavigatorUtil.goLoginPage(context);
複製代碼
使用 Provider
來全局存儲用戶信息, SharedPreferences
存儲用戶信息到本地。
其中 initUser()
方法就是用來從 SharedPreferences
中獲取用戶信息,若是沒有獲取到就爲null。
爲 null 的狀況下就要去跳轉登陸,若是不爲空那麼則刷新登陸狀態,而後跳轉到首頁。
固然,最後不要忘了 AnimationController.dispose()
。
這裏就區分兩種:
先說UI。
首先從上面的UI能看出來有兩個動畫效果:
Hero比較簡單我就很少說了,能夠查看我之前的文章:
全部的登陸組件被我封裝在了組件中,而後使用 AnimatedWidget
來控制動畫:
class _LoginAnimatedWidget extends AnimatedWidget {
final Tween<double> _opacityTween = Tween(begin: 0, end: 1);
final Tween<double> _offsetTween = Tween(begin: 40, end: 0);
final Animation animation;
_LoginAnimatedWidget({
@required this.animation,
}) : super(listenable: animation);
@override
Widget build(BuildContext context) {
return Opacity(
opacity: _opacityTween.evaluate(animation),
child: Container(
margin: EdgeInsets.only(top: _offsetTween.evaluate(animation)),
child: _LoginWidget(),
),
);
}
}
複製代碼
這樣就組成了從 splash 頁面跳轉到登陸頁的動畫效果。
前面說過,是使用 Provider
來存儲用戶信息的,那麼請求登陸也使用 Provider
來控制,以達到 UI 數據分離的效果。
先看一下 UserModel
類:
class UserModel with ChangeNotifier {
User _user;
User get user => _user;
/// 初始化 User
void initUser() {
if (Application.sp.containsKey('user')) {
String s = Application.sp.getString('user');
_user = User.fromJson(json.decode(s));
}
}
/// 登陸
void login(BuildContext context, String phone, String pwd) async {
User user = await NetUtils.login(context, phone, pwd);
if (user.code > 299) {
Fluttertoast.showToast(msg: user.msg ?? '登陸失敗,請檢查帳號密碼', gravity: ToastGravity.CENTER);
return;
}
Fluttertoast.showToast(msg: '登陸成功', gravity: ToastGravity.CENTER);
_saveUserInfo(user);
NavigatorUtil.goHomePage(context);
}
/// 保存用戶信息到 sp
_saveUserInfo(User user) {
_user = user;
Application.sp.setString('user', json.encode(user.toJson()));
}
}
複製代碼
代碼也很清晰,一共就三個方法:
發現頁從上到下,一共分五塊:
其中除「分類」模塊爲本地外,其他都是網絡請求的數據。
Banner 使用的控件,我以前也分享過文章:Flutter | 封裝一個 Banner 輪播圖。
其他的也是用我以前寫過的Flutter | 定義一個通用的多功能網絡請求 Widget。
使用該控件的好處就是 省去處理網絡請求的邏輯,只有返回正確數據時纔會走到build回調。其餘的邏輯一律不用考慮。
有沒有發現這兩個控件很像?
區別有兩個,第一個是上面的有播放量,下面的沒有。而下面的有第二行小字,上面的沒有。
那咱們就能夠把它封裝成一個組件!
等等!爲何咱們不把圓角矩形圖片也封裝成一個組件呢?萬一後面也能用到呢?
???那爲何不把封面也封裝成一個組件呢?萬一後面也能用到呢?
因此,咱們先來封裝圓角矩形圖片:
class RoundedNetImage extends StatelessWidget {
final String url;
final double width;
final double height;
final double radius;
RoundedNetImage(this.url, {this.width, this.height, this.radius = 10});
@override
Widget build(BuildContext context) {
return ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(radius)),
child: Image.network(
url,
width: ScreenUtil().setWidth(width),
height: ScreenUtil().setWidth(height),
),
);
}
}
複製代碼
這裏咱們作的靈活一點,角度和寬高都由調用者來傳入。
而後繼續封裝咱們的封面組件:
/// 歌單、新碟上架等封面組件
class PlayListCoverWidget extends StatelessWidget {
final String url;
final int playCount;
final double width;
PlayListCoverWidget(this.url, {this.playCount, this.width = 200});
@override
Widget build(BuildContext context) {
return ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(8)),
child: Container(
width: ScreenUtil().setWidth(width),
height: ScreenUtil().setWidth(width),
child: Stack(
alignment: Alignment.topRight,
children: <Widget>[
Image.network(url),
playCount == null
? Container()
: Padding(
padding: EdgeInsets.only(
top: ScreenUtil().setWidth(2),
right: ScreenUtil().setWidth(5)),
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Image.asset(
'images/icon_triangle.png',
width: ScreenUtil().setWidth(30),
height: ScreenUtil().setWidth(30),
),
Text(
'${NumberUtils.amountConversion(playCount)}',
style: TextStyle(
color: Colors.white,
fontSize: 12,
fontWeight: FontWeight.w500,
),
)
],
),
)
],
),
),
);
}
}
複製代碼
代碼也很簡單,一共須要三個參數:
接下來就簡單了,由於前兩個組件都已經封裝好了:
/// 歌單、新碟上架等封裝的組件
class PlayListWidget extends StatelessWidget {
final String picUrl;
final String text;
final String subText;
final num playCount;
final int maxLines;
final VoidCallback onTap;
final int index;
PlayListWidget({
this.picUrl,
@required this.text,
this.playCount,
this.subText,
this.onTap,
this.maxLines = -1,
this.index,
});
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onTap,
child: Container(
width: ScreenUtil().setWidth(200),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
picUrl == null ? Container() : PlayListCoverWidget(
picUrl,
playCount: playCount,
),
index == null ? Container() : Text(index.toString(), style: commonGrayTextStyle,),
VEmptyView(5),
Text(
text,
style: smallCommonTextStyle,
maxLines: maxLines != -1 ? maxLines : null,
overflow: maxLines != -1 ? TextOverflow.ellipsis : null,
),
subText == null ? Container() : VEmptyView(2),
subText == null
? Container()
: Text(
subText,
style: TextStyle(fontSize: 10, color: Colors.grey),
maxLines: maxLines != -1 ? maxLines : null,
overflow: maxLines != -1 ? TextOverflow.ellipsis : null,
),
],
),
),
);
}
}
複製代碼
雖然代碼比較多,但邏輯仍是很簡單的。
其實就是一個Column
,而後根據字段是否爲null來顯示/隱藏某一個組件。
這樣咱們的發現頁的邏輯大體就結束了。
能夠看得出來,只要咱們前期架構搭的好,後期寫起來代碼真的是一鼓作氣。
代碼我就不放在單獨的分支裏了,都在主分支裏。
而且如今主分支當中也上傳了到目前爲止所開發完成的功能,歡迎查看。
該系列文章代碼已傳至 GitHub:github.com/wanglu1209/…
另我我的建立了一個「Flutter 交流羣」,能夠添加我我的微信 「17610912320」來入羣。