|
| 1 | +import 'dart:convert'; |
| 2 | + |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:flutter_ecommerce_app/common_widget/AppBarWidget.dart'; |
| 5 | +import 'package:flutter_ecommerce_app/common_widget/CircularProgress.dart'; |
| 6 | +import 'package:flutter_ecommerce_app/models/ProductDetails.dart'; |
| 7 | +import 'package:flutter_ecommerce_app/utils/Urls.dart'; |
| 8 | +import 'package:http/http.dart'; |
| 9 | + |
| 10 | +ProductDetails productDetails; |
| 11 | + |
| 12 | +class ProductDetailScreen extends StatefulWidget { |
| 13 | + String slug; |
| 14 | + |
| 15 | + ProductDetailScreen({Key key, @required this.slug}) : super(key: key); |
| 16 | + |
| 17 | + @override |
| 18 | + _ProductDetailScreenState createState() => _ProductDetailScreenState(); |
| 19 | +} |
| 20 | + |
| 21 | +//https://api.evaly.com.bd/core/public/products/leather-backpack-for-women-6dba2a50d/ |
| 22 | +//https://api.evaly.com.bd/core/public/products/special-women-kids-cotton-panjabi-kameez-by-swapons-world-combo-pack-c8648f9f9/ |
| 23 | + |
| 24 | +class _ProductDetailScreenState extends State<ProductDetailScreen> { |
| 25 | + @override |
| 26 | + Widget build(BuildContext context) { |
| 27 | + return Scaffold( |
| 28 | + backgroundColor: Color(0xFFfafafa), |
| 29 | + appBar: appBarWidget(context), |
| 30 | + body: FutureBuilder( |
| 31 | + future: getDetailData(widget.slug), |
| 32 | + builder: (context, AsyncSnapshot snapshot) { |
| 33 | + switch (snapshot.connectionState) { |
| 34 | + case ConnectionState.none: |
| 35 | + case ConnectionState.waiting: |
| 36 | + return CircularProgress(); |
| 37 | + default: |
| 38 | + if (snapshot.hasError) |
| 39 | + return Text('Error: ${snapshot.error}'); |
| 40 | + else |
| 41 | + return createDetailView(context, snapshot); |
| 42 | + } |
| 43 | + }, |
| 44 | + ), |
| 45 | + bottomNavigationBar: BottomNavBar(), |
| 46 | + ); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +class BottomNavBar extends StatelessWidget { |
| 51 | + @override |
| 52 | + Widget build(BuildContext context) { |
| 53 | + return Container( |
| 54 | + padding: EdgeInsets.only(left: 20, right: 10), |
| 55 | + child: Row( |
| 56 | + children: <Widget>[ |
| 57 | + Icon( |
| 58 | + Icons.favorite_border, |
| 59 | + color: Color(0xFF5e5e5e), |
| 60 | + ), |
| 61 | + Spacer(), |
| 62 | + RaisedButton( |
| 63 | + elevation: 0, |
| 64 | + shape: new RoundedRectangleBorder( |
| 65 | + borderRadius: new BorderRadius.only( |
| 66 | + topLeft: Radius.circular(10), |
| 67 | + bottomLeft: Radius.circular(10)), |
| 68 | + side: BorderSide(color: Color(0xFFfef2f2))), |
| 69 | + onPressed: () {}, |
| 70 | + color: Color(0xFFfef2f2), |
| 71 | + textColor: Colors.white, |
| 72 | + child: Container( |
| 73 | + padding: EdgeInsets.only(left: 5, right: 5, top: 15, bottom: 15), |
| 74 | + child: Text("Add to cart".toUpperCase(), |
| 75 | + style: TextStyle( |
| 76 | + fontSize: 14, |
| 77 | + fontWeight: FontWeight.w400, |
| 78 | + color: Color(0xFFff665e))), |
| 79 | + ), |
| 80 | + ), |
| 81 | + RaisedButton( |
| 82 | + elevation: 0, |
| 83 | + shape: new RoundedRectangleBorder( |
| 84 | + borderRadius: new BorderRadius.only( |
| 85 | + topRight: Radius.circular(10), |
| 86 | + bottomRight: Radius.circular(10)), |
| 87 | + side: BorderSide(color: Color(0xFFff665e))), |
| 88 | + onPressed: () {}, |
| 89 | + color: Color(0xFFff665e), |
| 90 | + textColor: Colors.white, |
| 91 | + child: Container( |
| 92 | + padding: EdgeInsets.only(left: 5, right: 5, top: 15, bottom: 15), |
| 93 | + child: Text("available at shops".toUpperCase(), |
| 94 | + style: TextStyle( |
| 95 | + fontSize: 14, |
| 96 | + fontWeight: FontWeight.w400, |
| 97 | + color: Color(0xFFFFFFFF))), |
| 98 | + ), |
| 99 | + ), |
| 100 | + ], |
| 101 | + ), |
| 102 | + ); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +Widget createDetailView(BuildContext context, AsyncSnapshot snapshot) { |
| 107 | + ProductDetails values = snapshot.data; |
| 108 | + return DetailScreen( |
| 109 | + productDetails: values, |
| 110 | + ); |
| 111 | +} |
| 112 | + |
| 113 | +class DetailScreen extends StatefulWidget { |
| 114 | + ProductDetails productDetails; |
| 115 | + |
| 116 | + DetailScreen({Key key, this.productDetails}) : super(key: key); |
| 117 | + |
| 118 | + @override |
| 119 | + _DetailScreenState createState() => _DetailScreenState(); |
| 120 | +} |
| 121 | + |
| 122 | +class _DetailScreenState extends State<DetailScreen> { |
| 123 | + @override |
| 124 | + Widget build(BuildContext context) { |
| 125 | + return SingleChildScrollView( |
| 126 | + child: Column( |
| 127 | + children: <Widget>[ |
| 128 | + /*Image.network( |
| 129 | + widget.productDetails.data.productVariants[0].productImages[0]),*/ |
| 130 | + Image.network( widget.productDetails.data.productVariants[0].productImages[0],fit: BoxFit.fill, |
| 131 | + loadingBuilder:(BuildContext context, Widget child,ImageChunkEvent loadingProgress) { |
| 132 | + if (loadingProgress == null) return child; |
| 133 | + return Center( |
| 134 | + child: CircularProgressIndicator( |
| 135 | + value: loadingProgress.expectedTotalBytes != null ? |
| 136 | + loadingProgress.cumulativeBytesLoaded / loadingProgress.expectedTotalBytes |
| 137 | + : null, |
| 138 | + ), |
| 139 | + ); |
| 140 | + }, |
| 141 | + ), |
| 142 | + SizedBox( |
| 143 | + height: 10, |
| 144 | + ), |
| 145 | + Container( |
| 146 | + padding: EdgeInsets.only(left: 15, right: 15, top: 20, bottom: 20), |
| 147 | + color: Color(0xFFFFFFFF), |
| 148 | + child: Row( |
| 149 | + mainAxisAlignment: MainAxisAlignment.spaceBetween, |
| 150 | + children: <Widget>[ |
| 151 | + Text("SKU".toUpperCase(), |
| 152 | + style: TextStyle( |
| 153 | + fontSize: 16, |
| 154 | + fontWeight: FontWeight.w700, |
| 155 | + color: Color(0xFF565656))), |
| 156 | + Text(widget.productDetails.data.productVariants[0].sku, |
| 157 | + style: TextStyle( |
| 158 | + fontSize: 16, |
| 159 | + fontWeight: FontWeight.w700, |
| 160 | + color: Color(0xFFfd0100))), |
| 161 | + Icon( |
| 162 | + Icons.arrow_forward_ios, |
| 163 | + color: Color(0xFF999999), |
| 164 | + ) |
| 165 | + ], |
| 166 | + ), |
| 167 | + ), |
| 168 | + SizedBox( |
| 169 | + height: 10, |
| 170 | + ), |
| 171 | + Container( |
| 172 | + padding: EdgeInsets.only(left: 15, right: 15, top: 20, bottom: 20), |
| 173 | + color: Color(0xFFFFFFFF), |
| 174 | + child: Row( |
| 175 | + mainAxisAlignment: MainAxisAlignment.spaceBetween, |
| 176 | + children: <Widget>[ |
| 177 | + Text("Price".toUpperCase(), |
| 178 | + style: TextStyle( |
| 179 | + fontSize: 16, |
| 180 | + fontWeight: FontWeight.w700, |
| 181 | + color: Color(0xFF565656))), |
| 182 | + Text( |
| 183 | + "৳ ${(widget.productDetails.data.productVariants[0].maxPrice != null) ? widget.productDetails.data.productVariants[0].maxPrice : "Unavailable"}" |
| 184 | + .toUpperCase(), |
| 185 | + style: TextStyle( |
| 186 | + color: (widget.productDetails.data.productVariants[0] |
| 187 | + .maxPrice != |
| 188 | + null) |
| 189 | + ? Color(0xFFf67426) |
| 190 | + : Color(0xFF0dc2cd), |
| 191 | + fontFamily: 'Roboto-Light.ttf', |
| 192 | + fontSize: 20, |
| 193 | + fontWeight: FontWeight.w500)), |
| 194 | + ], |
| 195 | + ), |
| 196 | + ), |
| 197 | + SizedBox( |
| 198 | + height: 10, |
| 199 | + ), |
| 200 | + Container( |
| 201 | + alignment: Alignment.topLeft, |
| 202 | + width: double.infinity, |
| 203 | + padding: EdgeInsets.only(left: 15, right: 15, top: 20, bottom: 20), |
| 204 | + color: Color(0xFFFFFFFF), |
| 205 | + child: Column( |
| 206 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 207 | + children: <Widget>[ |
| 208 | + Text("Description", |
| 209 | + textAlign: TextAlign.left, |
| 210 | + style: TextStyle( |
| 211 | + fontSize: 16, |
| 212 | + fontWeight: FontWeight.w700, |
| 213 | + color: Color(0xFF565656))), |
| 214 | + SizedBox( |
| 215 | + height: 15, |
| 216 | + ), |
| 217 | + Text( |
| 218 | + "${widget.productDetails.data.productVariants[0].productDescription}", |
| 219 | + textAlign: TextAlign.justify, |
| 220 | + style: TextStyle( |
| 221 | + fontSize: 16, |
| 222 | + fontWeight: FontWeight.w400, |
| 223 | + color: Color(0xFF4c4c4c))), |
| 224 | + ], |
| 225 | + ), |
| 226 | + ), |
| 227 | + SizedBox( |
| 228 | + height: 10, |
| 229 | + ), |
| 230 | + Container( |
| 231 | + alignment: Alignment.topLeft, |
| 232 | + width: double.infinity, |
| 233 | + padding: EdgeInsets.only(left: 15, right: 15, top: 20, bottom: 20), |
| 234 | + color: Color(0xFFFFFFFF), |
| 235 | + child: Column( |
| 236 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 237 | + children: <Widget>[ |
| 238 | + Text("Specification", |
| 239 | + textAlign: TextAlign.left, |
| 240 | + style: TextStyle( |
| 241 | + fontSize: 16, |
| 242 | + fontWeight: FontWeight.w700, |
| 243 | + color: Color(0xFF565656))), |
| 244 | + SizedBox( |
| 245 | + height: 15, |
| 246 | + ), |
| 247 | + Column( |
| 248 | + children: generateProductSpecification(context), |
| 249 | + ) |
| 250 | + ], |
| 251 | + ), |
| 252 | + ) |
| 253 | + ], |
| 254 | + ), |
| 255 | + ); |
| 256 | + } |
| 257 | + |
| 258 | + List<Widget> generateProductSpecification(BuildContext context) { |
| 259 | + List<Widget> list = []; |
| 260 | + int count = 0; |
| 261 | + widget.productDetails.data.productSpecifications.forEach((specification) { |
| 262 | + Widget element = Container( |
| 263 | + height: 30, |
| 264 | + child: Row( |
| 265 | + mainAxisAlignment: MainAxisAlignment.spaceBetween, |
| 266 | + children: <Widget>[ |
| 267 | + Text(specification.specificationName, |
| 268 | + textAlign: TextAlign.left, |
| 269 | + style: TextStyle( |
| 270 | + fontSize: 14, |
| 271 | + fontWeight: FontWeight.w400, |
| 272 | + color: Color(0xFF444444))), |
| 273 | + Text(specification.specificationValue, |
| 274 | + textAlign: TextAlign.left, |
| 275 | + style: TextStyle( |
| 276 | + fontSize: 14, |
| 277 | + fontWeight: FontWeight.w400, |
| 278 | + color: Color(0xFF212121))), |
| 279 | + ], |
| 280 | + ), |
| 281 | + ); |
| 282 | + list.add(element); |
| 283 | + count++; |
| 284 | + }); |
| 285 | + return list; |
| 286 | + } |
| 287 | +} |
| 288 | + |
| 289 | +Future<ProductDetails> getDetailData(String slug) async { |
| 290 | + Response response; |
| 291 | + response = await get(Urls.ROOT_URL + slug); |
| 292 | + int statusCode = response.statusCode; |
| 293 | + final body = json.decode(response.body); |
| 294 | + if (statusCode == 200) { |
| 295 | + productDetails = ProductDetails.fromJson(body); |
| 296 | + return productDetails; |
| 297 | + } else { |
| 298 | + return productDetails = ProductDetails(); |
| 299 | + } |
| 300 | +} |
0 commit comments