Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.89 KB | None | 0 0
  1. import 'dart:async';
  2.  
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_bloc/flutter_bloc.dart';
  5. import 'package:flutter_svg/flutter_svg.dart';
  6.  
  7. import '../../bloc/home/newarrival_product/bloc.dart';
  8. import '../../bloc/home/sale_product/bloc.dart';
  9. import '../../bloc/product/product_list/bloc.dart';
  10. import '../../config/theme.dart';
  11. import '../../custom_widget/item_loading.dart';
  12. import '../../custom_widget/item_product.dart';
  13. import '../../service/api_service.dart';
  14. import '../list_product/list_product.dart';
  15.  
  16. class ProductItem extends StatelessWidget {
  17. final String text;
  18. final String showProduct;
  19. final ProductListEvent event;
  20. final NewArrivalBloc newArrivalBloc;
  21. final SaleProductBloc saleProductBloc;
  22. final Completer<void> refreshCompleter;
  23.  
  24. const ProductItem({
  25. Key key,
  26. this.text = '',
  27. this.showProduct = '',
  28. @required this.event,
  29. this.newArrivalBloc,
  30. this.refreshCompleter,
  31. this.saleProductBloc,
  32. }) : super(key: key);
  33.  
  34. @override
  35. Widget build(BuildContext context) {
  36. double width = MediaQuery.of(context).size.width;
  37. return Container(
  38. margin: EdgeInsets.symmetric(
  39. vertical: 8.0,
  40. ),
  41. color: Colors.white,
  42. child: Column(
  43. children: <Widget>[
  44. Row(
  45. children: <Widget>[
  46. Expanded(
  47. child: Row(
  48. children: <Widget>[
  49. Align(
  50. alignment: Alignment.topLeft,
  51. child: Padding(
  52. padding: const EdgeInsets.all(16.0),
  53. child: Text(
  54. text,
  55. textAlign: TextAlign.left,
  56. style: TextStyle(
  57. fontSize: 18.0,
  58. fontFamily: 'Avenir Black',
  59. color: ThemeApp.categoryTextColor,
  60. ),
  61. ),
  62. ),
  63. ),
  64. // TODO - issue can't load SVG
  65. SvgPicture.asset('assets/icons/icon_new_arrival.svg'),
  66. ],
  67. ),
  68. ),
  69. InkWell(
  70. child: Padding(
  71. padding: const EdgeInsets.symmetric(
  72. horizontal: 32.0,
  73. ),
  74. child: Text(
  75. 'View All',
  76. textAlign: TextAlign.end,
  77. style: ThemeApp.viewAllTextStyle(),
  78. ),
  79. ),
  80. onTap: () {
  81. Navigator.push(
  82. context,
  83. MaterialPageRoute(
  84. builder: (BuildContext context) {
  85. return BlocProvider<ProductListBloc>(
  86. create: (BuildContext context) {
  87. return ProductListBloc();
  88. },
  89. child: ListProductPage(
  90. event: event,
  91. ),
  92. );
  93. },
  94. ),
  95. );
  96. },
  97. ),
  98. ],
  99. ),
  100. SizedBox(
  101. width: width,
  102. child: NotificationListener<OverscrollIndicatorNotification>(
  103. onNotification: (OverscrollIndicatorNotification overScroll) {
  104. overScroll.disallowGlow();
  105. return false;
  106. },
  107. child: buildBlocBuilder(
  108. showProduct,
  109. newArrivalBloc,
  110. saleProductBloc,
  111. refreshCompleter,
  112. width,
  113. context,
  114. ),
  115. ),
  116. ),
  117. ],
  118. ),
  119. );
  120. }
  121.  
  122. Widget buildLoadingItem(double width) {
  123. double height = 240.0;
  124. return Container(
  125. height: height,
  126. margin: EdgeInsets.only(
  127. bottom: 26.0,
  128. ),
  129. child: ListView.builder(
  130. shrinkWrap: true,
  131. scrollDirection: Axis.horizontal,
  132. physics: ScrollPhysics(),
  133. itemCount: 4,
  134. itemBuilder: (BuildContext context, int index) {
  135. return ItemProductsLoading(
  136. margin: index == 0
  137. ? EdgeInsets.only(
  138. left: 16.0,
  139. right: 16.0,
  140. top: 6.0,
  141. bottom: 6.0,
  142. )
  143. : EdgeInsets.only(
  144. right: 16.0,
  145. top: 6.0,
  146. bottom: 6.0,
  147. ),
  148. width: width,
  149. );
  150. },
  151. ),
  152. );
  153. }
  154.  
  155. Widget buildLoadedItem(state, double width) {
  156. return Container(
  157. height: 240.0,
  158. margin: EdgeInsets.only(
  159. bottom: 26.0,
  160. ),
  161. child: ListView.builder(
  162. shrinkWrap: true,
  163. physics: ScrollPhysics(),
  164. scrollDirection: Axis.horizontal,
  165. itemCount: state.product.length,
  166. itemBuilder: (BuildContext context, int index) {
  167. return ItemProducts(
  168. index: index,
  169. products: state.product[index],
  170. margin: index == 0
  171. ? EdgeInsets.only(
  172. left: 16.0,
  173. right: 16.0,
  174. top: 6.0,
  175. bottom: 6.0,
  176. )
  177. : EdgeInsets.only(
  178. right: 16.0,
  179. top: 6.0,
  180. bottom: 6.0,
  181. ),
  182. width: width,
  183. );
  184. },
  185. ),
  186. );
  187. }
  188.  
  189. Widget buildErrorItem() {
  190. return Text('Error');
  191. }
  192.  
  193. BlocBuilder buildBlocBuilder(
  194. String showProduct,
  195. NewArrivalBloc newArrivalBloc,
  196. SaleProductBloc saleProductBloc,
  197. Completer<void> refreshCompleter,
  198. double width,
  199. BuildContext context,
  200. ) {
  201. switch (showProduct) {
  202. case ShowProduct.sale:
  203. BlocListener<SaleProductBloc, SaleProductState>(
  204. listener: (BuildContext context, SaleProductState state) {
  205. if (state is ProductLoaded) {
  206. saleProductBloc.add(GetSaleProductFromAPI());
  207. }
  208. refreshCompleter?.complete();
  209. refreshCompleter = Completer();
  210. },
  211. child: BlocBuilder<SaleProductBloc, SaleProductState>(
  212. builder: (BuildContext context, SaleProductState state) {
  213. if (state is ProductLoading) {
  214. return buildLoadingItem(width);
  215. }
  216. if (state is ProductLoaded) {
  217. return buildLoadedItem(state, width);
  218. }
  219. if (state is ProductError) {
  220. return Text("eroor ${state.message}");
  221. }
  222. return Container();
  223. },
  224. ),
  225. );
  226. break;
  227. case ShowProduct.newArrival:
  228. BlocListener<NewArrivalBloc, NewArrivalState>(
  229. listener: (BuildContext context, NewArrivalState state) {
  230. if (state is NewArrivalLoaded) {
  231. newArrivalBloc.add(GetNewArrivalFromAPI());
  232. }
  233. refreshCompleter?.complete();
  234. refreshCompleter = Completer();
  235. },
  236. child: BlocBuilder<NewArrivalBloc, NewArrivalState>(
  237. builder: (BuildContext context, NewArrivalState state) {
  238. if (state is NewArrivalLoading) {
  239. return buildLoadingItem(width);
  240. }
  241. if (state is NewArrivalLoaded) {
  242. return buildLoadedItem(state, width);
  243. }
  244. if (state is NewArrivalError) {
  245. return Text("INI JUGA ${state.message}");
  246. }
  247. return Container();
  248. },
  249. ),
  250. );
  251.  
  252. break;
  253. default:
  254. return null;
  255. }
  256. }
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement