Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.73 KB | None | 0 0
  1. import 'package:carousel_pro/carousel_pro.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:intl/intl.dart';
  4. import 'package:senyum_media/api/url.dart';
  5. import 'package:senyum_media/detailitemcategori.dart';
  6. import 'package:senyum_media/model/dataProduk.dart';
  7. import 'package:flutter_html_view/flutter_html_view.dart';
  8. import 'package:http/http.dart' as http;
  9. import 'dart:convert';
  10. import 'package:shared_preferences/shared_preferences.dart';
  11. import 'package:zoomable_image/zoomable_image.dart';
  12.  
  13. class SecondDetailItem extends StatefulWidget {
  14. final DataProduk _dataProduk;
  15. final String id;
  16. SecondDetailItem(this._dataProduk, {this.id});
  17. @override
  18. _DetailProductDiscountState createState() => _DetailProductDiscountState();
  19. }
  20.  
  21. class _DetailProductDiscountState extends State<SecondDetailItem> {
  22. var f = NumberFormat("#,##0", "en_US");
  23. @override
  24. final _list = List<DataProduk>();
  25. final _produkkategori = List<ProdukCategori>();
  26.  
  27. String idUser = "";
  28. getPref() async {
  29. SharedPreferences preferences = await SharedPreferences.getInstance();
  30. setState(() {
  31. idUser = preferences.getString("idUsers");
  32. });
  33. }
  34.  
  35. var loading = false;
  36. productSejenis() async {
  37. _produkkategori.clear();
  38. setState(() {
  39. loading = true;
  40. });
  41. final response =
  42. await http.get(BaseUrl.otherProduct + widget._dataProduk.categoryId);
  43. if (response.statusCode == 200) {
  44. final data = jsonDecode(response.body);
  45. data.forEach((api) {
  46. final res = ProdukCategori(
  47. api['id'],
  48. api['code'],
  49. api['name'],
  50. api['description'],
  51. api['discount'],
  52. api['image_1'],
  53. api['image_2'],
  54. api['image_3'],
  55. api['price'],
  56. api['stock'],
  57. api['weight'],
  58. api['height'],
  59. api['width'],
  60. api['length'],
  61. api['category_id'],
  62. api['producer_id'],
  63. api['deleted_by']);
  64. setState(() {
  65. _produkkategori.add(res);
  66. loading = false;
  67. });
  68. });
  69. }
  70. }
  71.  
  72. produkLainnya() async {
  73. _list.clear();
  74. setState(() {
  75. loading = true;
  76. });
  77. final response = await http.get(BaseUrl.showData);
  78. if (response.statusCode == 200) {
  79. final data = jsonDecode(response.body);
  80. data.forEach((api) {
  81. final res = DataProduk(
  82. api['id'],
  83. api['code'],
  84. api['name'],
  85. api['description'],
  86. api['discount'],
  87. api['image_1'],
  88. api['image_2'],
  89. api['image_3'],
  90. api['price'],
  91. api['stock'],
  92. api['weight'],
  93. api['height'],
  94. api['width'],
  95. api['length'],
  96. api['category_id'],
  97. api['producer_id'],
  98. api['deleted_by']);
  99. setState(() {
  100. _list.add(res);
  101. loading = false;
  102. });
  103. });
  104. }
  105. }
  106.  
  107. addtoCart() async {
  108. final response = await http.post(BaseUrl.getCartId + idUser, body: {
  109. "product_id":
  110. widget._dataProduk.id == "" ? widget.id : widget._dataProduk.id,
  111. "qty": "1",
  112. "price": widget._dataProduk.price,
  113. "discount": widget._dataProduk.discount.toString()
  114. });
  115. final data = jsonDecode(response.body);
  116. int value = data['value'];
  117. String msg = data['message'];
  118. if (response.statusCode == 200) {
  119. print(msg);
  120. } else {
  121. print(msg);
  122. }
  123. }
  124.  
  125. @override
  126. void initState() {
  127. // TODO: implement initState
  128. super.initState();
  129. produkLainnya();
  130. getPref();
  131. productSejenis();
  132. }
  133.  
  134. @override
  135. Widget build(BuildContext context) {
  136. double a = double.parse(widget._dataProduk.price);
  137.  
  138. double b = widget._dataProduk.discount != null
  139. ? double.parse(widget._dataProduk.discount)
  140. : 0.0;
  141. double c = widget._dataProduk.discount == null ? (a * 0) : (a * b);
  142. double d = (a - c);
  143. int price = d.toInt();
  144. return Scaffold(
  145. body: NestedScrollView(
  146. headerSliverBuilder: (BuildContext context, bool inner) {
  147. return <Widget>[
  148. SliverAppBar(
  149. leading: IconButton(
  150. icon: Icon(
  151. Icons.arrow_back,
  152. color: Colors.black38,
  153. ),
  154. onPressed: () => Navigator.pop(context),
  155. ),
  156. backgroundColor: Colors.white,
  157. expandedHeight: 200,
  158. pinned: true,
  159. flexibleSpace: FlexibleSpaceBar(
  160. background: Container(
  161. width: double.infinity,
  162. color: Colors.white,
  163. child: Carousel(
  164. autoplay: false,
  165. boxFit: BoxFit.scaleDown,
  166. dotSize: 4,
  167. indicatorBgPadding: 4,
  168. images: [
  169. new NetworkImage(
  170. BaseUrl.urlImgProduk + widget._dataProduk.image_1),
  171. NetworkImage(
  172. BaseUrl.urlImgProduk + widget._dataProduk.image_2),
  173. NetworkImage(
  174. BaseUrl.urlImgProduk + widget._dataProduk.image_0),
  175. ],
  176. ),
  177. ),
  178. ),
  179. )
  180. ];
  181. },
  182. body: ListView(
  183. padding: const EdgeInsets.all(0),
  184. children: <Widget>[
  185. Container(
  186. padding: const EdgeInsets.all(4.0),
  187. child: Column(
  188. mainAxisSize: MainAxisSize.min,
  189. crossAxisAlignment: CrossAxisAlignment.start,
  190. children: <Widget>[
  191. Card(
  192. child: Container(
  193. padding: const EdgeInsets.all(8.0),
  194. width: double.infinity,
  195. child: Column(
  196. crossAxisAlignment: CrossAxisAlignment.start,
  197. children: <Widget>[
  198. Text(
  199. widget._dataProduk.name,
  200. style: TextStyle(
  201. fontSize: 20, fontWeight: FontWeight.w600),
  202. maxLines: 2,
  203. overflow: TextOverflow.ellipsis,
  204. ),
  205. Padding(padding: const EdgeInsets.all(4.0)),
  206. Text("KODE PRODUK : " +
  207. widget._dataProduk.code.toString()),
  208. Padding(padding: const EdgeInsets.all(4.0)),
  209. Text("Rp " + f.format(price)),
  210. Padding(padding: const EdgeInsets.all(4.0)),
  211. widget._dataProduk.stock != "0"
  212. ? Row(
  213. children: <Widget>[
  214. Icon(
  215. Icons.check_circle,
  216. color: Colors.green,
  217. size: 15,
  218. ),
  219. Padding(padding: const EdgeInsets.all(4.0)),
  220. Text("Stock Tersedia")
  221. ],
  222. )
  223. : Row(
  224. children: <Widget>[
  225. Icon(
  226. Icons.cancel,
  227. color: Colors.redAccent,
  228. size: 15,
  229. ),
  230. Padding(padding: const EdgeInsets.all(4.0)),
  231. Text("Stock Tidak Tersedia")
  232. ],
  233. )
  234. ],
  235. ),
  236. ),
  237. ),
  238. Card(
  239. child: Container(
  240. child: Column(
  241. children: <Widget>[
  242. DefaultTabController(
  243. length: 2,
  244. child: Column(
  245. children: <Widget>[
  246. TabBar(
  247. labelColor: Colors.black,
  248. tabs: <Widget>[
  249. Tab(
  250. text: "Deskripsi",
  251. ),
  252. Tab(
  253. text: "Spesifikasi",
  254. )
  255. ],
  256. ),
  257. Container(
  258. height: 200,
  259. child: TabBarView(
  260. physics: NeverScrollableScrollPhysics(),
  261. children: <Widget>[
  262. Container(
  263. padding: const EdgeInsets.all(8.0),
  264. child: HtmlView(
  265. data: widget._dataProduk.description,
  266. ),
  267. ),
  268. Container(
  269. padding: const EdgeInsets.all(8.0),
  270. child: Row(
  271. children: <Widget>[
  272. Column(
  273. crossAxisAlignment:
  274. CrossAxisAlignment.start,
  275. children: <Widget>[
  276. Text("panjang "),
  277. Text("lebar "),
  278. Text("tinggi "),
  279. Text("berat "),
  280. ],
  281. ),
  282. Column(
  283. children: <Widget>[
  284. Text(": "),
  285. Text(": "),
  286. Text(": "),
  287. Text(": ")
  288. ],
  289. ),
  290. Column(
  291. crossAxisAlignment:
  292. CrossAxisAlignment.start,
  293. children: <Widget>[
  294. Text(widget._dataProduk.length
  295. .toString()),
  296. Text(widget._dataProduk.width
  297. .toString()),
  298. Text(widget._dataProduk.height
  299. .toString()),
  300. Text(widget._dataProduk.weight
  301. .toString() +
  302. " kg"),
  303. ],
  304. ),
  305. ],
  306. ))
  307. ],
  308. ),
  309. ),
  310. ],
  311. ),
  312. ),
  313. ],
  314. ),
  315. ),
  316. ),
  317. Card(
  318. child: Column(
  319. children: <Widget>[
  320. Container(
  321. padding: const EdgeInsets.all(8.0),
  322. child: Text("PRODUK SEJENIS"),
  323. ),
  324. Container(
  325. height: 200,
  326. child: loading != true
  327. ? ListView.builder(
  328. scrollDirection: Axis.horizontal,
  329. itemCount: _produkkategori.length <= 6
  330. ? _produkkategori.length
  331. : _produkkategori.length = 6,
  332. itemBuilder: (context, i) {
  333. final x = _produkkategori[i];
  334. return GestureDetector(
  335. onTap: () => Navigator.of(context)
  336. .pushReplacement(MaterialPageRoute(
  337. builder:
  338. (BuildContext context) =>
  339. DetailItemCategory(x))),
  340. child: widget._dataProduk.id == x.id
  341. ? Container(
  342. height: 0,
  343. width: 0,
  344. )
  345. : Card(
  346. child: Container(
  347. height: 150,
  348. width: 130,
  349. child: Column(
  350. children: <Widget>[
  351. Stack(
  352. children: <Widget>[
  353. Container(
  354. height: 100,
  355. width: 100,
  356. decoration: BoxDecoration(
  357. borderRadius:
  358. BorderRadius
  359. .circular(
  360. 10),
  361. image: DecorationImage(
  362. image: NetworkImage(
  363. BaseUrl.urlImgProduk +
  364. x.image_1))),
  365. ),
  366. x.stock == "0" ?Opacity(
  367. opacity: 0.35,
  368. child: Container(
  369. height: 100,
  370. width: 100,
  371. decoration: BoxDecoration(
  372. borderRadius:
  373. BorderRadius
  374. .circular(
  375. 10),
  376. color: Colors
  377. .red[100],),
  378. child: Center(
  379. child: Text(
  380. "Stock Tidak Tersedia", textAlign: TextAlign.center,)
  381. ),
  382. ),
  383. ) : Container()
  384. ],
  385. ),
  386. Container(
  387. child: Column(
  388. children: <Widget>[
  389. Text(
  390. x.name,
  391. style:
  392. TextStyle(
  393. fontSize: 16,
  394. ),
  395. textAlign:
  396. TextAlign
  397. .center,
  398. maxLines: 2,
  399. overflow:
  400. TextOverflow
  401. .ellipsis,
  402. ),
  403. Text(
  404. "\nRp " +
  405. f.format(int
  406. .parse(x
  407. .price)),
  408. style:
  409. TextStyle(
  410. fontSize: 14,
  411. ),
  412. maxLines: 2,
  413. overflow:
  414. TextOverflow
  415. .ellipsis,
  416. )
  417. ],
  418. ),
  419. )
  420. ],
  421. )),
  422. ),
  423. );
  424. },
  425. )
  426. : Center(child: CircularProgressIndicator()))
  427. ],
  428. ),
  429. ),
  430. Card(
  431. child: Column(
  432. children: <Widget>[
  433. Container(
  434. padding: const EdgeInsets.all(8.0),
  435. child: Text("PRODUK LAINNYA"),
  436. ),
  437. Container(
  438. height: 200,
  439. child: loading != true
  440. ? ListView.builder(
  441. scrollDirection: Axis.horizontal,
  442. itemCount: _list.length <= 6
  443. ? _list.length
  444. : _list.length = 6,
  445. itemBuilder: (context, i) {
  446. final x = _list[i];
  447. return GestureDetector(
  448. onTap: () => Navigator.of(context)
  449. .pushReplacement(MaterialPageRoute(
  450. builder:
  451. (BuildContext context) =>
  452. SecondDetailItem(x))),
  453. child: widget._dataProduk.id == x.id
  454. ? Container(
  455. height: 0,
  456. width: 0,
  457. )
  458. : Card(
  459. child: Container(
  460. height: 150,
  461. width: 130,
  462. child: Column(
  463. children: <Widget>[
  464. Container(
  465. height: 100,
  466. width: 100,
  467. decoration: BoxDecoration(
  468. borderRadius:
  469. BorderRadius
  470. .circular(
  471. 10),
  472. image: DecorationImage(
  473. image: NetworkImage(
  474. BaseUrl.urlImgProduk +
  475. x.image_1))),
  476. ),
  477. Container(
  478. child: Column(
  479. children: <Widget>[
  480. Text(
  481. x.name,
  482. style:
  483. TextStyle(
  484. fontSize: 16,
  485. ),
  486. textAlign:
  487. TextAlign
  488. .center,
  489. maxLines: 2,
  490. overflow:
  491. TextOverflow
  492. .ellipsis,
  493. ),
  494. Text(
  495. "\nRp " +
  496. f.format(int
  497. .parse(x
  498. .price)),
  499. style:
  500. TextStyle(
  501. fontSize: 14,
  502. ),
  503. maxLines: 2,
  504. overflow:
  505. TextOverflow
  506. .ellipsis,
  507. )
  508. ],
  509. ),
  510. )
  511. ],
  512. )),
  513. ),
  514. );
  515. },
  516. )
  517. : Center(child: CircularProgressIndicator()))
  518. ],
  519. ),
  520. )
  521. ],
  522. ),
  523. ),
  524. ],
  525. ),
  526. ),
  527. bottomNavigationBar: Container(
  528. height: 50,
  529. child: Row(
  530. mainAxisAlignment: MainAxisAlignment.spaceAround,
  531. children: <Widget>[
  532. Container(
  533. child: Text(
  534. "Rp " + f.format(price),
  535. style: TextStyle(color: Colors.blue, fontSize: 16),
  536. ),
  537. ),
  538. MaterialButton(
  539. color: Colors.blue,
  540. onPressed: () {
  541. addtoCart();
  542. },
  543. child: Text(
  544. "BELI",
  545. style: TextStyle(color: Colors.white),
  546. ),
  547. )
  548. ],
  549. ),
  550. decoration: BoxDecoration(color: Colors.white, boxShadow: <BoxShadow>[
  551. BoxShadow(
  552. color: Colors.black26,
  553. )
  554. ]),
  555. ),
  556. );
  557. }
  558. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement