|
|
|
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_appbar.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';
|
|
|
|
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
|
|
|
|
|
|
|
class HotArticlePage extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
State<StatefulWidget> createState() {
|
|
|
|
return _HotArticlePage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class _HotArticlePage extends State<HotArticlePage> {
|
|
|
|
ApiService apiService;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
super.dispose();
|
|
|
|
refreshController.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@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: MyAppBar(
|
|
|
|
background: Color(0xFFF7F7F7),
|
|
|
|
leadingColor: Colors.black,
|
|
|
|
title: S.of(context).huixiangtoutiao,
|
|
|
|
titleColor: Colors.black,
|
|
|
|
titleSize: 18.sp,
|
|
|
|
),
|
|
|
|
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.h,
|
|
|
|
margin: EdgeInsets.symmetric(vertical: 8, horizontal: 16),
|
|
|
|
child: HotArticleItem(article: articles[position]),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|