提交 7233760d authored 作者: yueweilu's avatar yueweilu

书架界面 接口调试与数据绑定

上级 6b193aa0
part of apis;
abstract class ShopAPI {
/// 1、加入书架
///
static Future <bool> addCart({
required String bookId,
}) async {
final result = await HttpService.to.post(
'/v1/orders/Orders/addCart',
params: {
'book_id':bookId,
},
);
if (result.data is Map && result.data['is_success'] == 1){
return true;
}
return false;
}
/// 2、删除书架
///
static Future <bool> delCart({
required String cartId,
}) async {
final result = await HttpService.to.post(
'/v1/orders/Orders/delCart',
params: {
'cart_id':cartId,
},
);
if (result.data is Map && result.data['is_success'] == 1){
return true;
}
return false;
}
/// 3、书架列表
static Future <List<CourseModel>> cart({
int page = 1,
int limit = 10,
}) async {
final result = await HttpService.to.post(
'/v1/orders/Orders/getCartListAll',
params: {
'page': page,
'pageSize': limit,
},
);
if (result.data is! Map && result.data['list'] is! List) return [];
return List.generate(result.data['list'].length, (index){
return CourseModel.fromJson(result.data['list'][index]);
});
}
}
\ No newline at end of file
......@@ -23,7 +23,15 @@ class CourseModel {
this.introduction,
this.readNum,
this.isCollection,
this.collectionId
this.collectionId,
/// 购物车列表需要参数
this.price,
this.vipPrice,
this.status,
this.cartId
///
});
CourseModel.fromJson(dynamic json) {
......@@ -53,6 +61,14 @@ class CourseModel {
num? isCollection;
num? collectionId;
/// 购物车列表需要参数
String? price;
String? vipPrice;
num? status;
num? cartId;
///
int get type {
if (progress == '0.00%'){
......@@ -87,6 +103,13 @@ class CourseModel {
num? isCollection,
num? collectionId,
/// 购物车列表需要参数
String? price,
String? vipPrice,
num? status,
num? cartId,
///
}) => CourseModel( bookId: bookId ?? this.bookId,
bookName: bookName ?? this.bookName,
authors: authors ?? this.authors,
......@@ -98,7 +121,13 @@ class CourseModel {
introduction: introduction ?? this.introduction,
readNum: readNum ?? this.readNum,
isCollection: isCollection ?? this.isCollection,
collectionId: collectionId ?? this.collectionId
collectionId: collectionId ?? this.collectionId,
/// 购物车列表需要参数
price: price ?? this.price,
vipPrice: vipPrice ?? this.vipPrice,
status: status ?? this.status,
cartId: cartId ?? this.cartId,
///
);
Map<String, dynamic> toJson() {
......@@ -115,6 +144,11 @@ class CourseModel {
map['is_collection'] = isCollection;
map['collection_id'] = collectionId;
map['introduction'] = introduction;
map['price'] = price;
map['vip_price'] = vipPrice;
map['status'] = status;
map['cart_id'] = cartId;
return map;
}
......
part of book_shop;
class BookshopController extends GetxController {
final EasyRefreshController refreshController = EasyRefreshController(
controlFinishLoad: true,
controlFinishRefresh: true,
);
// 书架数组
List<CourseModel> carts = [];
final int _limit = 10;
int _page = 1;
bool _noMore = false;
@override
void onClose() {
refreshController.dispose();
super.onClose();
}
/// 获取课程内图书列表
Future<void> _getCart([bool isRefresh = false]) async {
if (isRefresh) _page = 1;
// 网路请求
final result = await ShopAPI.cart(
page: _page,
limit: _limit
);
// 如果是刷新 清理数据
if (isRefresh) carts.clear();
carts.addAll(result);
_page ++;
_noMore = result.length < _limit;
update();
}
void onRefresh() async {
try {
await _getCart(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 _getCart();
refreshController.finishLoad();
} catch (error) {
refreshController.finishLoad(IndicatorResult.fail);
}
}
}
\ No newline at end of file
library book_shop;
import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_book/utils/index.dart';
import 'package:flutter_book/widgets/index.dart';
import 'package:get/get.dart';
import 'package:go_router/go_router.dart';
import '../../apis/index.dart';
import '../../models/index.dart';
import '../../routes/index.dart';
import '../../theme.dart';
......
......@@ -10,23 +10,31 @@ class BookShopPage extends StatefulWidget {
class _BookShopPageState extends State<BookShopPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
return GetBuilder<BookshopController>(
init: BookshopController(),
builder: (controller) => Scaffold(
appBar: AppBar(
title: const Text('书架'),
),
body: Column(
children: [
Expanded(
child: CustomPullScrollView(
controller: controller.refreshController,
onRefresh: controller.onRefresh,
onLoading: controller.onLoading,
child: ListView.builder(
itemBuilder: (BuildContext context,int index){
return const BookCell();
return BookCell(model: controller.carts[index],);
},
itemCount: 2,
itemCount: controller.carts.length,
),
),
),
createCounter()
],
),
),
);
}
......
......@@ -7,10 +7,11 @@ enum BookCellType {
class BookCell extends StatelessWidget {
final BookCellType? type;
final CourseModel? model;
const BookCell({
super.key,
this.type = BookCellType.normal,
this.model
});
@override
......@@ -21,10 +22,10 @@ class BookCell extends StatelessWidget {
color: Colors.white,
boxShadow: [
BoxShadow(
color: Color(0xFFC7C7C7).withOpacity(0.5),
color: const Color(0xFFC7C7C7).withOpacity(0.5),
spreadRadius: 2,
blurRadius: 5,
offset: Offset(3, 0), // changes the position of the shadow
offset: const Offset(3, 0), // changes the position of the shadow
),
]
),
......@@ -49,10 +50,10 @@ class BookCell extends StatelessWidget {
borderRadius: BorderRadius.circular(3),
boxShadow: [
BoxShadow(
color: Color(0xFF707070).withOpacity(0.5),
color: const Color(0xFF707070).withOpacity(0.5),
spreadRadius: 2,
blurRadius: 5,
offset: Offset(3, 0), // changes the position of the shadow
offset: const Offset(3, 0), // changes the position of the shadow
),
]
),
......@@ -60,7 +61,7 @@ class BookCell extends StatelessWidget {
height: 86,
width: 72,
child: Container(
padding: EdgeInsets.all(2),
padding: const EdgeInsets.all(2),
child: Container(
color: Colors.cyan,
),
......@@ -74,19 +75,19 @@ class BookCell extends StatelessWidget {
Expanded(
child: Container(
padding: const EdgeInsets.only(top: 12,bottom: 10),
child: const Column(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('一想到还有95%的问题留给人类,我就放一想到还有95%的问题留给人类,我就放一想到还有95%的问题留给人类,我就放',style: TextStyle(fontSize: 13,fontWeight: FontWeight.w500,color: Color(0xFF333333)),maxLines: 2,overflow: TextOverflow.ellipsis,),
SizedBox(height: 5,),
Text('威廉·莎士比亚',style: TextStyle(fontSize: 11,fontWeight: FontWeight.w400,color: Color(0xFF999999))),
Text(model != null?model!.bookName.toString():'',style: const TextStyle(fontSize: 13,fontWeight: Fonts.medium,color: Colours.c3),maxLines: 2,overflow: TextOverflow.ellipsis,),
const SizedBox(height: 5,),
Text(model != null?model!.authors.toString():'',style: const TextStyle(fontSize: 11,color: Colours.c9)),
],
),
Text('¥88',style: TextStyle(fontSize: 14,fontWeight: FontWeight.w500,color: AppTheme.primary)),
Text('¥${model != null?model!.authors.toString():''}',style: const TextStyle(fontSize: 14,fontWeight: FontWeight.w500,color: AppTheme.primary)),
],
),
),
......
......@@ -16,7 +16,7 @@ class CourseController extends GetxController {
controlFinishRefresh: true,
);
final int _limit = 20;
final int _limit = 10;
int _page = 1;
bool _noMore = false;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论