You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
141 lines
4.0 KiB
141 lines
4.0 KiB
import 'package:dio/dio.dart'; |
|
import 'package:flutter/cupertino.dart'; |
|
import 'package:flutter/material.dart'; |
|
import 'package:huixiang/generated/l10n.dart'; |
|
import 'package:huixiang/retrofit/data/article.dart'; |
|
import 'package:huixiang/retrofit/data/base_data.dart'; |
|
import 'package:huixiang/retrofit/data/page.dart'; |
|
import 'package:huixiang/retrofit/retrofit_api.dart'; |
|
import 'package:huixiang/view_widget/classic_header.dart'; |
|
import 'package:huixiang/view_widget/hot_item.dart'; |
|
import 'package:huixiang/view_widget/my_footer.dart'; |
|
import 'package:pull_to_refresh/pull_to_refresh.dart'; |
|
import 'package:shared_preferences/shared_preferences.dart'; |
|
|
|
class HotArticlePage extends StatefulWidget { |
|
@override |
|
State<StatefulWidget> createState() { |
|
return _HotArticlePage(); |
|
} |
|
} |
|
|
|
class _HotArticlePage extends State<HotArticlePage> { |
|
ApiService apiService; |
|
|
|
@override |
|
void initState() { |
|
super.initState(); |
|
|
|
SharedPreferences.getInstance().then((value) => { |
|
apiService = ApiService(Dio(), context: context, token: value.getString("token")), |
|
queryArticle(), |
|
}); |
|
} |
|
|
|
int pageNum = 1; |
|
RefreshController refreshController = RefreshController(); |
|
|
|
_onRefresh() { |
|
pageNum = 1; |
|
queryArticle(); |
|
} |
|
|
|
List<Article> articles = []; |
|
|
|
queryArticle() async { |
|
BaseData baseData = await apiService.queryArticle({ |
|
"pageNum": pageNum, |
|
"pageSize": 10, |
|
"searchKey": "", |
|
"state": 1, |
|
"type": 2 |
|
}).catchError((onError){ |
|
refreshController.refreshFailed(); |
|
refreshController.loadFailed(); |
|
}); |
|
if (baseData != null && baseData.isSuccess) { |
|
PageInfo pageInfo = PageInfo.fromJson(baseData.data); |
|
refreshController.refreshCompleted(); |
|
refreshController.loadComplete(); |
|
if(pageNum == 1) { |
|
articles.clear(); |
|
} |
|
articles.addAll(pageInfo.list.map((e) => Article.fromJson(e)).toList()); |
|
if (pageInfo.pageNum == pageInfo.pages) { |
|
refreshController.loadNoData(); |
|
} else { |
|
pageNum += 1; |
|
} |
|
setState(() {}); |
|
} else { |
|
refreshController.refreshFailed(); |
|
refreshController.loadFailed(); |
|
} |
|
} |
|
|
|
@override |
|
Widget build(BuildContext context) { |
|
return Scaffold( |
|
appBar: AppBar( |
|
backgroundColor: Color(0xFFF7F7F7), |
|
elevation: 0, |
|
title: Text( |
|
S.of(context).remenwenzhangshipinliebiao, |
|
style: TextStyle( |
|
color: Colors.black, |
|
fontWeight: FontWeight.bold, |
|
), |
|
), |
|
centerTitle: false, |
|
leading: GestureDetector( |
|
onTap: () { |
|
Navigator.of(context).pop(); |
|
}, |
|
child: Container( |
|
alignment: Alignment.centerRight, |
|
margin: EdgeInsets.only(left: 10), |
|
padding: EdgeInsets.all(6), |
|
child: Icon( |
|
Icons.arrow_back_ios, |
|
color: Colors.black, |
|
size: 24, |
|
), |
|
), |
|
), |
|
titleSpacing: 2, |
|
leadingWidth: 56, |
|
), |
|
body: Container( |
|
child: SmartRefresher( |
|
controller: refreshController, |
|
enablePullDown: true, |
|
enablePullUp: true, |
|
physics: BouncingScrollPhysics(), |
|
header: MyHeader(), |
|
footer: CustomFooter( |
|
builder: (context, mode) { |
|
return MyFooter(mode); |
|
}, |
|
), |
|
onRefresh: _onRefresh, |
|
onLoading: queryArticle, |
|
child: ListView.builder( |
|
itemCount: articles.length, |
|
physics: NeverScrollableScrollPhysics(), |
|
scrollDirection: Axis.vertical, |
|
itemBuilder: (context, position) { |
|
return AspectRatio( |
|
aspectRatio: 2.47, |
|
child: Container( |
|
height: 130, |
|
margin: EdgeInsets.symmetric(vertical: 8, horizontal: 16), |
|
child: HotArticleItem(article: articles[position]), |
|
), |
|
); |
|
}, |
|
), |
|
), |
|
), |
|
); |
|
} |
|
}
|
|
|