Skip to content

Commit 76fa292

Browse files
author
罗国雄
committed
商品页bug
1 parent 5ffc8c3 commit 76fa292

File tree

6 files changed

+172
-72
lines changed

6 files changed

+172
-72
lines changed

lib/Advertisement/openAd.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ class OpenAd extends StatelessWidget {
1313
width: double.infinity,
1414
height: double.infinity,
1515
decoration: BoxDecoration(
16-
image: DecorationImage(
17-
image: AssetImage('assets/images/timg.jpg'),
18-
fit: BoxFit.cover,
19-
)),
16+
image: DecorationImage(
17+
image: AssetImage('assets/images/timg.jpg'),
18+
fit: BoxFit.cover,
19+
),
20+
),
2021
child: Stack(
2122
children: <Widget>[
2223
Positioned(

lib/api/index.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,18 @@ class Api {
9191
}
9292

9393
// 商品信息
94-
static Future getGoodsMSG({int id}) async {
95-
return await http.get('/goods/detail', {'id': id});
94+
static Future getGoodsMSG({int id, String token}) async {
95+
return await http.getToken('/goods/detail', token, {'id': id});
9696
}
9797

9898
// 获取购物信息
9999
static Future getCartMsg({String token, int id}) async {
100100
return await http.getToken('/cart/index', token, {'id': id});
101101
}
102+
103+
// 添加到购物车
104+
static Future postAddCart({String token, int id}) async {
105+
return await http
106+
.postToken('/collect/addordelete', token, {'valueId': id, 'typeId': 0});
107+
}
102108
}

lib/component/SliverCustomHeader.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class SliverCustomHeaderDelegate extends SliverPersistentHeaderDelegate {
7171
children: <Widget>[
7272
IconButton(
7373
icon: Icon(
74-
Icons.arrow_back_ios,
74+
Icons.keyboard_arrow_left,
7575
color: this.makeStickyHeaderTextColor(
7676
shrinkOffset, true), // 返回图标颜色
7777
),

lib/component/count.dart

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import 'package:flutter/material.dart';
22
import 'package:easy_market/utils/rem.dart';
33

4-
Color borderColor = Color.fromARGB(255, 0, 191, 255);
4+
// Color borderColor = Color.fromARGB(255, 0, 191, 255);
5+
Color borderColor = Colors.grey;
56

67
class Count extends StatelessWidget {
78
Count({
@@ -24,30 +25,32 @@ class Count extends StatelessWidget {
2425
}
2526
}
2627

28+
// 相当于render
2729
Widget build(BuildContext context) {
2830
return Container(
29-
height: Rem.getPxToRem(80),
30-
width: Rem.getPxToRem(280),
31+
height: Rem.getPxToRem(70),
32+
width: Rem.getPxToRem(260),
3133
decoration: BoxDecoration(
32-
border: Border.all(
33-
color: borderColor,
34-
)),
34+
border: Border.all(
35+
color: borderColor,
36+
),
37+
),
3538
child: Row(
3639
children: <Widget>[
3740
Container(
38-
width: Rem.getPxToRem(80),
41+
width: Rem.getPxToRem(70),
3942
color: this.min >= this.number ? Colors.grey[200] : Colors.white,
40-
child: Center(
41-
child: IconButton(
42-
icon: Icon(
43+
child: InkResponse(
44+
child: Center(
45+
child: Icon(
4346
Icons.remove,
4447
color:
4548
this.min >= this.number ? Colors.grey[500] : Colors.black,
4649
),
47-
onPressed: () {
48-
this.onClickBtn('remove');
49-
},
5050
),
51+
onTap: () {
52+
this.onClickBtn('remove');
53+
},
5154
),
5255
),
5356
Expanded(
@@ -67,19 +70,19 @@ class Count extends StatelessWidget {
6770
),
6871
),
6972
Container(
70-
width: Rem.getPxToRem(80),
73+
width: Rem.getPxToRem(70),
7174
color: this.max <= this.number ? Colors.grey[200] : Colors.white,
72-
child: Center(
73-
child: IconButton(
74-
icon: Icon(
75+
child: InkResponse(
76+
child: Center(
77+
child: Icon(
7578
Icons.add,
7679
color:
7780
this.max <= this.number ? Colors.grey[500] : Colors.black,
7881
),
79-
onPressed: () {
80-
this.onClickBtn('add');
81-
},
8282
),
83+
onTap: () {
84+
this.onClickBtn('add');
85+
},
8386
),
8487
),
8588
],

lib/router/goodsDetail/index.dart

Lines changed: 134 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import 'package:easy_market/api/index.dart';
1414
import 'package:easy_market/component/bottom_sheet.dart';
1515
import 'package:easy_market/component/SliverCustomHeader.dart';
1616
import 'package:easy_market/component/count.dart';
17+
import 'package:easy_market/router/index.dart';
1718

1819
class GoodsDetail extends StatefulWidget {
1920
GoodsDetail({this.arguments});
@@ -45,6 +46,10 @@ class _GoodsDetail extends State<GoodsDetail> {
4546

4647
int goodsMax = 0; //商品最多
4748

49+
String userToken; //用户token
50+
51+
int userHasCollect; //用户是否收藏0:否;1:是
52+
4853
// 我不知道是否应该在这里进行数据请求,但我需要获取provider的值
4954
@override
5055
void didChangeDependencies() {
@@ -55,16 +60,15 @@ class _GoodsDetail extends State<GoodsDetail> {
5560
getInitData() async {
5661
int id = widget.arguments['id'];
5762
final model = Provider.of<Model>(this.context);
58-
List api = [Api.getGoodsMSG(id: id)];
63+
List<Future> api = [
64+
Api.getGoodsMSG(id: id, token: model.token == null ? null : model.token)
65+
];
5966
if (model.token != null) {
6067
api.add(
6168
Api.getCartMsg(token: model.token, id: id),
6269
);
6370
}
64-
var data = await Future.wait([
65-
Api.getGoodsMSG(id: id),
66-
Api.getCartMsg(token: model.token, id: id),
67-
]);
71+
var data = await Future.wait(api);
6872
var goodsMsg = data[0].data;
6973
var cartData = model.token != null ? data[1].data : null;
7074

@@ -84,11 +88,13 @@ class _GoodsDetail extends State<GoodsDetail> {
8488
setState(() {
8589
imgList = goodsMsg['gallery'];
8690
goodsMsgs = goodsMsg;
91+
userHasCollect = goodsMsg['userHasCollect'];
8792
initLoading = false;
8893
goodsMax = goodsStockPriceAny['goods_number'];
8994
chooseSizeIndex = sizeIndex;
9095
chooseSizeStr = sizeNameList.join('、');
9196
goodsStockPrice = goodsStockPriceAny;
97+
userToken = model.token;
9298
goodsCount =
9399
model.token != null ? cartData['cartTotal']['goodsCount'] : 0;
94100
});
@@ -118,26 +124,39 @@ class _GoodsDetail extends State<GoodsDetail> {
118124
padding: EdgeInsets.symmetric(vertical: Rem.getPxToRem(10)),
119125
child: Row(
120126
children: <Widget>[
121-
Container(
122-
width: Rem.getPxToRem(100),
123-
child: Column(
124-
children: <Widget>[
125-
Expanded(
126-
child: Icon(
127-
Icons.star,
128-
color: Colors.yellow,
129-
size: Rem.getPxToRem(50),
130-
),
131-
),
132-
Center(
133-
child: Text(
134-
'收藏',
135-
style: TextStyle(
136-
color: Colors.grey, fontSize: Rem.getPxToRem(22)),
127+
InkResponse(
128+
child: Container(
129+
width: Rem.getPxToRem(100),
130+
child: Column(
131+
children: <Widget>[
132+
Expanded(
133+
child: Icon(
134+
userHasCollect == 1 ? Icons.star : Icons.star_border,
135+
color: userHasCollect == 1 ? Colors.yellow : Colors.grey,
136+
size: Rem.getPxToRem(50),
137+
),
137138
),
138-
)
139-
],
139+
Center(
140+
child: Text(
141+
'收藏',
142+
style: TextStyle(
143+
color: Colors.grey, fontSize: Rem.getPxToRem(22)),
144+
),
145+
)
146+
],
147+
),
140148
),
149+
onTap: () async {
150+
if (userToken != null) {
151+
await Api.postAddCart(
152+
id: widget.arguments['id'], token: userToken);
153+
setState(() {
154+
userHasCollect = userHasCollect == 1 ? 0 : 1;
155+
});
156+
} else {
157+
Router.push('/login', context);
158+
}
159+
},
141160
),
142161
Container(
143162
width: Rem.getPxToRem(110),
@@ -154,28 +173,30 @@ class _GoodsDetail extends State<GoodsDetail> {
154173
color: Colors.grey,
155174
size: Rem.getPxToRem(50),
156175
),
157-
Positioned(
158-
top: 0,
159-
right: Rem.getPxToRem(20),
160-
child: Container(
161-
padding: EdgeInsets.symmetric(horizontal: 2),
162-
decoration: BoxDecoration(
163-
color: Colors.white,
164-
border: Border.all(color: Colors.red),
165-
borderRadius: BorderRadius.all(
166-
Radius.circular(30),
167-
),
168-
),
169-
child: Text(
170-
'$goodsCount',
171-
style: TextStyle(
172-
color: Colors.red,
173-
fontSize: Rem.getPxToRem(20),
174-
fontWeight: FontWeight.bold,
175-
),
176-
),
177-
),
178-
)
176+
userToken != null
177+
? Positioned(
178+
top: 0,
179+
right: Rem.getPxToRem(20),
180+
child: Container(
181+
padding: EdgeInsets.symmetric(horizontal: 2),
182+
decoration: BoxDecoration(
183+
color: Colors.white,
184+
border: Border.all(color: Colors.red),
185+
borderRadius: BorderRadius.all(
186+
Radius.circular(30),
187+
),
188+
),
189+
child: Text(
190+
'$goodsCount',
191+
style: TextStyle(
192+
color: Colors.red,
193+
fontSize: Rem.getPxToRem(20),
194+
fontWeight: FontWeight.bold,
195+
),
196+
),
197+
),
198+
)
199+
: Container(),
179200
],
180201
),
181202
),
@@ -626,6 +647,74 @@ class _GoodsDetail extends State<GoodsDetail> {
626647
buildSizeItem(context, setstate1),
627648
// 商品数量
628649
buildSizeNun(context, setstate1),
650+
// 下单或加入购物车
651+
Container(
652+
height: Rem.getPxToRem(80),
653+
child: Row(
654+
children: <Widget>[
655+
Expanded(
656+
child: Container(
657+
padding: EdgeInsets.fromLTRB(
658+
Rem.getPxToRem(20),
659+
0,
660+
Rem.getPxToRem(10),
661+
0,
662+
),
663+
child: Center(
664+
child: Container(
665+
child: Center(
666+
child: Text(
667+
'加入购物车',
668+
style: TextStyle(
669+
color: Colors.white,
670+
fontWeight: FontWeight.bold,
671+
),
672+
),
673+
),
674+
decoration: BoxDecoration(
675+
color: Colors.red,
676+
borderRadius: BorderRadius.all(
677+
Radius.circular(Rem.getPxToRem(80)),
678+
),
679+
),
680+
),
681+
),
682+
),
683+
),
684+
Expanded(
685+
child: Container(
686+
padding: EdgeInsets.fromLTRB(
687+
Rem.getPxToRem(10),
688+
0,
689+
Rem.getPxToRem(20),
690+
0,
691+
),
692+
child: Center(
693+
child: Container(
694+
child: Center(
695+
child: Text(
696+
'立即购买',
697+
style: TextStyle(
698+
color: Colors.white,
699+
fontWeight: FontWeight.bold,
700+
),
701+
),
702+
),
703+
width: double.infinity,
704+
height: double.infinity,
705+
decoration: BoxDecoration(
706+
color: Colors.orange,
707+
borderRadius: BorderRadius.all(
708+
Radius.circular(Rem.getPxToRem(80)),
709+
),
710+
),
711+
),
712+
),
713+
),
714+
)
715+
],
716+
),
717+
)
629718
],
630719
),
631720
);

lib/utils/cache.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class SpUtil {
2121
_spf = await SharedPreferences.getInstance();
2222
}
2323

24+
// 避免重复初始化
2425
static Future<SpUtil> getInstance() async {
2526
if (_instance == null) {
2627
_instance = new SpUtil._();

0 commit comments

Comments
 (0)