提交 9a668166 authored 作者: yueweilu's avatar yueweilu

购物车 逻辑

上级 a7cce90e
part of book_pay;
class BookPayController extends GetxController {
List<PayModel> pays = [
final List<CourseModel> buy;
BookPayController(this.buy);
// 支付方式
List<PayModel> pays = Platform.isIOS ?[
PayModel(type: 3, name: '紫荆币', icon: 'assets/images/pay_coin.png', selected: true),
]:[
PayModel(type: 1, name: '微信', icon: 'assets/images/pay_wechat.png', selected: true),
PayModel(type: 2, name: '支付宝', icon: 'assets/images/pay_ali.png', selected: false),
PayModel(type: 3, name: '紫荆币', icon: 'assets/images/pay_coin.png', selected: false),
......@@ -9,7 +15,7 @@ class BookPayController extends GetxController {
// 支付方式
late PayModel _payModel;
late PayModel _payModel = pays.first;
PayModel get payModel => _payModel;
void setPayModel(PayModel payModel){
......@@ -22,8 +28,22 @@ class BookPayController extends GetxController {
model.selected = false;
}
}
print('--------------------------------${_payModel.name}');
update();
}
// /// 获取总价
// ///总价格
// double get allPrice{
// Decimal price = Decimal.zero;
// for (CourseModel model in buy) {
// if (model.status == 1){
// if (model.selected == true){
// price = price + Decimal.parse(model.vipPrice??'0.00');
// }
// }
// }
// return price.toDouble();
// }
}
\ No newline at end of file
library book_pay;
import 'package:decimal/decimal.dart';
import 'package:flutter/material.dart';
import 'package:flutter_book/theme.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
......@@ -10,6 +11,7 @@ import '../../models/index.dart';
import '../../models/shop.dart';
import '../../utils/index.dart';
import '../book_shop/index.dart';
import 'dart:io';
part 'controller.dart';
part 'view.dart';
......
part of book_pay;
class BookPayPage extends StatefulWidget {
const BookPayPage({Key? key}) : super(key: key);
final List<CourseModel> buy;
const BookPayPage({
Key? key,
required this.buy
}) : super(key: key);
@override
State<BookPayPage> createState() => _BookPayPageState();
......@@ -12,7 +16,7 @@ class _BookPayPageState extends State<BookPayPage> {
@override
Widget build(BuildContext context) {
return GetBuilder<BookPayController>(
init: BookPayController(),
init: BookPayController(widget.buy),
builder: (controller) => Scaffold(
backgroundColor: Colours.cF9,
appBar: AppBar(title: const Text('支付'),),
......@@ -23,9 +27,9 @@ class _BookPayPageState extends State<BookPayPage> {
physics: const NeverScrollableScrollPhysics(),
shrinkWrap:true,
itemBuilder: (BuildContext context, int index){
return const BuildItem();
return BuildItem(model: controller.buy[index],);
},
itemCount: 2,
itemCount: controller.buy.length,
),
const SizedBox(height: 10,),
......
part of book_pay;
class BuildItem extends StatelessWidget {
final CourseModel? model;
final CourseModel model;
const BuildItem({
super.key,
this.model,
required this.model,
});
@override
......@@ -69,12 +69,12 @@ class BuildItem extends StatelessWidget {
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(model != null?model!.bookName.toString():'',style: const TextStyle(fontSize: 13,fontWeight: Fonts.medium,color: Colours.c3),maxLines: 2,overflow: TextOverflow.ellipsis,),
Text(model.bookName??'',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(model.authors??'',style: const TextStyle(fontSize: 11,color: Colours.c9)),
],
),
Text('¥${model != null?model!.authors.toString():''}',style: const TextStyle(fontSize: 14,fontWeight: FontWeight.w500,color: AppTheme.primary)),
Text('¥${model.vipPrice??''}',style: const TextStyle(fontSize: 14,fontWeight: FontWeight.w500,color: AppTheme.primary)),
],
),
),
......
......@@ -9,17 +9,128 @@ class BookshopController extends GetxController {
// 书架数组
List<CourseModel> carts = [];
// 是否全选
late bool all = false;
final int _limit = 10;
int _page = 1;
bool _noMore = false;
@override
void onReady() {
onRefresh();
super.onReady();
}
@override
void onClose() {
refreshController.dispose();
super.onClose();
}
// 选中 与 取消选中
void select(CourseModel courseModel){
if (courseModel.status == 1){
courseModel.selected = !courseModel.selected;
}
bool checkAll = true;
for (CourseModel model in carts){
if (model.status == 1){
if (model.selected == false){
checkAll = false;
}
}
}
all = checkAll;
update();
}
// 获取选中数量
int get num {
int num = 0;
for (CourseModel model in carts){
if (model.selected == true){
num ++;
}
}
return num;
}
// 获取总价
//总价格
double get allPrice{
Decimal price = Decimal.zero;
for (CourseModel model in carts) {
if (model.status == 1){
if (model.selected == true){
price = price + Decimal.parse(model.vipPrice??'0.00');
}
}
}
return price.toDouble();
}
/* 另一种方式处理精度
* double get allPrice {
double price = 0;
for (CourseModel model in carts) {
if (model.status == 1 && model.selected == true) {
print('----------------11111----------------${model.vipPrice ?? '0.00'}');
price += double.parse(model.vipPrice ?? '0.00');
}
}
NumberFormat formatter = NumberFormat("0.00");
String formattedPrice = formatter.format(price);
print('-------------22222-------------------$formattedPrice');
return double.parse(formattedPrice);
}
* */
// 全选
void selectAll() {
if(all == false){
for (CourseModel model in carts) {
if (model.status == 1){
model.selected = true;
}
}
all = true;
}
else {
for (CourseModel model in carts) {
if (model.status == 1){
model.selected = false;
}
}
all = false;
}
update();
}
/// 购买的书籍
List<CourseModel> get buy {
List<CourseModel> temp = [];
for (CourseModel model in carts) {
if (model.status == 1){
if (model.selected == true){
temp.add(model);
}
}
}
return temp;
}
/// 删除购物车 单个书籍
void delCart({
required String cartId,
......@@ -75,9 +186,9 @@ class BookshopController extends GetxController {
List<CourseModel> _test(){
return [
CourseModel(),
CourseModel(),
CourseModel(),
CourseModel(bookName: '哈1',bookId: 111,vipPrice: '12.33',authors: 'John',status: 1),
CourseModel(bookName: '哈2',bookId: 123,vipPrice: '12.00',authors: 'json',status: 1),
CourseModel(bookName: '哈3',bookId: 11,vipPrice: '12.43',authors: 'hash',status: 1),
];
}
......
library book_shop;
import 'dart:ffi';
import 'package:decimal/decimal.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_book/utils/index.dart';
......@@ -7,12 +10,13 @@ import 'package:flutter_book/widgets/index.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:get/get.dart';
import 'package:go_router/go_router.dart';
import 'package:intl/intl.dart';
import '../../apis/index.dart';
import '../../models/index.dart';
import '../../routes/index.dart';
import '../../theme.dart';
import 'dart:math';
part 'controller.dart';
part 'view.dart';
......
......@@ -19,22 +19,22 @@ class _BookShopPageState extends State<BookShopPage> {
body: Column(
children: [
Expanded(
child: CustomPullScrollView(
controller: controller.refreshController,
onRefresh: controller.onRefresh,
onLoading: controller.onLoading,
child: ListView.builder(
itemBuilder: (BuildContext context,int index){
CourseModel model = controller.carts[index];
return BookCell(model:model ,onTap: (){
child: ListView.builder(
itemBuilder: (BuildContext context,int index){
CourseModel model = controller.carts[index];
return GestureDetector(
onTap: (){
controller.select(model);
},
child: BookCell(model:model ,delTap: (){
controller.delCart(cartId: model.cartId.toString());
},);
},
itemCount: controller.carts.length,
),
},),
);
},
itemCount: controller.carts.length,
),
),
BuildCounter()
BuildCounter(controller: controller,)
],
),
),
......
......@@ -2,11 +2,11 @@ part of book_shop;
class BookCell extends StatelessWidget {
final CourseModel model;
final void Function() onTap;
final void Function() delTap;
const BookCell({
super.key,
required this.model,
required this.onTap
required this.delTap
});
@override
......@@ -17,13 +17,13 @@ class BookCell extends StatelessWidget {
color: Colors.white,
boxShadow: [
BoxShadow(
color: const Color(0xFFC7C7C7).withOpacity(0.5),
color: model.selected?AppTheme.primary.withOpacity(0.21):const Color(0xFFC7C7C7).withOpacity(0.5),
spreadRadius: 0,
blurRadius: 10,
offset: const Offset(3, 0), // changes the position of the shadow
),
],
border:Border.all(width: 0.5,color: AppTheme.primary)
border:model.selected?Border.all(width: 0.5,color: AppTheme.primary):null,
),
margin: const EdgeInsets.only(left: 10,right: 10,top: 10),
height: 110,
......@@ -34,7 +34,7 @@ class BookCell extends StatelessWidget {
SlidableAction(
// An action can be bigger than the others.
onPressed: (BuildContext context){
onTap;
delTap;
},
backgroundColor: AppTheme.primary,
foregroundColor: Colors.white,
......@@ -50,10 +50,11 @@ class BookCell extends StatelessWidget {
margin: const EdgeInsets.only(left: 12,right: 11),
child: Row(
children: [
Container(
SizedBox(
height: 17,
width: 17,
color: Colors.cyan,
// color: Colors.cyan,
child: Image.asset(model.selected?'assets/images/pay_check.png':'assets/images/pay_uncheck.png'),
),
const SizedBox(width: 12,),
Container(
......@@ -94,12 +95,12 @@ class BookCell extends StatelessWidget {
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(model != null?model!.bookName.toString():'',style: const TextStyle(fontSize: 13,fontWeight: Fonts.medium,color: Colours.c3),maxLines: 2,overflow: TextOverflow.ellipsis,),
Text(model.bookName??'',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(model.authors??'',style: const TextStyle(fontSize: 11,color: Colours.c9)),
],
),
Text('¥${model != null?model!.authors.toString():''}',style: const TextStyle(fontSize: 14,fontWeight: FontWeight.w500,color: AppTheme.primary)),
Text('¥${model.vipPrice??''}',style: const TextStyle(fontSize: 14,fontWeight: FontWeight.w500,color: AppTheme.primary)),
],
),
),
......
......@@ -2,7 +2,11 @@ part of book_shop;
class BuildCounter extends StatelessWidget {
const BuildCounter({Key? key}) : super(key: key);
final BookshopController controller;
const BuildCounter({
Key? key,
required this.controller
}) : super(key: key);
@override
Widget build(BuildContext context) {
......@@ -16,26 +20,40 @@ class BuildCounter extends StatelessWidget {
children: [
Row(
children: [
Container(
height: 17,
width: 17,
color: Colors.yellow,
),
const SizedBox(width: 11),
const Text('全选',style: TextStyle(color: Color(0xFF333333),fontSize: 12,fontWeight: FontWeight.w500)),
GestureDetector(
onTap: (){
controller.selectAll();
},
child: Container(
color: Colors.white,
child: Row(
children: [
SizedBox(
height: 17,
width: 17,
child: Image.asset(controller.all?'assets/images/pay_check.png':'assets/images/pay_uncheck.png'),
),
const SizedBox(width: 11),
const Text('全选',style: TextStyle(color: Color(0xFF333333),fontSize: 12,fontWeight: FontWeight.w500)),
],
),
),
),
const SizedBox(width: 15,),
const Column(
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('合计 ¥98.9',style: TextStyle(color: AppTheme.primary,fontSize: 12,fontWeight: FontWeight.w500)),
Text('已选 2 件',style: TextStyle(color: Color(0xFF999999),fontSize: 10,))
Text('合计 ¥${controller.allPrice.toStringAsFixed(2)}',style: const TextStyle(color: AppTheme.primary,fontSize: 12,fontWeight: FontWeight.w500)),
Text('已选 ${controller.num} 件',style: const TextStyle(color: Color(0xFF999999),fontSize: 10,))
],
)
],
),
GestureDetector(
onTap: (){
context.pushNamed(Routes.bookPay);
if (controller.num > 0){
context.pushNamed(Routes.bookPay,extra: controller.buy);
}
},
child: Container(
margin: const EdgeInsets.symmetric(vertical: 9),
......@@ -46,7 +64,7 @@ class BuildCounter extends StatelessWidget {
// height: 35,
alignment: Alignment.center,
padding: const EdgeInsets.symmetric(vertical: 8,horizontal: 20),
child: Text('结算(2)',style: TextStyle(fontSize: 14,color: Colors.white,height: 1.1,fontWeight: Fonts.medium),),
child: Text('结算(${controller.num})',style: const TextStyle(fontSize: 14,color: Colors.white,height: 1.1,fontWeight: Fonts.medium),),
),
)
],
......
......@@ -29,6 +29,7 @@ import 'package:flutter_book/pages/user_wrong/index.dart';
import 'package:flutter_book/pages/web/index.dart';
import 'package:go_router/go_router.dart';
import '../models/index.dart';
import '../pages/user_terms/index.dart';
import '../store/index.dart';
......
......@@ -168,7 +168,7 @@ abstract class Routes {
pageBuilder: (context, state) =>CupertinoPage(
name: state.uri.toString(),
key: state.pageKey,
child: const BookPayPage()
child: BookPayPage(buy: state.extra as List<CourseModel>,)
)
),
GoRoute(
......
......@@ -89,6 +89,14 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.0.6"
decimal:
dependency: "direct main"
description:
name: decimal
sha256: "24a261d5d5c87e86c7651c417a5dbdf8bcd7080dd592533910e8d0505a279f21"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.3.3"
dio:
dependency: "direct main"
description:
......@@ -525,6 +533,14 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.0.1"
rational:
dependency: transitive
description:
name: rational
sha256: ba58e9e18df9abde280e8b10051e4bce85091e41e8e7e411b6cde2e738d357cf
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.2.2"
recase:
dependency: transitive
description:
......
......@@ -79,8 +79,10 @@ dependencies:
flutter_star: ^1.2.0
# toast
oktoast: ^3.0.0
# 策划
# 侧滑
flutter_slidable: ^3.0.1
# 精度
decimal: ^2.3.3
dev_dependencies:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论