Skip to content

Commit 3dcad85

Browse files
author
罗国雄
committed
完善商品页
1 parent 776d1c8 commit 3dcad85

File tree

6 files changed

+238
-55
lines changed

6 files changed

+238
-55
lines changed

lib/page/cart/index.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import 'package:flutter/material.dart';
2-
import 'package:provider/provider.dart';
3-
import 'package:easy_market/model/index.dart';
42
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
53

64
class Cart extends StatefulWidget {

lib/router/goodsDetail/index.dart

Lines changed: 187 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
import 'package:flutter/material.dart';
88
import 'package:flutter_swiper/flutter_swiper.dart';
99
import 'package:cached_network_image/cached_network_image.dart';
10-
import 'package:provider/provider.dart';
11-
import 'package:easy_market/model/index.dart';
1210
import 'package:easy_market/utils/rem.dart';
11+
import 'package:easy_market/utils/cache.dart';
1312
import 'package:easy_market/api/index.dart';
1413
import 'package:easy_market/component/bottom_sheet.dart';
1514
import 'package:easy_market/component/SliverCustomHeader.dart';
1615
import 'package:easy_market/component/count.dart';
1716
import 'package:easy_market/router/index.dart';
17+
import 'package:flutter_html/flutter_html.dart';
1818

1919
class GoodsDetail extends StatefulWidget {
2020
GoodsDetail({this.arguments});
@@ -50,27 +50,27 @@ class _GoodsDetail extends State<GoodsDetail> {
5050

5151
int userHasCollect; //用户是否收藏0:否;1:是
5252

53-
// 我不知道是否应该在这里进行数据请求,但我需要获取provider的值
5453
@override
55-
void didChangeDependencies() {
56-
super.didChangeDependencies();
54+
void initState() {
55+
super.initState();
5756
getInitData();
5857
}
5958

6059
getInitData() async {
6160
int id = widget.arguments['id'];
62-
final model = Provider.of<Model>(this.context);
61+
var sq = await SpUtil.getInstance();
62+
final token = sq.getString('token');
6363
List<Future> api = [
64-
Api.getGoodsMSG(id: id, token: model.token == null ? null : model.token)
64+
Api.getGoodsMSG(id: id, token: token == null ? null : token)
6565
];
66-
if (model.token != null) {
66+
if (token != null) {
6767
api.add(
68-
Api.getCartMsg(token: model.token, id: id),
68+
Api.getCartMsg(token: token, id: id),
6969
);
7070
}
7171
var data = await Future.wait(api);
7272
var goodsMsg = data[0].data;
73-
var cartData = model.token != null ? data[1].data : null;
73+
var cartData = token != null ? data[1].data : null;
7474

7575
var specificationList = goodsMsg['specificationList'];
7676
List<int> sizeIndex = [];
@@ -94,9 +94,8 @@ class _GoodsDetail extends State<GoodsDetail> {
9494
chooseSizeIndex = sizeIndex;
9595
chooseSizeStr = sizeNameList.join('、');
9696
goodsStockPrice = goodsStockPriceAny;
97-
userToken = model.token;
98-
goodsCount =
99-
model.token != null ? cartData['cartTotal']['goodsCount'] : 0;
97+
userToken = token;
98+
goodsCount = token != null ? cartData['cartTotal']['goodsCount'] : 0;
10099
});
101100
}
102101

@@ -154,7 +153,11 @@ class _GoodsDetail extends State<GoodsDetail> {
154153
userHasCollect = userHasCollect == 1 ? 0 : 1;
155154
});
156155
} else {
157-
Router.push('/login', context);
156+
Router.push('/login', context, null, () async {
157+
var sq = await SpUtil.getInstance();
158+
final token = sq.getString('token');
159+
print(token);
160+
});
158161
}
159162
},
160163
),
@@ -277,9 +280,9 @@ class _GoodsDetail extends State<GoodsDetail> {
277280
color: Colors.white,
278281
boxShadow: [
279282
new BoxShadow(
280-
color: Colors.grey[200],
283+
color: Colors.grey[300],
281284
blurRadius: Rem.getPxToRem(1),
282-
spreadRadius: 0.1,
285+
spreadRadius: 0.2,
283286
)
284287
],
285288
),
@@ -318,9 +321,31 @@ class _GoodsDetail extends State<GoodsDetail> {
318321
buildOneWidget(
319322
buildSize(context),
320323
),
321-
buildOneWidget(Container(
322-
height: 500,
323-
))
324+
buildOneWidget(
325+
buildComment(context),
326+
),
327+
buildOneWidget(Column(
328+
children: <Widget>[
329+
Container(
330+
margin: EdgeInsets.only(top: 10),
331+
height: Rem.getPxToRem(100),
332+
color: Colors.white,
333+
width: double.infinity,
334+
alignment: Alignment.center,
335+
child: Text(
336+
'商品详情',
337+
style: TextStyle(
338+
color: Colors.orange,
339+
fontWeight: FontWeight.bold,
340+
),
341+
),
342+
),
343+
Html(
344+
data: goodsMsgs['info']['goods_desc']
345+
.replaceAll('<p><br/></p>', ''),
346+
),
347+
],
348+
)),
324349
],
325350
);
326351
}
@@ -480,49 +505,166 @@ class _GoodsDetail extends State<GoodsDetail> {
480505

481506
// 选择规格
482507
Widget buildSize(BuildContext context) {
508+
return InkResponse(
509+
child: Container(
510+
height: Rem.getPxToRem(100),
511+
margin: EdgeInsets.only(top: 10),
512+
decoration: BoxDecoration(
513+
color: Colors.white,
514+
),
515+
child: Row(
516+
children: <Widget>[
517+
Padding(
518+
padding: EdgeInsets.all(5),
519+
child: Text(
520+
'已选',
521+
style: TextStyle(color: Colors.grey),
522+
),
523+
),
524+
Expanded(
525+
child: Padding(
526+
padding: EdgeInsets.all(5),
527+
child: Row(
528+
children: <Widget>[
529+
Expanded(
530+
child: Text(
531+
'${chooseSizeStr.length > 0 ? chooseSizeStr : '该商品没有size!'}',
532+
overflow: TextOverflow.ellipsis,
533+
),
534+
),
535+
Text('x$goodsNumber'),
536+
],
537+
),
538+
),
539+
),
540+
Padding(
541+
padding: EdgeInsets.all(5),
542+
child: Icon(
543+
Icons.keyboard_arrow_right,
544+
color: Colors.grey,
545+
),
546+
),
547+
],
548+
),
549+
),
550+
onTap: () {
551+
buildSizeModel(context);
552+
},
553+
);
554+
}
555+
556+
// 商品评论
557+
Widget buildComment(BuildContext context) {
558+
List<Widget> imgs = [];
559+
if (goodsMsgs['comment']['count'] > 0) {
560+
var imgListMap = goodsMsgs['comment']['data']['pic_list'];
561+
for (int i = 0; i < imgListMap.length; i++) {
562+
imgs.add(Container(
563+
height: Rem.getPxToRem(200),
564+
width: Rem.getPxToRem(200),
565+
child: CachedNetworkImage(
566+
imageUrl: imgListMap[i]['pic_url'],
567+
fit: BoxFit.cover,
568+
),
569+
));
570+
}
571+
}
483572
return Container(
484-
height: Rem.getPxToRem(100),
485573
margin: EdgeInsets.only(top: 10),
486574
decoration: BoxDecoration(
487575
color: Colors.white,
488576
),
489-
child: Row(
577+
child: Column(
490578
children: <Widget>[
491-
Padding(
492-
padding: EdgeInsets.all(5),
493-
child: Text(
494-
'已选',
495-
style: TextStyle(color: Colors.grey),
496-
),
497-
),
498-
Expanded(
499-
child: Padding(
500-
padding: EdgeInsets.all(5),
579+
InkResponse(
580+
child: Container(
581+
height: Rem.getPxToRem(100),
501582
child: Row(
502583
children: <Widget>[
503-
Expanded(
584+
Container(
585+
padding: EdgeInsets.only(left: 5),
504586
child: Text(
505-
'${chooseSizeStr.length > 0 ? chooseSizeStr : '该商品没有size!'}',
506-
overflow: TextOverflow.ellipsis,
587+
'评论',
588+
style: TextStyle(color: Colors.grey),
589+
),
590+
),
591+
Expanded(
592+
child: Container(
593+
padding: EdgeInsets.only(left: 10),
594+
child: Text(
595+
'${goodsMsgs['comment']['count']}条',
596+
),
597+
),
598+
),
599+
Padding(
600+
padding: EdgeInsets.only(right: 5),
601+
child: Icon(
602+
Icons.keyboard_arrow_right,
603+
color: Colors.grey,
507604
),
508605
),
509-
Text('x$goodsNumber'),
510606
],
511607
),
512608
),
513-
),
514-
InkResponse(
515-
child: Padding(
516-
padding: EdgeInsets.all(5),
517-
child: Icon(
518-
Icons.keyboard_arrow_right,
519-
color: Colors.grey,
520-
),
521-
),
522609
onTap: () {
523-
buildSizeModel(context);
610+
if (goodsMsgs['comment']['count'] > 0) {
611+
Router.push(
612+
'/moreComment', context, {'id': widget.arguments['id']});
613+
}
524614
},
525615
),
616+
goodsMsgs['comment']['count'] > 0
617+
? Container(
618+
height: Rem.getPxToRem(80),
619+
decoration: BoxDecoration(
620+
border: Border(
621+
top: BorderSide(
622+
color: Colors.grey,
623+
width: .5,
624+
),
625+
),
626+
),
627+
padding: EdgeInsets.symmetric(horizontal: 5),
628+
child: Row(
629+
children: <Widget>[
630+
Expanded(
631+
child: Text('匿名用户'),
632+
),
633+
Text(
634+
'${goodsMsgs['comment']['data']['add_time']}',
635+
style: TextStyle(
636+
color: Colors.grey,
637+
),
638+
),
639+
],
640+
),
641+
)
642+
: Container(),
643+
goodsMsgs['comment']['count'] > 0
644+
? Container(
645+
height: Rem.getPxToRem(80),
646+
width: double.infinity,
647+
padding: EdgeInsets.symmetric(horizontal: 5),
648+
child: Text(
649+
'${goodsMsgs['comment']['data']['content']}',
650+
softWrap: true,
651+
maxLines: 2,
652+
overflow: TextOverflow.ellipsis,
653+
style: TextStyle(
654+
color: Colors.grey,
655+
),
656+
),
657+
)
658+
: Container(),
659+
goodsMsgs['comment']['count'] > 0
660+
? Container(
661+
padding: EdgeInsets.fromLTRB(5, 0, 5, 5),
662+
width: double.infinity,
663+
child: Wrap(
664+
children: imgs,
665+
),
666+
)
667+
: Container()
526668
],
527669
),
528670
);

lib/router/index.dart

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,44 @@ class Router {
5454

5555
// 组件跳转
5656
static link(Widget child, String routeName, BuildContext context,
57-
[Map parmas]) {
57+
[Map parmas, Function callBack]) {
5858
return GestureDetector(
5959
onTap: () {
6060
if (parmas != null) {
61-
Navigator.pushNamed(context, routeName, arguments: parmas);
61+
Navigator.pushNamed(context, routeName, arguments: parmas)
62+
.then((onValue) {
63+
if (callBack != null) {
64+
callBack();
65+
}
66+
});
6267
} else {
63-
Navigator.pushNamed(context, routeName);
68+
Navigator.pushNamed(context, routeName).then((onValue) {
69+
if (callBack != null) {
70+
callBack();
71+
}
72+
});
6473
}
6574
},
6675
child: child,
6776
);
6877
}
6978

7079
// 方法跳转
71-
static push(String routeName, BuildContext context, [Map parmas]) {
80+
static push(String routeName, BuildContext context,
81+
[Map parmas, Function callBack]) {
7282
if (parmas != null) {
73-
Navigator.pushNamed(context, routeName, arguments: parmas);
83+
Navigator.pushNamed(context, routeName, arguments: parmas)
84+
.then((onValue) {
85+
if (callBack != null) {
86+
callBack();
87+
}
88+
});
7489
} else {
75-
Navigator.pushNamed(context, routeName);
90+
Navigator.pushNamed(context, routeName).then((onValue) {
91+
if (callBack != null) {
92+
callBack();
93+
}
94+
});
7695
}
7796
}
7897

lib/utils/rem.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,21 @@ import 'package:flutter/material.dart';
88

99
class Rem {
1010
static double _designWidth = 750.0;
11+
static double _windowWidth;
1112
static bool _isInit = false;
1213

1314
static double getPxToRem(val) {
14-
return MediaQueryData.fromWindow(ui.window).size.width * val / _designWidth;
15+
return _windowWidth * val / _designWidth;
1516
}
1617

1718
static double getRemToPx(val) {
18-
return val * _designWidth / MediaQueryData.fromWindow(ui.window).size.width;
19+
return val * _designWidth / _windowWidth;
1920
}
2021

2122
static setDesignWidth(val) {
2223
if (!_isInit) {
2324
_designWidth = val;
25+
_windowWidth = MediaQueryData.fromWindow(ui.window).size.width;
2426
_isInit = true;
2527
} else {
2628
// print('is init!!');

0 commit comments

Comments
 (0)