release_goods_page.dart 41 KB

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