@@ -14,6 +14,7 @@ import 'package:easy_market/api/index.dart';
1414import 'package:easy_market/component/bottom_sheet.dart' ;
1515import 'package:easy_market/component/SliverCustomHeader.dart' ;
1616import 'package:easy_market/component/count.dart' ;
17+ import 'package:easy_market/router/index.dart' ;
1718
1819class 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 );
0 commit comments