|
|
|
import 'package:dio/dio.dart';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
|
|
|
import 'package:huixiang/generated/l10n.dart';
|
|
|
|
import 'package:huixiang/retrofit/data/article.dart';
|
|
|
|
import 'package:huixiang/retrofit/data/banner.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 TouTiao extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
State<StatefulWidget> createState() {
|
|
|
|
return _TouTiao();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class _TouTiao extends State<TouTiao> {
|
|
|
|
ApiService apiService;
|
|
|
|
List<BannerData> bannerData = [];
|
|
|
|
|
|
|
|
@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<PageInfo<Article>> baseData = await apiService.queryArticle({
|
|
|
|
"pageNum": pageNum,
|
|
|
|
"pageSize": 10,
|
|
|
|
"searchKey": "",
|
|
|
|
"state": 1,
|
|
|
|
"type": 2
|
|
|
|
}).catchError((onError){
|
|
|
|
refreshController.refreshFailed();
|
|
|
|
refreshController.loadFailed();
|
|
|
|
});
|
|
|
|
if (baseData != null && baseData.isSuccess) {
|
|
|
|
refreshController.refreshCompleted();
|
|
|
|
refreshController.loadComplete();
|
|
|
|
if(pageNum == 1) {
|
|
|
|
articles.clear();
|
|
|
|
}
|
|
|
|
articles.addAll(baseData.data.list);
|
|
|
|
if (baseData.data.pageNum == baseData.data.pages) {
|
|
|
|
refreshController.loadNoData();
|
|
|
|
} else {
|
|
|
|
pageNum += 1;
|
|
|
|
}
|
|
|
|
setState(() {});
|
|
|
|
} else {
|
|
|
|
refreshController.refreshFailed();
|
|
|
|
refreshController.loadFailed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///banner
|
|
|
|
queryCourseBanner() async {
|
|
|
|
if (apiService == null) {
|
|
|
|
SharedPreferences value = await SharedPreferences.getInstance();
|
|
|
|
apiService = ApiService(
|
|
|
|
Dio(),
|
|
|
|
context: context,
|
|
|
|
token: value.getString("token"),
|
|
|
|
showLoading: true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
BaseData<PageInfo<BannerData>> baseData =
|
|
|
|
await apiService.queryBanner({
|
|
|
|
"model": {"type": "COURSE_BANNER"},
|
|
|
|
}).catchError((onError) {
|
|
|
|
refreshController.refreshFailed();
|
|
|
|
});
|
|
|
|
if (baseData != null && baseData.isSuccess) {
|
|
|
|
bannerData.clear();
|
|
|
|
bannerData.addAll(baseData.data.records);
|
|
|
|
refreshController.refreshCompleted();
|
|
|
|
} else {
|
|
|
|
refreshController.refreshFailed();
|
|
|
|
// SmartDialog.showToast(baseData.msg, alignment: Alignment.center);
|
|
|
|
}
|
|
|
|
EasyLoading.dismiss();
|
|
|
|
}
|
|
|
|
|
|
|
|
@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: Column(
|
|
|
|
children: [
|
|
|
|
|
|
|
|
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: position == 0 ? 1.38 : 2.56,
|
|
|
|
child: Container(
|
|
|
|
height: position == 0 ? 247.h : 96.h,
|
|
|
|
margin: EdgeInsets.symmetric(vertical:6.h, horizontal: 16.w),
|
|
|
|
child:
|
|
|
|
HotArticleItem(article: articles[position], isHot: position == 0),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|