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.

269 lines
7.5 KiB

3 years ago
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:huixiang/main.dart';
import 'package:huixiang/retrofit/data/banner.dart';
import 'package:huixiang/retrofit/data/base_data.dart';
import 'package:huixiang/retrofit/data/brand.dart';
import 'package:huixiang/retrofit/data/brand_data.dart';
import 'package:huixiang/retrofit/data/category_select_list.dart';
3 years ago
import 'package:huixiang/retrofit/data/collect_class_list.dart';
import 'package:huixiang/retrofit/data/course_details.dart';
3 years ago
import 'package:huixiang/retrofit/data/course_list.dart';
import 'package:huixiang/retrofit/data/page.dart';
import 'package:huixiang/retrofit/retrofit_api.dart';
import 'package:huixiang/utils/event_type.dart';
import 'package:huixiang/view_widget/classic_header.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:huixiang/view_widget/login_tips_dialog.dart';
import 'package:huixiang/view_widget/store_title_tab.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'community_view/class_list_view.dart';
import 'community_view/class_title_tab.dart';
import 'community_view/course_banner.dart';
import 'community_view/home_class.dart';
class CommunityCourse extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _CommunityCourse();
}
}
class _CommunityCourse extends State<CommunityCourse>
with SingleTickerProviderStateMixin, AutomaticKeepAliveClientMixin {
final ScrollController scrollController = ScrollController();
final RefreshController refreshController = RefreshController();
ApiService apiService;
List<Brand> brands = [];
BrandData brandData;
List<GlobalKey> globaKeys = [];
List<BannerData> bannerData = [];
List<CategorySelectList> classSelectList = [];
List<CourseList> classList = [];
List<CourseList> classTabList = [];
3 years ago
List<CollectClassList> collectList = [];
List<CourseList> collect = [];
3 years ago
int checkIndex = 0;
@override
void initState() {
super.initState();
eventBus.on<EventType>().listen((event) {
print("object: CommunityCourse");
if (event.type < 3) {
setState(() {});
}
});
queryCourseBanner();
3 years ago
classListAsync();
3 years ago
collectionList();
}
///banner
queryCourseBanner() async {
BaseData<PageInfo<BannerData>> banner = await apiService.queryBanner({
"model": {"type": "COURSE_BANNER"},
}).catchError((error) {
refreshController.refreshFailed();
});
bannerData.clear();
bannerData.addAll(banner.data.records);
3 years ago
}
///课程分类列表
classListAsync() async {
3 years ago
if (apiService == null) {
SharedPreferences value = await SharedPreferences.getInstance();
apiService = ApiService(
Dio(),
context: context,
token: value.getString("token"),
);
}
BaseData<List<CategorySelectList>> baseData = await apiService.categoryList().catchError((onError) {});
if (baseData != null && baseData.isSuccess) {
setState(() {
classSelectList = baseData.data;
});
queryClassList("");
}
EasyLoading.dismiss();
}
///课程列表
queryClassList(categoryId) async {
if (apiService == null) {
SharedPreferences value = await SharedPreferences.getInstance();
apiService = ApiService(
Dio(),
context: context,
token: value.getString("token"),
);
}
BaseData<PageInfo<CourseList>> baseData = await apiService.courseList({
"categoryId":categoryId,
"pageNum": 1,
"pageSize":10,
"searchKey": "",
"state":0
}).catchError((error) {
if(categoryId == ""){
refreshController.refreshFailed();
refreshController.loadFailed();
}
});
if (baseData.isSuccess) {
if(categoryId == "") {
refreshController.refreshCompleted();
refreshController.loadComplete();
setState(() {
classList.clear();
classList.addAll(baseData.data.list);
});
if(classSelectList.length > 0)
queryClassList(classSelectList[checkIndex].id);
}else{
setState(() {
classTabList.clear();
classTabList.addAll(baseData.data.list);
});
}
}
}
3 years ago
///课程的合集列表
collectionList() async {
if (apiService == null) {
SharedPreferences value = await SharedPreferences.getInstance();
apiService = ApiService(
Dio(),
context: context,
token: value.getString("token"),
);
}
BaseData<List<CollectClassList>> baseData = await apiService.collectList().catchError((onError) {});
if (baseData != null && baseData.isSuccess) {
setState(() {
collectList.clear();
collectList.addAll(baseData.data);
if(collectList.length > 0)
collectCourse(collectList[0].id);
});
}
EasyLoading.dismiss();
}
///合集包含的课程列表
collectCourse(collectId) async {
if (apiService == null) {
SharedPreferences value = await SharedPreferences.getInstance();
apiService = ApiService(
Dio(),
context: context,
token: value.getString("token"),
);
}
BaseData<List<CourseList>> baseData = await apiService.collect(collectId).catchError((error) {
});
if (baseData != null && baseData.isSuccess) {
setState(() {
collect.clear();
collect.addAll(baseData.data);
});
}
}
3 years ago
_onRefresh(){
classListAsync();
}
@override
Widget build(BuildContext context) {
super.build(context);
return
Stack(
children: [
Positioned(
child: Container(
child: SmartRefresher(
controller: refreshController,
enablePullDown: true,
enablePullUp: false,
header: MyHeader(),
physics: ClampingScrollPhysics(),
onRefresh: _onRefresh,
scrollController: scrollController,
child: Container(
child: SingleChildScrollView(
physics: NeverScrollableScrollPhysics(),
child: Container(
color: Color(0xFFF7F7F7),
margin: EdgeInsets.only(top: 16.h),
child: Column(
children: classChildItem(),
),
),
),
),
),
),
bottom:0,
top: 0,
left: 0,
right: 0,
),
if (brands != null && brands.length > 0)
Positioned(
child: Container(
color: Colors.white,
child: StoreTitleTab(
brands,
globaKeys,
scrollController,
isScroll: true,
),
),
top: 0,
left: 0,
right: 0,
),
],
);
}
List<Widget> classChildItem() {
var widgets = <Widget>[
///课程banner
CourseBanner(bannerData),
SizedBox(height: 28),
///回乡小课堂
3 years ago
HomeClass(collect,collectList),
3 years ago
///课程导航栏
ClassTitleTab(classSelectList,(index){
checkIndex = index;
queryClassList(classSelectList[index].id);
}),
///课程列表
ClassListView(classTabList),
];
return widgets;
}
@override
bool get wantKeepAlive => true;
}