api get:
import 'package:flutter/material.dart';
import 'package:http/http.dart' show post;
import 'dart:convert';
import 'dart:async';
import 'package:shared_preferences/shared_preferences.dart';
import '../models/cart_model.dart';
import 'bottom_navigation.dart';
// import '../screen/grocery_pro_details_screen.dart';
import '../widgets/shimmer_widget.dart';
import '../utils.dart';
class CartItemsWidget extends StatefulWidget {
@override
_CartItemsWidgetState createState() => _CartItemsWidgetState();
}
class _CartItemsWidgetState extends State<CartItemsWidget> {
String logtoken;
Future mycatList;
Future fetchData() async {
SharedPreferences localStorage = await SharedPreferences.getInstance();
logtoken = localStorage.getString('mytoken');
var postdata = jsonEncode({'token': logtoken});
var fullUrl = Constants.BaseUrl + "cart-items";
var response =
await post(fullUrl, body: postdata, headers: Constants.setHeaders());
var resBody = CartModel.fromJson(json.decode(response.body));
return resBody.cartData;
}
@override
void initState() {
super.initState();
mycatList = fetchData();
}
@override
Widget build(BuildContext context) {
return Expanded(
child: Container(
child: FutureBuilder(
future: mycatList,
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
return ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
physics: ClampingScrollPhysics(),
itemCount: snapshot.data == null ? 0 : snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
return SingleChildScrollView(
child: Card(
elevation: 1,
margin: EdgeInsets.only(bottom: 4),
shadowColor: Color.fromRGBO(255, 255, 255, 0.7),
child: new InkWell(
onTap: () {
// Navigator.of(context).push(
// MaterialPageRoute(
// builder: (context) => ProDetailsScreen(
// proid: snapshot.data[index].proId,
// ),
// ),
// );
},
child: Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(5.0),
child: Container(
width: 70.0,
height: 70.0,
decoration: new BoxDecoration(
image: new DecorationImage(
fit: BoxFit.contain,
image: NetworkImage(
snapshot.data[index].proImg),
),
),
),
),
Flexible(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
snapshot.data[index].proName,
textAlign: TextAlign.left,
style: TextStyle(
color: Color(0xff0E918C),
fontSize: 15.0,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 2),
Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: <Widget>[
RichText(
text: TextSpan(
children: [
WidgetSpan(
child: Container(
//color: Colors.red,
padding:
EdgeInsets.only(right: 8.0),
child: Text(
"Rs: ${snapshot.data[index].proSp}",
style: TextStyle(
color: Color(0xFF069040),
fontSize: 15,
fontWeight: FontWeight.w700,
),
),
),
),
WidgetSpan(
child: Container(
//color: Colors.red,
padding:
EdgeInsets.only(right: 8.0),
child: Text(
snapshot.data[index].proPrice
.toString(),
style: TextStyle(
color: Color(0xFF9FA1A1),
fontSize: 12,
decoration: TextDecoration
.lineThrough,
),
),
),
),
percentageOff(
snapshot.data[index].proOff),
],
),
),
],
),
Padding(
padding: EdgeInsets.only(
top: 8,
right: 5,
bottom: 5,
),
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceAround,
children: <Widget>[
Text(
"Total: ${snapshot.data[index].proSp} x ${snapshot.data[index].proQty} = Rs: ${snapshot.data[index].proSp * snapshot.data[index].proQty}"),
Expanded(
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceAround,
children: <Widget>[
Expanded(
flex: 3,
child: Row(
mainAxisAlignment:
MainAxisAlignment.end,
children: <Widget>[
InkWell(
onTap: () {
if (snapshot.data[index]
.proQty >
0) {
setState(() {
snapshot.data[index]
.proQty--;
});
_addTocart(
snapshot
.data[index]
.proId,
snapshot
.data[index]
.proQty);
}
},
child: Container(
decoration:
BoxDecoration(
borderRadius:
BorderRadius.only(
topLeft:
Radius.circular(
4.0),
topRight:
Radius.zero,
bottomLeft:
Radius.circular(
4.0),
bottomRight:
Radius.zero,
),
border: Border.all(
color: Color(
0xFFAC3709),
),
),
width: 25,
height: 30,
child: Icon(
Icons.add,
color:
Color(0xFF0F0F0F),
size: 16.0,
),
),
),
Container(
decoration: BoxDecoration(
color:
Color(0xFFAC3709),
borderRadius:
BorderRadius
.circular(0),
border: Border.all(
color:
Color(0xFFAC3709),
),
),
width: 30,
height: 30,
child: Center(
child: Text(
snapshot.data[index]
.proQty
.toString(),
textAlign:
TextAlign.center,
style: TextStyle(
color: Color(
0xFFF6F6F6),
fontSize: 14,
fontWeight:
FontWeight.bold,
),
),
),
),
InkWell(
onTap: () {
setState(() {
snapshot.data[index]
.proQty++;
});
_addTocart(
snapshot.data[index]
.proId,
snapshot.data[index]
.proQty);
},
child: Container(
decoration:
BoxDecoration(
borderRadius:
BorderRadius.only(
topLeft:
Radius.zero,
topRight:
Radius.circular(
4.0),
bottomLeft:
Radius.zero,
bottomRight:
Radius.circular(
4.0),
),
border: Border.all(
color: Color(
0xFFAC3709),
),
),
width: 25,
height: 30,
child: Icon(
Icons.add,
color:
Color(0xFF0F0F0F),
size: 20.0,
),
),
),
],
),
)
// Expanded(
// flex: 3,
// child: ClipRRect(
// borderRadius:
// BorderRadius.circular(0),
// child: FlatButton(
// padding: EdgeInsets.symmetric(
// vertical: 10,
// horizontal: 1),
// color: Color(0xFF077E21),
// child: Text(
// "Add",
// style: TextStyle(
// color: Colors.white,
// fontWeight:
// FontWeight.bold,
// fontSize: 12.0,
// ),
// ),
// onPressed: _addTocart,
// ),
// ),
// ),
],
),
),
],
),
),
],
),
),
],
),
),
),
);
},
);
} else if (snapshot.hasError) {
//return Center(child: Text("Please Try later"));
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Icon(
Icons.error_outline,
color: Colors.red,
size: 60,
),
Padding(
padding: const EdgeInsets.only(top: 16),
child: Text('Error: ${snapshot.error}'),
)
],
),
);
} else {
return ShimmerList();
}
},
),
),
);
}
void _addTocart(proid, nowqty) async {
String logtoken;
SharedPreferences localStorage = await SharedPreferences.getInstance();
logtoken = localStorage.getString('mytoken');
var postdata =
jsonEncode({'token': logtoken, 'proid': proid, 'qty': nowqty});
var fullUrl = Constants.BaseUrl + "add-to-cart";
var response =
await post(fullUrl, body: postdata, headers: Constants.setHeaders());
var resBody = json.decode(response.body);
if (resBody['status']) {
myTost(resBody['msg'], true);
//openBottomSheet();
CartItemsWidget();
} else {
myTost(resBody['msg'], false);
}
}
percentageOff(off) {
if (off != null && off > 0) {
return WidgetSpan(
child: Container(
//color: Colors.red,
padding: EdgeInsets.only(
right: 5.0,
left: 5.0,
top: 2.0,
bottom: 1.0,
),
decoration: BoxDecoration(
border: Border.all(
color: Color(0xFF880617),
),
borderRadius: BorderRadius.all(
Radius.circular(500.0),
),
),
child: Text(
"10% Off",
style: TextStyle(
color: Color(0xff0E918C),
fontSize: 9,
),
),
),
);
} else {
return WidgetSpan(child: Container());
}
}
}
0 Comments