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.

126 lines
3.6 KiB

4 years ago
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
4 years ago
import 'package:flutter/material.dart';
4 years ago
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';
4 years ago
import 'package:huixiang/view_widget/hot_item.dart';
4 years ago
import 'package:huixiang/view_widget/my_footer.dart';
4 years ago
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
4 years ago
class HotArticlePage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _HotArticlePage();
}
}
class _HotArticlePage extends State<HotArticlePage> {
4 years ago
ApiService apiService;
3 years ago
@override
void dispose() {
super.dispose();
refreshController.dispose();
}
4 years ago
@override
void initState() {
super.initState();
SharedPreferences.getInstance().then((value) => {
4 years ago
apiService = ApiService(Dio(), context: context, token: value.getString("token")),
4 years ago
queryArticle(),
});
}
int pageNum = 1;
RefreshController refreshController = RefreshController();
_onRefresh() {
pageNum = 1;
queryArticle();
}
List<Article> articles = [];
queryArticle() async {
3 years ago
BaseData<PageInfo<Article>> baseData = await apiService.queryArticle({
4 years ago
"pageNum": pageNum,
"pageSize": 10,
"searchKey": "",
4 years ago
"state": 1,
4 years ago
"type": 2
}).catchError((onError){
refreshController.refreshFailed();
refreshController.loadFailed();
});
if (baseData != null && baseData.isSuccess) {
refreshController.refreshCompleted();
refreshController.loadComplete();
if(pageNum == 1) {
articles.clear();
}
3 years ago
articles.addAll(baseData.data.list);
if (baseData.data.pageNum == baseData.data.pages) {
4 years ago
refreshController.loadNoData();
} else {
pageNum += 1;
}
setState(() {});
} else {
refreshController.refreshFailed();
refreshController.loadFailed();
}
}
4 years ago
@override
Widget build(BuildContext context) {
return Scaffold(
3 years ago
// appBar: MyAppBar(
// background: Color(0xFFF7F7F7),
// leadingColor: Colors.black,
// title: S.of(context).huixiangtoutiao,
// titleColor: Colors.black,
// titleSize: 18.sp,
// ),
4 years ago
body: Container(
4 years ago
child: SmartRefresher(
controller: refreshController,
enablePullDown: true,
enablePullUp: true,
4 years ago
physics: BouncingScrollPhysics(),
4 years ago
header: MyHeader(),
footer: CustomFooter(
builder: (context, mode) {
4 years ago
return MyFooter(mode);
4 years ago
},
),
onRefresh: _onRefresh,
onLoading: queryArticle,
child: ListView.builder(
itemCount: articles.length,
physics: NeverScrollableScrollPhysics(),
scrollDirection: Axis.vertical,
itemBuilder: (context, position) {
return AspectRatio(
3 years ago
aspectRatio: position == 0 ? 1.38 : 2.56,
4 years ago
child: Container(
3 years ago
height: position == 0 ? 247.h : 96.h,
3 years ago
margin: EdgeInsets.symmetric(vertical:6.h, horizontal: 16.w),
child:
HotArticleItem(article: articles[position], isHot: position == 0),
4 years ago
),
);
},
),
4 years ago
),
),
);
}
}