提交 3cef8b26 authored 作者: yueweilu's avatar yueweilu

图书界面 问题还未解决

上级 83f8ae84
......@@ -34,12 +34,18 @@ abstract class LibraryAPI {
static Future <List<CourseModel>> books({
int page = 1,
int limit = 10,
String sortField = '',
String sort = '',
String isFree = '',
}) async {
final result = await HttpService.to.post(
'/v1/book/category/getBookList',
params: {
'page': page,
'page_size': limit,
// 'sort_field':sortField,
// 'sort':sort,
// 'is_free':isFree
},
);
if (result.data is! Map && result.data['list'] is! List) return [];
......
......@@ -33,14 +33,17 @@ class LabelModel {
LabelModel({
this.labelId,
this.name,
this.selected = false,
});
LabelModel.fromJson(dynamic json) {
labelId = json['label_id'];
name = json['name'];
selected = false;
}
num? labelId;
String? name;
late bool selected;
LabelModel copyWith({ num? labelId,
String? name,
}) => LabelModel( labelId: labelId ?? this.labelId,
......
......@@ -16,6 +16,8 @@ class LibraryController extends GetxController with GetTickerProviderStateMixin{
// 广告数组
List<AdModel> ads = [];
late LabelModel selectedLabel = LabelModel();
late TabController tabController = TabController(length:categories.length, vsync: this);
final int _limit = 20;
......@@ -61,6 +63,16 @@ class LibraryController extends GetxController with GetTickerProviderStateMixin{
onRefresh();
}
}
//
void selectLabel(LabelModel model){
for (LabelModel m in labels){
m.selected = false;
}
model.selected = true;
selectedLabel = model;
update();
}
/// 获取广告数据
void _getAds() async {
......@@ -79,6 +91,8 @@ class LibraryController extends GetxController with GetTickerProviderStateMixin{
/// 获取标签数据
void _getLabels() async {
labels = await LibraryAPI.labels();
selectedLabel = labels.first;
selectedLabel.selected = true;
update();
}
......
......@@ -14,6 +14,7 @@ import '../../models/index.dart';
import '../../routes/index.dart';
import '../../widgets/index.dart';
import '../course/index.dart';
import '../library_content/index.dart';
import '../study/index.dart';
......
......@@ -7,7 +7,7 @@ class LibraryPage extends StatefulWidget {
State<LibraryPage> createState() => _LibraryPageState();
}
class _LibraryPageState extends State<LibraryPage> with SingleTickerProviderStateMixin{
class _LibraryPageState extends State<LibraryPage> with AutomaticKeepAliveClientMixin {
//AutomaticKeepAliveClientMixin
@override
......@@ -84,19 +84,45 @@ class _LibraryPageState extends State<LibraryPage> with SingleTickerProviderStat
),
),
),
body: TabBarView(
body: Column(
children: [
Container(
height: 43,
color: Colours.cF9,
child: ListView.builder(
scrollDirection:Axis.horizontal ,
itemBuilder: (BuildContext context, int index){
return GestureDetector(
child: BuildLabelWidget(model: controller.labels[index],),
onTap: (){
controller.selectLabel( controller.labels[index]);
},
);
},
itemCount: controller.labels.length,
),
),
Container(
color: Colors.transparent,
padding: const EdgeInsets.symmetric(horizontal: 10),
child: BuildBanner(items: controller.ads,),
),
Expanded(
child: TabBarView(
controller: controller.tabController,
children: List.generate(controller.categories.length, (index){
print('=======================================$index');
return LibraryContentPage(controller: controller,);
})
),
),
],
),
),
);
}
// @override
// // TODO: implement wantKeepAlive
// bool get wantKeepAlive => true;
@override
bool get wantKeepAlive => true;
}
......@@ -13,49 +13,27 @@ class LibraryContentPage extends StatefulWidget {
State<LibraryContentPage> createState() => _LibraryContentPageState();
}
class _LibraryContentPageState extends State<LibraryContentPage> {
class _LibraryContentPageState extends State<LibraryContentPage> with AutomaticKeepAliveClientMixin {
@override
Widget build(BuildContext context) {
return Column(
children: [
SizedBox(
height: 43,
// color: Colors.green,
child: ListView.builder(
scrollDirection:Axis.horizontal ,
itemBuilder: (BuildContext context, int index){
return GestureDetector(
child: BuildLabelWidget(model: widget.controller.labels[index],),
onTap: (){
},
);
},
itemCount: widget.controller.labels.length,
),
),
Expanded(
child: CustomPullScrollView(
return CustomPullScrollView(
controller: widget.controller.refreshController,
onRefresh: widget.controller.onRefresh,
onLoading: widget.controller.onLoading,
child: NestedScrollView(
headerSliverBuilder: (BuildContext context ,bool innerBoxIsScrolled){
return [
SliverList(
delegate: SliverChildListDelegate([
Container(
color: Colors.transparent,
padding: const EdgeInsets.symmetric(horizontal: 10),
child: BuildBanner(items: widget.controller.ads,),
),
])
)
];
},
body: ListView.builder(
child: ListView.builder(
// shrinkWrap: true,
// physics: NeverScrollableScrollPhysics(),
scrollDirection: Axis.vertical,
itemCount: widget.controller.books.length,
itemBuilder: (BuildContext context, int index) {
// if (index == 0){
// return Container(
// color: Colors.transparent,
// padding: const EdgeInsets.symmetric(horizontal: 10),
// child: BuildBanner(items: widget.controller.ads,),
// );
// }
// else {
CourseModel model = widget.controller.books[index];
return GestureDetector(
onTap: (){
......@@ -65,12 +43,11 @@ class _LibraryContentPageState extends State<LibraryContentPage> {
widget.controller.love(bookId: model.bookId.toString(), isCollection: model.isCollection!);
},),
);
},
),
),
),
}
// },
),
],
);
}
@override
bool get wantKeepAlive => true;
}
......@@ -12,7 +12,7 @@ class BuildLabelWidget extends StatelessWidget {
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.only(left: 10),
color: Colors.white,
color: Colors.transparent,
width: 65,
padding: const EdgeInsets.only(top: 10,bottom: 10),
child: Container(
......@@ -22,10 +22,18 @@ class BuildLabelWidget extends StatelessWidget {
height: 23,
decoration: BoxDecoration(
borderRadius: const BorderRadius.all(Radius.circular(12.5)),
border: Border.all(color:AppTheme.primary,width: 0.5,style: BorderStyle.solid),
// color: widget.model.selected == true?Colours.book_app_main:Colors.white,
border:model.selected? Border.all(color:AppTheme.primary,width: 0.5,style: BorderStyle.solid):null,
color: Colors.white,
boxShadow: model.selected?[
BoxShadow(
color: AppTheme.primary.withOpacity(0.3),
offset: const Offset(0, 1),
blurRadius: 2.w,
spreadRadius: 0.w,
),
]:null,
),
child: Text(model.name??'',style: const TextStyle(fontSize: 13,height: 1.5,color: AppTheme.primary,fontWeight: Fonts.medium),)
child: Text(model.name??'',style: TextStyle(fontSize: 13,height: 1.5,color: model.selected?AppTheme.primary:Colours.c9,fontWeight: Fonts.medium),)
),
);
}
......
part of library_content;
class LibraryContentController extends GetxController {
final EasyRefreshController refreshController = EasyRefreshController(
controlFinishLoad: true,
controlFinishRefresh: true,
);
final int _limit = 10;
int _page = 1;
bool _noMore = false;
// 图书列表数据
List<CourseModel> books = [];
@override
void onReady() {
super.onReady();
}
@override
void onClose() {
refreshController.dispose();
super.onClose();
}
/// 收藏 与 取消收藏
void love({
required String bookId,
required num isCollection
}) async {
if (isCollection == 0){
isCollection = 1;
}
else{
isCollection = 0;
}
bool result = await CommonAPI.love(bookId: bookId, love: isCollection.toString());
if (result) {
onRefresh();
}
}
/// 获取图书列表数据
Future<void> _getBooks([bool isRefresh = false]) async {
if (isRefresh) _page = 1;
// 网路请求
final result = await LibraryAPI.books(
page: _page,
limit: _limit
);
// 如果是刷新 清理数据
if (isRefresh) books.clear();
books.addAll(result);
_page ++;
_noMore = result.length < _limit;
update();
}
void onRefresh() async {
try {
await _getBooks(true);
refreshController.finishRefresh(IndicatorResult.success);
refreshController.resetFooter();
} catch (error) {
refreshController.finishRefresh(IndicatorResult.fail);
}
}
void onLoading() async {
if (_noMore) {
refreshController.finishLoad(IndicatorResult.noMore);
return;
}
try {
await _getBooks();
refreshController.finishLoad();
} catch (error) {
refreshController.finishLoad(IndicatorResult.fail);
}
}
}
\ No newline at end of file
library library_content;
import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_state_manager/src/simple/get_controllers.dart';
import 'package:go_router/go_router.dart';
import '../../apis/index.dart';
import '../../models/index.dart';
import '../../routes/index.dart';
import '../../widgets/index.dart';
import '../course/index.dart';
import '../library/index.dart';
part 'view.dart';
part 'controller.dart';
\ No newline at end of file
part of library_content;
class LibraryContentPage extends StatefulWidget {
final CategoryModel categoryModel;
final LabelModel labelModel;
const LibraryContentPage({
Key? key,
required this.categoryModel,
required this.labelModel
}) : super(key: key);
@override
State<LibraryContentPage> createState() => _LibraryContentPageState();
}
class _LibraryContentPageState extends State<LibraryContentPage> with AutomaticKeepAliveClientMixin {
@override
Widget build(BuildContext context) {
return GetBuilder<LibraryContentController>(
init: LibraryContentController(),
builder: (controller) => CustomPullScrollView(
controller: controller.refreshController,
onRefresh: controller.onRefresh,
onLoading: controller.onLoading,
child: ListView.builder(
itemCount: controller.books.length,
itemBuilder: (BuildContext context, int index) {
// if (index == 0){
// return Container(
// color: Colors.transparent,
// padding: const EdgeInsets.symmetric(horizontal: 10),
// child: BuildBanner(items: controller.ads,),
// );
// }
// else {
CourseModel model = controller.books[index];
return GestureDetector(
onTap: (){
context.pushNamed(Routes.bookDetail,queryParameters: {'book_id':model.bookId.toString()});
},
child: LibraryCell(model: model,onTap: (){
controller.love(bookId: model.bookId.toString(), isCollection: model.isCollection!);
},),
);
}
// },
),
),
);
}
@override
bool get wantKeepAlive => true;
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论