release_goods_page.dart 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  1. import 'dart:convert';
  2. import 'dart:io';
  3. import 'package:bbyyy/beans/commodity_category_bean_entity.dart';
  4. import 'package:bbyyy/beans/goods_bean_entity.dart';
  5. import 'package:bbyyy/beans/my_shop_bean_entity.dart';
  6. import 'package:bbyyy/beans/upload_image_bean_entity.dart';
  7. import 'package:bbyyy/https/MyDio.dart';
  8. import 'package:bbyyy/https/my_request.dart';
  9. import 'package:bbyyy/https/url.dart';
  10. import 'package:bbyyy/my_tools/const.dart';
  11. import 'package:bbyyy/my_tools/dims.dart';
  12. import 'package:bbyyy/my_tools/easy_loading/easy_loading.dart';
  13. import 'package:bbyyy/my_tools/event_bus.dart';
  14. import 'package:bbyyy/my_tools/my_colors.dart';
  15. import 'package:bbyyy/my_tools/my_cookie.dart';
  16. import 'package:bbyyy/my_tools/my_datas.dart';
  17. import 'package:bbyyy/my_tools/my_tools.dart';
  18. import 'package:bbyyy/my_tools/my_views.dart';
  19. import 'package:flutter/cupertino.dart';
  20. import 'package:flutter/material.dart';
  21. import 'package:flutter/services.dart';
  22. import 'package:flutter_svg/flutter_svg.dart';
  23. class ReleaseGoodsPage extends StatefulWidget {
  24. GoodsBeanDataData data;
  25. ReleaseGoodsPage(this.data);
  26. @override
  27. _ReleaseGoodsPageState createState() => _ReleaseGoodsPageState();
  28. }
  29. class _ReleaseGoodsPageState extends State<ReleaseGoodsPage> {
  30. List<DropdownMenuItem<String>> sortItems = [];
  31. List<DropdownMenuItem<String>> sortItems1 = [];
  32. String typeValue = '';
  33. String typeValue1 = '';
  34. File _frontCover;
  35. TextEditingController _title = TextEditingController();
  36. TextEditingController _costOfProduction = TextEditingController();
  37. TextEditingController _markedPrice = TextEditingController();
  38. TextEditingController _num = TextEditingController();
  39. TextEditingController _description = TextEditingController();
  40. List<MyShopBeanDataData> shops = [];
  41. bool onSale = true;
  42. List<CommodityCategoryBeanData> commodityCategoryBeanData = [];
  43. var detailsPictures = [];
  44. @override
  45. initState() {
  46. // TODO: implement initState
  47. super.initState();
  48. detailsPictures.add(null);
  49. queryGang();
  50. initCC();
  51. if (widget.data != null) {
  52. print(widget.data.pictures);
  53. _title.text = widget.data.title;
  54. _costOfProduction.text = widget.data.originalPrice.toString();
  55. onSale = widget.data.onSale;
  56. _markedPrice.text = widget.data.price.toString();
  57. _num.text = widget.data.stock.toString();
  58. _description.text = widget.data.description;
  59. picturesPath = widget.data.pictures;
  60. picturesPath.split(',').forEach((element) {
  61. if (detailsPictures.length < 6) {
  62. detailsPictures.insert(detailsPictures.length - 1, element);
  63. } else if (detailsPictures.length == 6) {
  64. detailsPictures[5] = element;
  65. }
  66. });
  67. }
  68. }
  69. @override
  70. Widget build(BuildContext context) {
  71. return GestureDetector(
  72. onTap: () {
  73. MyTools().hideKeyboard(context);
  74. },
  75. child: Scaffold(
  76. body: Column(
  77. children: [
  78. MyViews()
  79. .myAppBar(widget.data == null ? '发布商品' : '修改商品', context, []),
  80. Expanded(
  81. child: SingleChildScrollView(
  82. child: Column(
  83. children: [
  84. Container(
  85. color: Colors.white,
  86. child: Column(
  87. children: [
  88. Padding(
  89. padding: EdgeInsets.only(
  90. left: 16, right: 13, top: 15, bottom: 15),
  91. child: Row(
  92. children: [
  93. Text(
  94. '商品名称',
  95. style: TextStyle(
  96. color: MyColors.c333333, fontSize: 15),
  97. ),
  98. Expanded(
  99. child: Container(
  100. alignment: Alignment.centerRight,
  101. child: TextField(
  102. textAlign: TextAlign.end,
  103. controller: _title,
  104. cursorColor: MyColors.cFF4233,
  105. cursorWidth: 1.0,
  106. onTap: () {},
  107. decoration: InputDecoration(
  108. border: InputBorder.none,
  109. disabledBorder: InputBorder.none,
  110. enabledBorder: InputBorder.none,
  111. focusedBorder: InputBorder.none,
  112. isDense: true,
  113. hintText: '请输入',
  114. hintStyle: TextStyle(
  115. color: MyColors.c999999,
  116. fontSize: 14),
  117. contentPadding:
  118. const EdgeInsets.fromLTRB(
  119. 14, 4.5, 8, 4.5),
  120. ),
  121. maxLines: 1,
  122. style: TextStyle(
  123. color: MyColors.c333333,
  124. fontSize: 14,
  125. height: 1.3,
  126. letterSpacing: 0.2),
  127. keyboardType: TextInputType.text,
  128. onChanged: (t) {},
  129. ),
  130. ),
  131. )
  132. ],
  133. ),
  134. ),
  135. Container(
  136. height: 0.5,
  137. color: MyColors.cE7E7E7,
  138. margin: EdgeInsets.only(left: 16, right: 14),
  139. ),
  140. Padding(
  141. padding: EdgeInsets.only(
  142. left: 16, right: 13, top: 15, bottom: 15),
  143. child: Row(
  144. children: [
  145. Text(
  146. '封面图片',
  147. style: TextStyle(
  148. color: MyColors.c333333, fontSize: 15),
  149. ),
  150. Expanded(
  151. child: GestureDetector(
  152. onTap: () async {
  153. _frontCover = await selectImage();
  154. setState(() {});
  155. },
  156. behavior: HitTestBehavior.translucent,
  157. child: Container(
  158. alignment: Alignment.centerRight,
  159. child: _frontCover == null
  160. ? (widget.data == null
  161. ? SvgPicture.asset(
  162. 'images/svg/更换头像.svg',
  163. height: 40,
  164. width: 40,
  165. )
  166. : MyViews().netImg(
  167. imgURL(widget.data.coverPath),
  168. 40,
  169. 40))
  170. : ClipRRect(
  171. child: Image.file(
  172. _frontCover,
  173. height: 40,
  174. width: 40,
  175. ),
  176. borderRadius:
  177. BorderRadius.circular(4),
  178. ),
  179. ),
  180. ),
  181. )
  182. ],
  183. ),
  184. ),
  185. Container(
  186. height: 0.5,
  187. color: MyColors.cE7E7E7,
  188. margin: EdgeInsets.only(left: 16, right: 14),
  189. ),
  190. Padding(
  191. padding: EdgeInsets.only(
  192. left: 16, right: 13, top: 4, bottom: 4),
  193. child: Row(
  194. children: [
  195. Text(
  196. '商品类型',
  197. style: TextStyle(
  198. color: MyColors.c333333, fontSize: 15),
  199. ),
  200. Expanded(
  201. child: Container(
  202. alignment: Alignment.centerRight,
  203. child: DropdownButtonHideUnderline(
  204. child: DropdownButton(
  205. items: sortItems,
  206. onChanged: (value) {
  207. setState(() {
  208. typeValue = value;
  209. });
  210. },
  211. value: typeValue,
  212. ),
  213. ),
  214. ),
  215. )
  216. ],
  217. ),
  218. ),
  219. Visibility(
  220. visible: widget.data == null,
  221. child: Column(
  222. children: [
  223. Container(
  224. height: 0.5,
  225. color: MyColors.cE7E7E7,
  226. margin: EdgeInsets.only(left: 16, right: 14),
  227. ),
  228. Padding(
  229. padding: EdgeInsets.only(
  230. left: 16, right: 13, top: 4, bottom: 4),
  231. child: Row(
  232. children: [
  233. Text(
  234. '所属货帮',
  235. style: TextStyle(
  236. color: MyColors.c333333,
  237. fontSize: 15),
  238. ),
  239. Expanded(
  240. child: Container(
  241. alignment: Alignment.centerRight,
  242. child: DropdownButtonHideUnderline(
  243. child: DropdownButton(
  244. items: sortItems1,
  245. onChanged: (value) {
  246. setState(() {
  247. typeValue1 = value;
  248. });
  249. },
  250. value: typeValue1,
  251. ),
  252. ),
  253. ),
  254. )
  255. ],
  256. ),
  257. ),
  258. ],
  259. ),
  260. ),
  261. Container(
  262. height: 0.5,
  263. color: MyColors.cE7E7E7,
  264. margin: EdgeInsets.only(left: 16, right: 14),
  265. ),
  266. Padding(
  267. padding: EdgeInsets.only(
  268. left: 16, right: 13, top: 15, bottom: 15),
  269. child: Row(
  270. children: [
  271. Text(
  272. '原价',
  273. style: TextStyle(
  274. color: MyColors.c333333, fontSize: 15),
  275. ),
  276. Expanded(
  277. child: Container(
  278. alignment: Alignment.centerRight,
  279. child: TextField(
  280. textAlign: TextAlign.end,
  281. controller: _costOfProduction,
  282. cursorColor: MyColors.cFF4233,
  283. cursorWidth: 1.0,
  284. onTap: () {},
  285. decoration: InputDecoration(
  286. border: InputBorder.none,
  287. disabledBorder: InputBorder.none,
  288. enabledBorder: InputBorder.none,
  289. focusedBorder: InputBorder.none,
  290. isDense: true,
  291. hintText: '请输入',
  292. hintStyle: TextStyle(
  293. color: MyColors.c999999,
  294. fontSize: 14),
  295. contentPadding:
  296. const EdgeInsets.fromLTRB(
  297. 14, 4.5, 8, 4.5),
  298. ),
  299. maxLines: 1,
  300. style: TextStyle(
  301. color: MyColors.c333333,
  302. fontSize: 14,
  303. height: 1.3,
  304. letterSpacing: 0.2),
  305. keyboardType: TextInputType.number,
  306. onChanged: (t) {},
  307. ),
  308. ),
  309. )
  310. ],
  311. ),
  312. ),
  313. Container(
  314. height: 0.5,
  315. color: MyColors.cE7E7E7,
  316. margin: EdgeInsets.only(left: 16, right: 14),
  317. ),
  318. Padding(
  319. padding: EdgeInsets.only(
  320. left: 16, right: 13, top: 15, bottom: 15),
  321. child: Row(
  322. children: [
  323. Text(
  324. '标价',
  325. style: TextStyle(
  326. color: MyColors.c333333, fontSize: 15),
  327. ),
  328. Expanded(
  329. child: Container(
  330. alignment: Alignment.centerRight,
  331. child: TextField(
  332. textAlign: TextAlign.end,
  333. controller: _markedPrice,
  334. cursorColor: MyColors.cFF4233,
  335. cursorWidth: 1.0,
  336. onTap: () {},
  337. decoration: InputDecoration(
  338. border: InputBorder.none,
  339. disabledBorder: InputBorder.none,
  340. enabledBorder: InputBorder.none,
  341. focusedBorder: InputBorder.none,
  342. isDense: true,
  343. hintText: '请输入',
  344. hintStyle: TextStyle(
  345. color: MyColors.c999999,
  346. fontSize: 14),
  347. contentPadding:
  348. const EdgeInsets.fromLTRB(
  349. 14, 4.5, 8, 4.5),
  350. ),
  351. maxLines: 1,
  352. style: TextStyle(
  353. color: MyColors.c333333,
  354. fontSize: 14,
  355. height: 1.3,
  356. letterSpacing: 0.2),
  357. keyboardType: TextInputType.number,
  358. onChanged: (t) {},
  359. ),
  360. ),
  361. )
  362. ],
  363. ),
  364. ),
  365. Container(
  366. height: 0.5,
  367. color: MyColors.cE7E7E7,
  368. margin: EdgeInsets.only(left: 16, right: 14),
  369. ),
  370. Padding(
  371. padding: EdgeInsets.only(
  372. left: 16, right: 13, top: 15, bottom: 15),
  373. child: Row(
  374. children: [
  375. Text(
  376. '数量',
  377. style: TextStyle(
  378. color: MyColors.c333333, fontSize: 15),
  379. ),
  380. Expanded(
  381. child: Container(
  382. alignment: Alignment.centerRight,
  383. child: TextField(
  384. textAlign: TextAlign.end,
  385. controller: _num,
  386. cursorColor: MyColors.cFF4233,
  387. cursorWidth: 1.0,
  388. onTap: () {},
  389. decoration: InputDecoration(
  390. border: InputBorder.none,
  391. disabledBorder: InputBorder.none,
  392. enabledBorder: InputBorder.none,
  393. focusedBorder: InputBorder.none,
  394. isDense: true,
  395. hintText: '请输入',
  396. hintStyle: TextStyle(
  397. color: MyColors.c999999,
  398. fontSize: 14),
  399. contentPadding:
  400. const EdgeInsets.fromLTRB(
  401. 14, 4.5, 8, 4.5),
  402. ),
  403. maxLines: 1,
  404. style: TextStyle(
  405. color: MyColors.c333333,
  406. fontSize: 14,
  407. height: 1.3,
  408. letterSpacing: 0.2),
  409. keyboardType: TextInputType.number,
  410. onChanged: (t) {},
  411. ),
  412. ),
  413. )
  414. ],
  415. ),
  416. ),
  417. Container(
  418. height: 0.5,
  419. color: MyColors.cE7E7E7,
  420. margin: EdgeInsets.only(left: 16, right: 14),
  421. ),
  422. Padding(
  423. padding: EdgeInsets.only(
  424. left: 16, right: 13, top: 15, bottom: 15),
  425. child: Row(
  426. children: [
  427. Text(
  428. '是否上架',
  429. style: TextStyle(
  430. color: MyColors.c333333, fontSize: 15),
  431. ),
  432. Expanded(
  433. child: Container(
  434. alignment: Alignment.centerRight,
  435. child: CupertinoSwitch(
  436. value: onSale,
  437. activeColor: MyColors.cFF4233,
  438. onChanged: (v) {
  439. setState(() {
  440. onSale = v;
  441. });
  442. },
  443. ),
  444. ),
  445. )
  446. ],
  447. ),
  448. ),
  449. ],
  450. ),
  451. margin: EdgeInsets.only(top: 10),
  452. ),
  453. Container(
  454. margin: EdgeInsets.only(top: 10),
  455. color: Colors.white,
  456. child: Column(
  457. children: [
  458. Container(
  459. width: double.infinity,
  460. margin: EdgeInsets.only(left: 16, top: 17),
  461. child: Text(
  462. '商品描述',
  463. style: TextStyle(
  464. color: MyColors.c333333, fontSize: 15),
  465. ),
  466. ),
  467. Container(
  468. margin:
  469. EdgeInsets.only(left: 15, right: 15, top: 17),
  470. constraints: BoxConstraints(minHeight: 86),
  471. decoration: BoxDecoration(
  472. border:
  473. Border.all(color: MyColors.cE7E7E7, width: 1),
  474. borderRadius: BorderRadius.all(
  475. Radius.circular(4),
  476. ),
  477. ),
  478. child: TextField(
  479. controller: _description,
  480. cursorColor: MyColors.cFF4233,
  481. maxLines: null,
  482. cursorWidth: 1.0,
  483. onTap: () {},
  484. decoration: InputDecoration(
  485. border: InputBorder.none,
  486. disabledBorder: InputBorder.none,
  487. enabledBorder: InputBorder.none,
  488. focusedBorder: InputBorder.none,
  489. isDense: true,
  490. hintText: '请输入',
  491. hintStyle: TextStyle(
  492. color: MyColors.c999999, fontSize: 14),
  493. contentPadding:
  494. const EdgeInsets.fromLTRB(14, 4.5, 8, 4.5),
  495. ),
  496. style: TextStyle(
  497. color: MyColors.c333333,
  498. fontSize: 14,
  499. height: 1.3,
  500. letterSpacing: 0.2),
  501. keyboardType: TextInputType.text,
  502. onChanged: (t) {},
  503. ),
  504. ),
  505. Container(
  506. width: double.infinity,
  507. margin: EdgeInsets.only(left: 16, top: 17),
  508. child: Text(
  509. '商品图片',
  510. style: TextStyle(
  511. color: MyColors.c333333, fontSize: 15),
  512. ),
  513. ),
  514. Container(
  515. height: detailsPictures.length > 3 ? 279 : 137,
  516. margin: EdgeInsets.only(bottom: 15, top: 15),
  517. child: GridView.builder(
  518. padding: EdgeInsets.symmetric(horizontal: 15),
  519. gridDelegate:
  520. SliverGridDelegateWithFixedCrossAxisCount(
  521. crossAxisCount: 3,
  522. childAspectRatio: 102 / 137,
  523. mainAxisSpacing: 5,
  524. crossAxisSpacing:
  525. (MediaQuery.of(context).size.width -
  526. 336) /
  527. 2),
  528. itemBuilder: (BuildContext context, int index) {
  529. return GestureDetector(
  530. child: picItem(detailsPictures[index]),
  531. onTap: () {
  532. selectPic(detailsPictures[index]);
  533. },
  534. behavior: HitTestBehavior.translucent,
  535. );
  536. },
  537. itemCount: detailsPictures.length,
  538. physics: NeverScrollableScrollPhysics(),
  539. ),
  540. ),
  541. ],
  542. ),
  543. ),
  544. GestureDetector(
  545. onTap: () {
  546. if (widget.data == null) {
  547. checkData();
  548. } else {
  549. modifyCargo();
  550. }
  551. },
  552. behavior: HitTestBehavior.translucent,
  553. child: Container(
  554. width: double.infinity,
  555. height: 50,
  556. color: Colors.white,
  557. child: Text(
  558. widget.data == null ? '发布' : '保存',
  559. style:
  560. TextStyle(color: MyColors.cFF4233, fontSize: 16),
  561. ),
  562. alignment: Alignment.center,
  563. margin: EdgeInsets.only(top: 10, bottom: 20),
  564. ),
  565. )
  566. ],
  567. ),
  568. ),
  569. )
  570. ],
  571. ),
  572. backgroundColor: MyColors.cF7F7F7,
  573. ),
  574. );
  575. }
  576. queryGang() {
  577. MyDio().query({
  578. "key": "shop_user",
  579. "filters": {
  580. "or": true,
  581. "conditions": [
  582. "role!=$shopUserOwner",
  583. "user_uid==${MyCookie().getUID()}",
  584. "review_state==1"
  585. ],
  586. "filters": [
  587. {
  588. "conditions": [
  589. "role==$shopUserOwner",
  590. "user_uid==${MyCookie().getUID()}"
  591. ]
  592. }
  593. ]
  594. },
  595. "dims": shopUserDims,
  596. "paging": [1, 2000]
  597. }, (response, hasError) {
  598. if (!hasError) {
  599. MyShopBeanEntity entity =
  600. MyShopBeanEntity().fromJson(json.decode(response.data.toString()));
  601. shops = entity.data.data;
  602. shops.forEach((element) {
  603. //自己的公开店铺
  604. if (!element.privateShop &&
  605. element.ownerUid == MyCookie().getUID() &&
  606. element.shopState == shopStateNormal) {
  607. element.canSend = true;
  608. }
  609. //自己在某个允许顾客间交易的私有店铺中
  610. if (element.innerTrade) {
  611. element.canSend = true;
  612. }
  613. });
  614. shops.removeWhere((element) => !element.canSend);
  615. if (shops.length == 0) {
  616. showDialog(
  617. context: context,
  618. builder: (BuildContext context) {
  619. return Center(
  620. child: Container(
  621. decoration: BoxDecoration(
  622. color: Colors.white,
  623. borderRadius: BorderRadius.all(Radius.circular(8))),
  624. child: Column(
  625. children: [
  626. Expanded(
  627. child: Center(
  628. child: Padding(
  629. padding: const EdgeInsets.only(left: 15, right: 15),
  630. child: Text(
  631. '对不起你没有可以发布商品的货帮',
  632. style: TextStyle(
  633. color: MyColors.c333333,
  634. fontSize: 16,
  635. fontWeight: FontWeight.normal,
  636. decoration: TextDecoration.none),
  637. ),
  638. ),
  639. ),
  640. ),
  641. Container(
  642. height: 0.5,
  643. color: MyColors.cE7E7E7,
  644. ),
  645. Row(
  646. children: [
  647. Expanded(
  648. child: GestureDetector(
  649. onTap: () async {
  650. Navigator.pop(context);
  651. Navigator.pop(context);
  652. },
  653. behavior: HitTestBehavior.translucent,
  654. child: Container(
  655. alignment: Alignment.center,
  656. height: 55,
  657. child: Text(
  658. '确定',
  659. style: TextStyle(
  660. color: MyColors.cFF4233,
  661. fontSize: 16,
  662. fontWeight: FontWeight.normal,
  663. decoration: TextDecoration.none),
  664. ),
  665. ),
  666. )),
  667. ],
  668. )
  669. ],
  670. ),
  671. height: 126,
  672. margin: EdgeInsets.only(left: 48, right: 48),
  673. ),
  674. );
  675. },
  676. );
  677. }
  678. if (shops != null) {
  679. for (int i = 0; i < shops.length; i++) {
  680. if (i == 0) {
  681. typeValue1 = shops[i].shopName;
  682. }
  683. sortItems1.add(DropdownMenuItem(
  684. child: Text(
  685. '${shops[i].shopName}',
  686. style: TextStyle(color: MyColors.c666666, fontSize: 14),
  687. ),
  688. value: '${shops[i].shopName}',
  689. ));
  690. }
  691. }
  692. setState(() {});
  693. }
  694. }, (error) {});
  695. }
  696. void checkData() {
  697. if (shops.isEmpty) {
  698. showToast('货帮获取失败,请稍后再试');
  699. return;
  700. }
  701. if (_title.text.isEmpty) {
  702. showToast('请填写商品名称');
  703. return;
  704. }
  705. if (_frontCover == null) {
  706. showToast('请选择商品封面');
  707. return;
  708. }
  709. bool hasP = false;
  710. detailsPictures.forEach((element) {
  711. if (element != null) {
  712. hasP = true;
  713. }
  714. });
  715. if (!hasP) {
  716. showToast('请设置商品详情图片');
  717. return;
  718. }
  719. if (_costOfProduction.text.isEmpty) {
  720. showToast('请填写原价');
  721. return;
  722. }
  723. if (_markedPrice.text.isEmpty) {
  724. showToast('请填写标价');
  725. return;
  726. }
  727. if (_num.text.isEmpty) {
  728. showToast('请填写数量');
  729. return;
  730. }
  731. uploadCover();
  732. }
  733. void productsOnTheShelves() {
  734. if (widget.data != null) {
  735. MyDio().update({
  736. 'key': 'commodity',
  737. 'values': {
  738. 'id': widget.data.id,
  739. 'category': typeValue,
  740. 'title': _title.text.toString(),
  741. 'cover_path': coverPath,
  742. 'description': _description.text.toString(),
  743. 'original_price': double.parse(_costOfProduction.text.toString()),
  744. 'price': double.parse(_markedPrice.text.toString()),
  745. 'on_sale': onSale,
  746. 'stock': int.parse(_num.text.toString()),
  747. 'pictures': picturesPath,
  748. }
  749. }, (response, hasError) {
  750. if (!hasError) {
  751. showToast('保存成功');
  752. Navigator.pop(context);
  753. EventBus().emit('uploadGoods');
  754. }
  755. }, (error) {});
  756. } else {
  757. int sIndex =
  758. shops.indexWhere((element) => element.shopName == typeValue1);
  759. MyDio().save({
  760. 'key': 'commodity',
  761. 'object': {
  762. 'category': typeValue,
  763. 'shop_uid': shops[sIndex].shopUid,
  764. 'shop_name': shops[sIndex].shopName,
  765. 'shop_pic': shops[sIndex].shopPic,
  766. 'user_uid': MyCookie().getUID(),
  767. 'user_name': MyCookie().loginInformation.data.extra.name,
  768. 'user_pic': MyCookie().loginInformation.data.extra.picture,
  769. 'title': _title.text.toString(),
  770. 'cover_path': coverPath,
  771. 'description': _description.text.toString(),
  772. 'original_price': double.parse(_costOfProduction.text.toString()),
  773. 'price': double.parse(_markedPrice.text.toString()),
  774. 'on_sale': onSale,
  775. 'stock': int.parse(_num.text.toString()),
  776. 'pictures': picturesPath,
  777. }
  778. }, (response, hasError) {
  779. if (!hasError) {
  780. showToast('上传成功');
  781. Navigator.pop(context);
  782. EventBus().emit('uploadGoods');
  783. }
  784. }, (error) {});
  785. }
  786. }
  787. String coverPath = '';
  788. String picturesPath = '';
  789. void uploadCover() {
  790. if (_frontCover == null) {
  791. if (widget.data != null) {
  792. coverPath = widget.data.coverPath;
  793. upPic();
  794. } else {
  795. showToast('请选择商品封面');
  796. return;
  797. }
  798. } else {
  799. EasyLoading.instance
  800. ..contentPadding = EdgeInsets.symmetric(horizontal: 20, vertical: 12)
  801. ..alignment = Alignment.center
  802. ..loadingStyle = EasyLoadingStyle.light
  803. ..contentMargin = EdgeInsets.all(20);
  804. EasyLoading.show();
  805. print('uploadCover');
  806. upload(_frontCover, (r, hE) {
  807. if (!hE) {
  808. UploadImageBeanEntity data =
  809. UploadImageBeanEntity().fromJson(json.decode(r.data.toString()));
  810. coverPath = data.data[0].path;
  811. upPic();
  812. } else {
  813. EasyLoading.dismiss();
  814. }
  815. }, (e) {
  816. EasyLoading.dismiss();
  817. });
  818. }
  819. }
  820. Future<void> initCC() async {
  821. commodityCategoryBeanData =
  822. await MyData().getCommodityCategoryBeanData(context);
  823. if (commodityCategoryBeanData.length > 0) {
  824. typeValue = commodityCategoryBeanData[0].name;
  825. }
  826. commodityCategoryBeanData.forEach((element) {
  827. sortItems.add(DropdownMenuItem(
  828. child: Text(
  829. '${element.name}',
  830. style: TextStyle(color: MyColors.c666666, fontSize: 14),
  831. ),
  832. value: '${element.name}',
  833. ));
  834. });
  835. setState(() {});
  836. }
  837. void modifyCargo() {
  838. bool havePic = false;
  839. detailsPictures.forEach((element) {
  840. if (element != null) {
  841. havePic = true;
  842. }
  843. });
  844. if (!havePic) {
  845. showToast('请设置商品详情图片');
  846. return;
  847. }
  848. uploadCover();
  849. }
  850. upPic() {
  851. List<File> fs = [];
  852. List<int> oP = [];
  853. for (int i = 0; i < detailsPictures.length; i++) {
  854. if (detailsPictures[i] is File) {
  855. fs.add(detailsPictures[i]);
  856. }
  857. if (detailsPictures[i] is String) {
  858. oP.add(i);
  859. }
  860. }
  861. if (fs.length == 0) {
  862. detailsPictures.forEach((element) {
  863. if (element is String) {
  864. if (picturesPath == '') {
  865. picturesPath = element;
  866. } else {
  867. picturesPath = picturesPath + ',' + element;
  868. }
  869. }
  870. });
  871. productsOnTheShelves();
  872. } else {
  873. uploadFiles(fs, (re, hE) {
  874. if (!hE) {
  875. UploadImageBeanEntity data =
  876. UploadImageBeanEntity().fromJson(json.decode(re.data.toString()));
  877. for (int i = 0; i < data.data.length; i++) {
  878. oP.forEach((element) {
  879. if (i == element) {
  880. if (picturesPath == '') {
  881. picturesPath = detailsPictures[i];
  882. } else {
  883. picturesPath = picturesPath + ',' + detailsPictures[i];
  884. }
  885. }
  886. });
  887. if (picturesPath == '') {
  888. picturesPath = data.data[i].path;
  889. } else {
  890. picturesPath = picturesPath + ',' + data.data[i].path;
  891. }
  892. }
  893. oP.forEach((element) {
  894. if (element >= picturesPath.split(',').length) {
  895. if (picturesPath == '') {
  896. picturesPath = detailsPictures[element];
  897. } else {
  898. picturesPath = picturesPath + ',' + detailsPictures[element];
  899. }
  900. }
  901. });
  902. productsOnTheShelves();
  903. } else {
  904. EasyLoading.dismiss();
  905. }
  906. }, (e) {
  907. EasyLoading.dismiss();
  908. });
  909. }
  910. }
  911. Widget picItem(data) {
  912. if (data is String) {
  913. return Container(
  914. width: 102,
  915. child: Column(
  916. children: [
  917. ClipRRect(
  918. child: MyViews().netImg(imgURL(data), 102, 102),
  919. borderRadius: BorderRadius.circular(4),
  920. ),
  921. GestureDetector(
  922. child: Container(
  923. height: 30,
  924. width: 30,
  925. margin: EdgeInsets.only(top: 5),
  926. child: Icon(
  927. Icons.cancel,
  928. color: MyColors.cFF4233,
  929. ),
  930. ),
  931. onTap: () {
  932. detailsPictures.remove(data);
  933. if (detailsPictures.length == 5 &&
  934. !detailsPictures.contains(null)) {
  935. detailsPictures.add(null);
  936. }
  937. setState(() {});
  938. })
  939. ],
  940. ),
  941. );
  942. } else if (data is File) {
  943. return Container(
  944. width: 102,
  945. child: Column(
  946. children: [
  947. ClipRRect(
  948. child: Image.file(
  949. data,
  950. height: 102,
  951. width: 102,
  952. fit: BoxFit.cover,
  953. ),
  954. borderRadius: BorderRadius.circular(4),
  955. ),
  956. GestureDetector(
  957. child: Container(
  958. height: 30,
  959. width: 30,
  960. margin: EdgeInsets.only(top: 5),
  961. child: Icon(
  962. Icons.cancel,
  963. color: MyColors.cFF4233,
  964. ),
  965. ),
  966. onTap: () {
  967. detailsPictures.remove(data);
  968. if (detailsPictures.length == 5 &&
  969. !detailsPictures.contains(null)) {
  970. detailsPictures.add(null);
  971. }
  972. setState(() {});
  973. })
  974. ],
  975. ),
  976. );
  977. } else {
  978. return Column(
  979. children: [
  980. Container(
  981. height: 102,
  982. width: 102,
  983. decoration: BoxDecoration(
  984. color: MyColors.cF0F0F0,
  985. borderRadius: BorderRadius.all(Radius.circular(4))),
  986. child: Icon(
  987. Icons.add,
  988. color: MyColors.cB6B6B6,
  989. size: 44,
  990. ),
  991. ),
  992. Container(
  993. height: 35,
  994. )
  995. ],
  996. );
  997. }
  998. }
  999. Future<void> selectPic(data) async {
  1000. File f = await selectImage(isCover: false);
  1001. if (f == null) {
  1002. return;
  1003. }
  1004. if (data == null) {
  1005. if (detailsPictures.length == 6) {
  1006. detailsPictures[
  1007. detailsPictures.indexWhere((element) => element == data)] = f;
  1008. } else {
  1009. detailsPictures.insert(detailsPictures.length - 1, f);
  1010. }
  1011. } else {
  1012. detailsPictures[
  1013. detailsPictures.indexWhere((element) => element == data)] = f;
  1014. }
  1015. setState(() {});
  1016. }
  1017. }