Tuesday, April 27, 2021

Edward Lance Lorilla

Edward Lance Lorilla


【JAVASCRIPT】social media share native component

Posted: 27 Apr 2021 01:01 AM PDT

<div id="parent">
<social-share-component socialNetwork="tw" user="edwardlancelorilla"></social-share-component>
<social-share-component socialNetwork="fb" user="edwardlancelorilla"></social-share-component>
</div>
view raw index.html hosted with ❤ by GitHub
class SocialShareComponent extends HTMLElement {
constructor() {
super();
this.root = this.attachShadow({mode: 'open'});
this.container = document.createElement('div');
this.root.appendChild(this.container);
switch(this.socialNetwork) {
case 'tw':
this.container.innerHTML = SocialShareComponent.twTemplate(this.user);
break;
case 'fb':
this.container.innerHTML = SocialShareComponent.fbTemplate(this.user);
break;
}
}
get socialNetwork() {
return this.getAttribute('socialNetwork') || 'tw';
}
set socialNetwork(newValue) {
this.setAttribute('socialNetwork', newValue);
}
get user() {
return this.getAttribute('user') || 'none';
}
set user(newValue) {
this.setAttribute('user', newValue);
}
static twTemplate(user) {
return `
${SocialShareComponent.twStyle()}
<span class="twitter-button">
<a href="https://twitter.com/${user}">
Follow @${user}
</a>
</span>`;
}
static twStyle() {
return `
<style>
a {
height: 20px;
padding: 3px 6px;
background-color: #1b95e0;
color: #fff;
border-radius: 3px;
font-weight: 500;
font-size: 11px;
font-family:'Helvetica Neue', Arial, sans-serif;
line-height: 18px;
text-decoration: none;
}
a:hover { background-color: #0c7abf; }
span {
margin: 5px 2px;
}
</style>`;
}
static fbTemplate(user) {
return `
${SocialShareComponent.fbStyle()}
<span class="facebook-button">
<a href="https://facebook.com/${user}">
Follow @${user}
</a>
</span>`;
}
static fbStyle() {
return `
<style>
a {
height: 20px;
padding: 3px 6px;
background-color: #4267b2;
color: #fff;
border-radius: 3px;
font-weight: 500;
font-size: 11px;
font-family:'Helvetica Neue', Arial, sans-serif;
line-height: 18px;
text-decoration: none;
}
a:hover { background-color: #0c7abf; }
span {
margin: 5px 2px;
}
</style>`;
}
}
customElements.define('social-share-component', SocialShareComponent);
view raw script.js hosted with ❤ by GitHub

【LARAVEL and FLUTTER ANDROID STUDIO and IOS】Ecommerce Shopping

Posted: 27 Apr 2021 12:54 AM PDT

Flutter Code
import 'package:ecom_app/screens/home_screen.dart';
import 'package:flutter/material.dart';
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: HomeScreen(),
);
}
}
view raw app.dart hosted with ❤ by GitHub
import 'package:ecom_app/models/product.dart';
import 'package:ecom_app/screens/checkout_screen.dart';
import 'package:ecom_app/screens/login_screen.dart';
import 'package:ecom_app/services/cart_service.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
class CartScreen extends StatefulWidget {
final List<Product> cartItems;
CartScreen(this.cartItems);
@override
_CartScreenState createState() => _CartScreenState();
}
class _CartScreenState extends State<CartScreen> {
double _total;
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
CartService _cartService = CartService();
@override
void initState() {
super.initState();
_getTotal();
}
_getTotal() {
_total = 0.0;
this.widget.cartItems.forEach((item) {
setState(() {
_total += (item.price - item.discount) * item.quantity;
});
});
}
void _checkOut(List<Product> cartItems) async {
SharedPreferences _prefs = await SharedPreferences.getInstance();
int _userId = _prefs.getInt('userId');
if (_userId != null && _userId > 0) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => CheckoutScreen(cartItems: cartItems)));
} else {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => LoginScreen(cartItems: cartItems)));
}
}
_showSnackMessage(message) {
var snackBar = SnackBar(
content: message,
);
_scaffoldKey.currentState.showSnackBar(snackBar);
}
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
appBar: AppBar(
leading: Text(''),
title: Text('Items in Cart'),
backgroundColor: Colors.redAccent,
actions: <Widget>[
IconButton(
icon: Icon(Icons.close),
onPressed: () {
Navigator.pop(context);
},
)
],
),
body: ListView.builder(
itemCount: this.widget.cartItems.length,
itemBuilder: (context, index) {
return Dismissible(
key: Key(this.widget.cartItems[index].id.toString()),
onDismissed: (param) {
_deleteCartItem(index, this.widget.cartItems[index].id);
},
background: Container(
color: Colors.redAccent,
),
child: Card(
child: ListTile(
leading: Image.network(this.widget.cartItems[index].photo),
title: Text(this.widget.cartItems[index].name,
style:
TextStyle(fontSize: 17, fontWeight: FontWeight.bold)),
subtitle: Row(
children: <Widget>[
Text(
'\$${this.widget.cartItems[index].price -
this.widget.cartItems[index].discount}',
style:
TextStyle(fontSize: 17, fontWeight: FontWeight.bold),
),
Text(
' x ',
style:
TextStyle(fontSize: 17, fontWeight: FontWeight.bold),
),
Text(
'${this.widget.cartItems[index].quantity}',
style:
TextStyle(fontSize: 17, fontWeight: FontWeight.bold),
),
Text(
' = ',
style:
TextStyle(fontSize: 17, fontWeight: FontWeight.bold),
),
Text(
'\$${(this.widget.cartItems[index].price - this.widget
.cartItems[index].discount) *
this.widget.cartItems[index].quantity}',
style:
TextStyle(fontSize: 17, fontWeight: FontWeight.bold),
)
],
),
trailing: Column(
children: <Widget>[
InkWell(
onTap: () {
setState(() {
_total += this.widget.cartItems[index].price -
this.widget.cartItems[index].discount;
this.widget.cartItems[index].quantity++;
});
},
child: Icon(
Icons.arrow_drop_up,
size: 21,
),
),
Text('${this.widget.cartItems[index].quantity}',
style: TextStyle(
fontSize: 11, fontWeight: FontWeight.bold)),
InkWell(
onTap: () {
setState(() {
if (this.widget.cartItems[index].quantity > 1) {
_total -= this.widget.cartItems[index].price -
this.widget.cartItems[index].discount;
this.widget.cartItems[index].quantity--;
}
});
},
child: Icon(
Icons.arrow_drop_down,
size: 21,
),
)
],
),
),
),
);
},
),
bottomNavigationBar: Container(
color: Colors.white,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: <Widget>[
Expanded(
child: Text(
'Total : \$$_total',
style: TextStyle(
fontWeight: FontWeight.bold, color: Colors.redAccent),
),
),
Expanded(
child: RaisedButton(
color: Colors.redAccent,
onPressed: () {
_checkOut(this.widget.cartItems);
},
child: Text(
'Checkout',
style: TextStyle(color: Colors.white),
),
),
)
],
),
),
),
);
}
void _deleteCartItem(int index, int id) async {
setState(() {
this.widget.cartItems.removeAt(index);
});
var result = await _cartService.deleteCartItemById(id);
if (result > 0) {
_showSnackMessage(Text(
'One item deleted from cart',
style: TextStyle(color: Colors.red),
));
} else {
_showSnackMessage(Text(
'Cart items can not be deleted!',
style: TextStyle(color: Colors.yellow),
));
}
}
}
import 'package:ecom_app/models/product.dart';import 'package:ecom_app/repository/repository.dart';
class CartService {
Repository _repository;
CartService() {
_repository = Repository();
}
addToCart(Product product) async {
List<Map> items = await _repository.getLocalByCondition(
'carts', 'productId', product.id);
if (items.length > 0) {
product.quantity = items.first['productQuantity'] + 1;
return await _repository.updateLocal(
'carts', 'productId', product.toMap());
}
product.quantity = 1;
return await _repository.saveLocal('carts', product.toMap());
}
getCartItems() async {
return await _repository.getAllLocal('carts');
}
deleteCartItemById(int id) async {
return await _repository.deleteLocalById('carts', id);
}
makeTheCartEmpty() async {
return await _repository.deleteLocal('carts');
}
}
class Category {
int id;
String name;
String icon;
}
view raw category.dart hosted with ❤ by GitHub
import 'package:ecom_app/repository/repository.dart';
class CategoryService {
Repository _repository;
CategoryService(){
_repository = Repository();
}
getCategories() async {
return await _repository.httpGet('categories');
}
}
import 'dart:convert';
import 'package:ecom_app/models/product.dart';
import 'package:ecom_app/models/shipping.dart';
import 'package:ecom_app/screens/payment_screen.dart';
import 'package:ecom_app/services/shipping_service.dart';
import 'package:flutter/material.dart';
class CheckoutScreen extends StatefulWidget {
final List<Product> cartItems;
CheckoutScreen({this.cartItems});
@override
_CheckoutScreenState createState() => _CheckoutScreenState();
}
class _CheckoutScreenState extends State<CheckoutScreen> {
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
final name = TextEditingController();
final email = TextEditingController();
final address = TextEditingController();
_showSnackMessage(message) {
var snackBar = SnackBar(
content: message,
);
_scaffoldKey.currentState.showSnackBar(snackBar);
}
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
backgroundColor: Colors.white,
appBar: AppBar(
title: Text('Checkout'),
backgroundColor: Colors.redAccent,
leading: Text(''),
elevation: 0.0,
actions: <Widget>[
IconButton(
icon: Icon(
Icons.close,
color: Colors.white,
),
onPressed: () {
Navigator.pop(context);
},
),
],
),
body: Padding(
padding: const EdgeInsets.only(top: 0.0),
child: ListView(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(
left: 28.0, top: 28.0, right: 28.0, bottom: 14.0),
child: Text('Shipping Address',
style:
TextStyle(fontSize: 28.0, fontWeight: FontWeight.bold)),
),
Divider(
height: 5.0,
color: Colors.black,
),
Padding(
padding: const EdgeInsets.only(
left: 28.0, top: 14.0, right: 28.0, bottom: 14.0),
child: TextField(
controller: name,
decoration: InputDecoration(
hintText: 'Enter your name', labelText: 'Enter your name'),
),
),
Padding(
padding: const EdgeInsets.only(
left: 28.0, top: 14.0, right: 28.0, bottom: 14.0),
child: TextField(
keyboardType: TextInputType.emailAddress,
controller: email,
decoration: InputDecoration(
hintText: 'Enter your email address',
labelText: 'Enter your email address'),
),
),
Padding(
padding: const EdgeInsets.only(
left: 28.0, top: 14.0, right: 28.0, bottom: 14.0),
child: TextField(
controller: address,
maxLines: 3,
decoration:
InputDecoration(hintText: 'Address', labelText: 'Address'),
),
),
Column(
children: <Widget>[
ButtonTheme(
minWidth: 320.0,
height: 45.0,
child: FlatButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(7.0)),
color: Colors.redAccent,
onPressed: () {
var shipping = Shipping();
shipping.name = name.text;
shipping.email = email.text;
shipping.address = address.text;
_shipping(context, shipping);
},
child: Text('Continue to Payment',
style: TextStyle(color: Colors.white)),
),
),
],
),
],
),
),
);
}
void _shipping(BuildContext context, Shipping shipping) async {
var _shippingService = ShippingService();
var shippingData = await _shippingService.addShipping(shipping);
print("resuly"+shippingData.body);
var result = jsonDecode(shippingData.body);
if (result['result'] == true) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
PaymentScreen(
cartItems: this.widget.cartItems,
)));
} else {
_showSnackMessage(
Text('Failed to add shipping', style: TextStyle(color: Colors.red),));
}
}
}
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
class DatabaseConnection {
initDatabase() async {
var directory = await getApplicationDocumentsDirectory();
var path = join(directory.path, 'db_ecom_1');
var database =
await openDatabase(path, version: 1, onCreate: _onCreatingDatabase);
return database;
}
_onCreatingDatabase(Database db, int version) async {
await db.execute(
"CREATE TABLE carts(id INTEGER PRIMARY KEY, productId INTEGER, productName TEXT, productPhoto TEXT,detail TEXT, productPrice INTEGER, productDiscount INTEGER, productQuantity INTEGER)");
}
}
import 'package:ecom_app/models/product.dart';
import 'package:ecom_app/screens/product_detail.dart';
import 'package:flutter/material.dart';
class HomeHotProduct extends StatefulWidget {
final Product product;
HomeHotProduct(this.product);
@override
_HomeHotProductState createState() => _HomeHotProductState();
}
class _HomeHotProductState extends State<HomeHotProduct> {
@override
Widget build(BuildContext context) {
return Container(
width: 190.0,
height: 260.0,
child: InkWell(
onTap: (){
Navigator.push(context, MaterialPageRoute(builder: (context) => ProductDetail(this.widget.product)));
},
child: Card(
child: Column(
children: <Widget>[
Text(this.widget.product.name),
Image.network(widget.product.photo, width: 190.0, height: 160.0,),
Row(children: <Widget>[
Text('Price: ${this.widget.product.price}'),
Text('Discount: ${this.widget.product.discount}'),
],)
],
),
),
),
);
}
}
import 'package:ecom_app/models/product.dart';
import 'package:flutter/material.dart';
import 'home_hot_product.dart';
class HomeHotProducts extends StatefulWidget {
final List<Product> productList;
HomeHotProducts({this.productList});
@override
_HomeHotProductsState createState() => _HomeHotProductsState();
}
class _HomeHotProductsState extends State<HomeHotProducts> {
@override
Widget build(BuildContext context) {
return Container(
height: 205,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: this.widget.productList.length,
itemBuilder: (context, index) {
return
HomeHotProduct(this.widget.productList[index]);
},
),
);
}
}
import 'package:ecom_app/models/product.dart';
import 'package:ecom_app/screens/product_detail.dart';
import 'package:flutter/material.dart';
class HomeNewArrivalProduct extends StatefulWidget {
final Product product;
HomeNewArrivalProduct(this.product);
@override
_HomeNewArrivalProductState createState() => _HomeNewArrivalProductState();
}
class _HomeNewArrivalProductState extends State<HomeNewArrivalProduct> {
@override
Widget build(BuildContext context) {
return Container(
width: 190.0,
height: 260.0,
child: InkWell(
onTap: () {
Navigator.push(context, MaterialPageRoute(
builder: (context) => ProductDetail(this.widget.product)));
},
child: Card(
child: Column(
children: <Widget>[
Text(this.widget.product.name),
Image.network(widget.product.photo, width: 190.0, height: 160.0,),
Row(children: <Widget>[
Text('Price: ${this.widget.product.price}'),
Text('Discount: ${this.widget.product.discount}'),
],)
],
),
),
),
);
}
}
import 'package:ecom_app/models/product.dart';
import 'package:ecom_app/widgets/home_new_arrival_product.dart';
import 'package:flutter/material.dart';
class HomeNewArrivalProducts extends StatefulWidget {
final List<Product> productList;
HomeNewArrivalProducts({this.productList});
@override
_HomeNewArrivalProductsState createState() => _HomeNewArrivalProductsState();
}
class _HomeNewArrivalProductsState extends State<HomeNewArrivalProducts> {
@override
Widget build(BuildContext context) {
return Container(
height: 205,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: this.widget.productList.length,
itemBuilder: (context, index) {
return
HomeNewArrivalProduct(this.widget.productList[index]);
},
),
);
}
}
import 'package:ecom_app/models/category.dart';
import 'package:flutter/material.dart';
import 'home_product_category.dart';
class HomeProductCategories extends StatefulWidget {
final List<Category> categoryList;
HomeProductCategories({this.categoryList});
@override
_HomeProductCategoriesState createState() => _HomeProductCategoriesState();
}
class _HomeProductCategoriesState extends State<HomeProductCategories> {
@override
Widget build(BuildContext context) {
return Container(
height: 205,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: this.widget.categoryList.length,
itemBuilder: (context, index) {
return HomeProductCategory(this.widget.categoryList[index].icon,
this.widget.categoryList[index].name,
this.widget.categoryList[index].id);
},
),
);
}
}
import 'package:ecom_app/screens/products_by_category_screen.dart';import 'package:flutter/material.dart';
class HomeProductCategory extends StatefulWidget {
final int categoryId;
final String categoryIcon;
final String categoryName;
HomeProductCategory(this.categoryIcon, this.categoryName, this.categoryId);
@override
_HomeProductCategoryState createState() => _HomeProductCategoryState();
}
class _HomeProductCategoryState extends State<HomeProductCategory> {
@override
Widget build(BuildContext context) {
return Container(
width: 140.0,
height: 190.0,
child: InkWell(
onTap: () {
Navigator.push(context, MaterialPageRoute(builder: (context) =>
ProductsByCategoryScreen(categoryName: widget.categoryName,
categoryId: widget.categoryId)));
},
child: Card(
child: Column(
children: <Widget>[
Image.network(widget.categoryIcon, width: 190.0, height: 160.0,),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(widget.categoryName),
),
],
),
),
),
);
}
}
import 'dart:convert';
import 'package:ecom_app/models/category.dart';
import 'package:ecom_app/models/product.dart';
import 'package:ecom_app/screens/cart_screen.dart';
import 'package:ecom_app/services/cart_service.dart';
import 'package:ecom_app/services/category_service.dart';
import 'package:ecom_app/services/product_service.dart';
import 'package:ecom_app/services/slider_service.dart';
import 'package:ecom_app/widgets/carousel_slider.dart';
import 'package:ecom_app/widgets/home_hot_products.dart';
import 'package:ecom_app/widgets/home_new_arrival_products.dart';
import 'package:ecom_app/widgets/home_product_categories.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
SliderService _sliderService = SliderService();
CategoryService _categoryService = CategoryService();
ProductService _productService = ProductService();
List<Category> _categoryList = List<Category>();
List<Product> _productList = List<Product>();
List<Product> _newArrivalproductList = List<Product>();
CartService _cartService = CartService();
List<Product> _cartItems;
var items = [];
@override
void initState() {
super.initState();
_getAllSliders();
_getAllCategories();
_getAllHotProducts();
_getAllNewArrivalProducts();
_getCartItems();
_setSharedPrefs();
}
_setSharedPrefs() async {
SharedPreferences _prefs = await SharedPreferences.getInstance();
_prefs.setInt('userId', 0);
_prefs.setString('userName', '');
_prefs.setString('userEmail', '');
}
_getCartItems() async {
_cartItems = List<Product>();
var cartItems = await _cartService.getCartItems();
cartItems.forEach((data) {
var product = Product();
product.id = data['productId'];
product.name = data['productName'];
product.photo = data['productPhoto'];
product.price = data['productPrice'];
product.discount = data['productDiscount'];
product.detail = data['detail'] ?? 'No detail';
product.quantity = data['productQuantity'];
setState(() {
_cartItems.add(product);
});
});
}
_getAllSliders() async {
var sliders = await _sliderService.getSliders();
var result = json.decode(sliders.body);
result['data'].forEach((data) {
setState(() {
items.add(NetworkImage(data['image_url']));
});
});
}
_getAllCategories() async {
var categories = await _categoryService.getCategories();
var result = json.decode(categories.body);
result['data'].forEach((data) {
var model = Category();
model.id = data['id'];
model.name = data['categoryName'];
model.icon = data['categoryIcon'];
setState(() {
_categoryList.add(model);
});
});
}
_getAllHotProducts() async {
var hotProducts = await _productService.getHotProducts();
var result = json.decode(hotProducts.body);
result['data'].forEach((data) {
var model = Product();
model.id = data['id'];
model.name = data['name'];
model.photo = data['photo'];
model.price = data['price'];
model.discount = data['discount'];
model.detail = data['detail'];
setState(() {
_productList.add(model);
});
});
}
_getAllNewArrivalProducts() async {
var newArrivalProducts = await _productService.getNewArrivalProducts();
var result = json.decode(newArrivalProducts.body);
result['data'].forEach((data) {
var model = Product();
model.id = data['id'];
model.name = data['name'];
model.photo = data['photo'];
model.price = data['price'];
model.discount = data['discount'];
model.detail = data['detail'];
setState(() {
_newArrivalproductList.add(model);
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('eComm App'),
backgroundColor: Colors.redAccent,
actions: <Widget>[
InkWell(
onTap: () {
Navigator.push(context, MaterialPageRoute(
builder: (context) => CartScreen(_cartItems)));
},
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Container(
height: 150,
width: 30,
child: Stack(
children: <Widget>[
IconButton(
iconSize: 30,
icon: Icon(Icons.shopping_cart, color: Colors.white,),
onPressed: () {
},
),
Positioned(
child: Stack(
children: <Widget>[
Icon(Icons.brightness_1, size: 25, color: Colors
.black),
Positioned(
top: 4.0,
right: 8.0,
child: Center(
child: Text(_cartItems.length.toString())),
),
],
),
),
],
),
),
),
)
],
),
body: Container(
child: ListView(
children: <Widget>[
carouselSlider(items),
Padding(
padding: EdgeInsets.all(10.0),
child: Text('Product Categories'),
),
HomeProductCategories(
categoryList: _categoryList,
),
Padding(
padding: EdgeInsets.all(10.0),
child: Text('Hot Products'),
),
HomeHotProducts(
productList: _productList,
),
Padding(
padding: EdgeInsets.all(10.0),
child: Text('New Arrival Products'),
),
HomeNewArrivalProducts(
productList: _newArrivalproductList,
)
],
)),
);
}
}
import 'dart:convert';
import 'package:ecom_app/models/product.dart';
import 'package:ecom_app/models/user.dart';
import 'package:ecom_app/screens/registration_screen.dart';
import 'package:ecom_app/services/user_service.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'checkout_screen.dart';
class LoginScreen extends StatefulWidget {
final List<Product> cartItems;
LoginScreen({this.cartItems});
@override
_LoginScreenState createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
final email = TextEditingController();
final password = TextEditingController();
_login(BuildContext context, User user) async {
var _userService = UserService();
var registeredUser = await _userService.login(user);
var result = json.decode(registeredUser.body);
if (result['result'] == true) {
SharedPreferences _prefs = await SharedPreferences.getInstance();
_prefs.setInt('userId', result['user']['id']);
_prefs.setString('userName', result['user']['name']);
_prefs.setString('userEmail', result['user']['email']);
Navigator.push(
context, MaterialPageRoute(builder: (context) =>
CheckoutScreen(cartItems: this.widget.cartItems,)));
} else {
_showSnackMessage(
Text('Failed to login!', style: TextStyle(color: Colors.red),));
}
}
_showSnackMessage(message) {
var snackBar = SnackBar(
content: message,
);
_scaffoldKey.currentState.showSnackBar(snackBar);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
backgroundColor: Colors.white,
elevation: 0.0,
actions: <Widget>[
IconButton(
icon: Icon(
Icons.close,
color: Colors.red,
),
onPressed: () {
Navigator.pop(context);
},
),
],
),
body: Padding(
padding: const EdgeInsets.only(top: 120),
child: ListView(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(
left: 48.0, top: 14.0, right: 48.0, bottom: 14.0),
child: TextField(
controller: email,
decoration: InputDecoration(
hintText: 'youremail@example.com',
labelText: 'Enter your email'
),
),
),
Padding(
padding: const EdgeInsets.only(
left: 48.0, top: 14.0, right: 48.0, bottom: 14.0),
child: TextField(
controller: password,
obscureText: true,
decoration: InputDecoration(
hintText: 'Enter your password', labelText: '******'
),
),
),
Column(
children: <Widget>[
ButtonTheme(
minWidth: 320,
height: 45.0,
child: FlatButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5.0)),
color: Colors.redAccent,
onPressed: () {
var user = User();
user.email = email.text;
user.password = password.text;
_login(context, user);
},
child: Text(
'Log in',
style: TextStyle(color: Colors.white),
),
),
),
FlatButton(
onPressed: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) =>
RegistrationScreen(cartItems: this.widget
.cartItems,)));
},
child: FittedBox(child: Text('Register your account')),
),
],
),
],
),
),
);
}
}
import 'widgets/app.dart';
import 'package:flutter/material.dart';
void main() => runApp(App());
view raw main.dart hosted with ❤ by GitHub
import 'dart:convert';
import 'package:ecom_app/models/product.dart';
class Payment {
int id;
String name;
String email;
String cardNumber;
String expiryMonth;
String expiryYear;
String cvcNumber;
int userId;
List<Product> cartItems;
toJson() {
return {
'id': id.toString(),
'userId': userId.toString(),
'name': name,
'email': email,
'cardNumber': cardNumber,
'expiryMonth': expiryMonth,
'expiryYear': expiryYear,
'cvcNumber': cvcNumber,
'cartItems': json.encoder.convert(cartItems)
};
}
}
view raw payment.dart hosted with ❤ by GitHub
import 'dart:async';
import 'dart:convert';
import 'package:ecom_app/models/payment.dart';
import 'package:ecom_app/models/product.dart';
import 'package:ecom_app/screens/home_screen.dart';
import 'package:ecom_app/services/cart_service.dart';
import 'package:ecom_app/services/payment_service.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
class PaymentScreen extends StatefulWidget {
final List<Product> cartItems;
PaymentScreen({this.cartItems});
@override
_PaymentScreenState createState() => _PaymentScreenState();
}
class _PaymentScreenState extends State<PaymentScreen> {
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
final _cardHolderName = TextEditingController();
final _cardHolderEmail = TextEditingController();
final _cardNumber = TextEditingController();
final _expiryMonth = TextEditingController();
final _expiryYear = TextEditingController();
final _cvcNumber = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
backgroundColor: Colors.white,
appBar: AppBar(
title: Text('Make payment'),
backgroundColor: Colors.redAccent,
leading: Text(''),
elevation: 0.0,
actions: <Widget>[
IconButton(
icon: Icon(
Icons.close,
color: Colors.white,
),
onPressed: () {
Navigator.pop(context);
},
),
],
),
body: Padding(
padding: const EdgeInsets.only(top: 0.0),
child: ListView(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(
left: 28.0, top: 28.0, right: 28.0, bottom: 14.0),
child: Text('Make payment',
style:
TextStyle(fontSize: 28.0, fontWeight: FontWeight.bold)),
),
Divider(
height: 5.0,
color: Colors.black,
),
Padding(
padding: const EdgeInsets.only(
left: 28.0, top: 14.0, right: 28.0, bottom: 14.0),
child: TextField(
keyboardType: TextInputType.emailAddress,
controller: _cardHolderEmail,
decoration: InputDecoration(hintText: 'Email'),
),
),
Padding(
padding: const EdgeInsets.only(
left: 28.0, top: 14.0, right: 28.0, bottom: 14.0),
child: TextField(
controller: _cardHolderName,
decoration: InputDecoration(hintText: 'Name'),
),
),
Padding(
padding: const EdgeInsets.only(
left: 28.0, top: 14.0, right: 28.0, bottom: 14.0),
child: TextField(
controller: _cardNumber,
decoration: InputDecoration(
hintText: 'Card Number',
),
),
),
Padding(
padding: const EdgeInsets.only(
left: 28.0, top: 14.0, right: 28.0, bottom: 14.0),
child: TextField(
controller: _expiryMonth,
decoration: InputDecoration(
hintText: 'Expiry Month',
),
),
),
Padding(
padding: const EdgeInsets.only(
left: 28.0, top: 14.0, right: 28.0, bottom: 14.0),
child: TextField(
controller: _expiryYear,
decoration: InputDecoration(
hintText: 'Expiry Year',
),
),
),
Padding(
padding: const EdgeInsets.only(
left: 28.0, top: 14.0, right: 28.0, bottom: 14.0),
child: TextField(
controller: _cvcNumber,
obscureText: true,
decoration: InputDecoration(
hintText: 'CVC',
),
),
),
Column(
children: <Widget>[
ButtonTheme(
minWidth: 320.0,
height: 45.0,
child: FlatButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(7.0)),
color: Colors.redAccent,
onPressed: () async {
SharedPreferences _prefs =
await SharedPreferences.getInstance();
var payment = Payment();
payment.userId = _prefs.getInt('userId');
payment.name = _cardHolderName.text;
payment.email = _cardHolderEmail.text;
payment.cardNumber = _cardNumber.text;
payment.expiryMonth = _expiryMonth.text;
payment.expiryYear = _expiryYear.text;
payment.cvcNumber = _cvcNumber.text;
payment.cartItems = this.widget.cartItems;
_makePayment(context, payment);
},
child: Text('Make Payment',
style: TextStyle(color: Colors.white)),
),
),
],
),
],
),
),
);
}
void _makePayment(BuildContext context, Payment payment) async {
PaymentService _paymentService = PaymentService();
var paymentData = await _paymentService.makePayment(payment);
var result = json.decode(paymentData.body);
if (result['result'] == true) {
CartService _cartService = CartService();
this.widget.cartItems.forEach((cartItem) {
_cartService.deleteCartItemById(cartItem.id);
});
_showPaymentSuccessMessage(context);
Timer(Duration(seconds: 2), () {
Navigator.pop(context);
Navigator.push(
context, MaterialPageRoute(builder: (context) => HomeScreen()));
});
}
}
_showPaymentSuccessMessage(BuildContext context) {
return showDialog(
context: context,
barrierDismissible: true,
builder: (context) {
return AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0))),
content: Container(
height: 360,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.asset('assets/success.png'),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Order & Payment is successfully done!',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 24.0),
),
),
],
),
),
);
});
}
}
import 'package:ecom_app/models/payment.dart';
import 'package:ecom_app/repository/repository.dart';
class PaymentService {
Repository _repository;
PaymentService() {
_repository = Repository();
}
makePayment(Payment payment) async {
return await _repository.httpPost('make-payment', payment.toJson());
}
}
class Product {
int id;
String name;
String photo;
int price;
int discount;
String detail;
int quantity;
toMap() {
var map = Map<String, dynamic>();
map['productId'] = id;
map['productName'] = name;
map['productPhoto'] = photo;
map['detail'] = detail;
map['productPrice'] = price;
map['productDiscount'] = discount;
map['productQuantity'] = quantity;
return map;
}
toJson() {
return {
'productId': id.toString(),
'productName': name.toString(),
'productPhoto': photo.toString(),
'productPrice': price.toString(),
'productDiscount': discount.toString(),
'productQuantity': quantity.toString(),
'detail': detail.toString(),
};
}
}
view raw product.dart hosted with ❤ by GitHub
import 'package:ecom_app/models/product.dart';
import 'package:ecom_app/screens/product_detail.dart';
import 'package:flutter/material.dart';
class ProductByCategory extends StatefulWidget {
final Product product;
ProductByCategory(this.product);
@override
_ProductByCategoryState createState() => _ProductByCategoryState();
}
class _ProductByCategoryState extends State<ProductByCategory> {
@override
Widget build(BuildContext context) {
return Container(
height: 260,
width: 190,
child: InkWell(
onTap: () {
Navigator.push(context, MaterialPageRoute(
builder: (context) => ProductDetail(this.widget.product)));
},
child: Card(
child: Column(
children: <Widget>[
Text(this.widget.product.name),
Image.network(widget.product.photo, width: 190.0, height: 160.0,),
Row(children: <Widget>[
Text('Price: ${this.widget.product.price}'),
Text('Discount: ${this.widget.product.discount}'),
],)
],
),
),
),
);
}
}
import 'dart:convert';
import 'package:ecom_app/models/product.dart';import 'package:ecom_app/screens/cart_screen.dart';
import 'package:ecom_app/services/cart_service.dart';
import 'package:flutter/material.dart';
class ProductDetail extends StatefulWidget {
final Product product;
ProductDetail(this.product);
@override
_ProductDetailState createState() => _ProductDetailState();
}
class _ProductDetailState extends State<ProductDetail> {
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
CartService _cartService = CartService();
List<Product> _cartItems;
@override
void initState() {
super.initState();
print(this.widget.product.toJson());
_getCartItems();
}
_getCartItems() async {
_cartItems = List<Product>();
var cartItems = await _cartService.getCartItems();
cartItems.forEach((data) {
var product = Product();
product.id = data['productId'] ?? "";
product.name = data['productName'] ?? "";
product.photo = data['productPhoto'] ?? "";
product.price = data['productPrice'] ?? "";
product.discount = data['productDiscount'] ?? "";
product.detail = data['detail'] ?? 'No detail';
product.quantity = data['productQuantity'] ;
setState(() {
_cartItems.add(product);
});
});
}
_addToCart(BuildContext context, Product product) async {
var result = await _cartService.addToCart(product);
if (result > 0) {
_getCartItems();
_showSnackMessage(Text('Item added to cart successfully!',
style: TextStyle(color: Colors.green),));
} else {
_showSnackMessage(
Text('Failed to add to cart!', style: TextStyle(color: Colors.red),));
}
}
_showSnackMessage(message) {
var snackBar = SnackBar(
content: message,
);
_scaffoldKey.currentState.showSnackBar(snackBar);
}
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
appBar: AppBar(
title: Text(this.widget.product.name),
backgroundColor: Colors.redAccent,
actions: <Widget>[
InkWell(
onTap: () {
Navigator.push(context, MaterialPageRoute(
builder: (context) => CartScreen(_cartItems)));
},
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Container(
height: 150,
width: 30,
child: Stack(
children: <Widget>[
IconButton(
iconSize: 30,
icon: Icon(Icons.shopping_cart, color: Colors.white,),
onPressed: () {
},
),
Positioned(
child: Stack(
children: <Widget>[
Icon(Icons.brightness_1, size: 25, color: Colors
.black),
Positioned(
top: 4.0,
right: 8.0,
child: Center(
child: Text(_cartItems.length.toString())),
),
],
),
),
],
),
),
),
)
],
),
body: ListView(
children: <Widget>[
Container(
height: 300,
child: GridTile(
child: Padding(
padding: const EdgeInsets.only(bottom: 40.0),
child: Container(
child: Image.network(this.widget.product.photo),
),
),
footer: Padding(
padding: const EdgeInsets.only(left: 10.0),
child: Container(
child: ListTile(
leading: Text(this.widget.product.name, style: TextStyle(
color: Colors.black,
fontSize: 20,
fontWeight: FontWeight.bold),),
title: Row(
children: <Widget>[
Expanded(child: Text('\$${this.widget.product.price -
this.widget.product.discount}', style: TextStyle(
color: Colors.redAccent,
fontSize: 20,
fontWeight: FontWeight.bold),)),
Expanded(child: Text('\$${this.widget.product.price}',
style: TextStyle(color: Colors.black54,
fontSize: 20,
fontWeight: FontWeight.bold,
decoration: TextDecoration.lineThrough),)),
],
),
),
),
),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
FlatButton(
onPressed: () {
_addToCart(context, this.widget.product);
},
textColor: Colors.redAccent,
child: Row(
children: <Widget>[
Text('Add to cart'),
IconButton(
onPressed: () {
},
icon: Icon(Icons.shopping_cart),
)
],
),
),
IconButton(onPressed: () {
}, icon: Icon(Icons.favorite_border, color: Colors.redAccent,),)
],),
Divider(),
Padding(
padding: const EdgeInsets.only(left: 10.0),
child: ListTile(
title: Text('Product detail', style: TextStyle(fontSize: 20),),
subtitle: Text(this.widget.product.detail),
),
),
],
),
);
}
}
import 'package:ecom_app/repository/repository.dart';
class ProductService {
Repository _repository;
ProductService() {
_repository = Repository();
}
getHotProducts() async {
return await _repository.httpGet('get-all-hot-products');
}
getNewArrivalProducts() async {
return await _repository.httpGet('get-all-new-arrival-products');
}
getProductsByCategoryId(int categoryId) async {
return await _repository.httpGetById(
"get-products-by-category", categoryId);
}
}
import 'dart:convert';
import 'package:ecom_app/models/product.dart';
import 'package:ecom_app/services/product_service.dart';
import 'package:ecom_app/widgets/product_by_category.dart';
import 'package:flutter/material.dart';
class ProductsByCategoryScreen extends StatefulWidget {
final String categoryName;
final int categoryId;
ProductsByCategoryScreen({this.categoryName, this.categoryId});
@override
_ProductsByCategoryScreenState createState() => _ProductsByCategoryScreenState();
}
class _ProductsByCategoryScreenState extends State<ProductsByCategoryScreen> {
ProductService _productService = ProductService();
List<Product> _productListByCategory = List<Product>();
_getProductsByCategory() async {
var products = await _productService.getProductsByCategoryId(this.widget.categoryId);
var _list = json.decode(products.body);
_list['data'].forEach((data){
var model = Product();
model.id = data['id'];
model.name = data['name'];
model.photo = data['photo'];
model.price = data['price'];
model.discount = data['discount'];
model.detail = data['detail'];
setState(() {
_productListByCategory.add(model);
});
});
}
@override
void initState() {
super.initState();
_getProductsByCategory();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.redAccent,
title: Text(this.widget.categoryName),
),
body: Container(
child: GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
itemCount: _productListByCategory.length,
itemBuilder: (context, index){
return ProductByCategory(this._productListByCategory[index]);
},
),
),
);
}
}
import 'dart:convert';
import 'package:ecom_app/models/product.dart';
import 'package:ecom_app/models/user.dart';
import 'package:ecom_app/screens/login_screen.dart';
import 'package:ecom_app/services/user_service.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
class RegistrationScreen extends StatefulWidget {
final List<Product> cartItems;
RegistrationScreen({this.cartItems});
@override
_RegistrationScreenState createState() => _RegistrationScreenState();
}
class _RegistrationScreenState extends State<RegistrationScreen> {
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
final name = TextEditingController();
final email = TextEditingController();
final password = TextEditingController();
_register(BuildContext context, User user) async {
var _userService = UserService();
var registeredUser = await _userService.createUser(user);
var result = json.decode(registeredUser.body);
if (result['result'] == true) {
SharedPreferences _prefs = await SharedPreferences.getInstance();
_prefs.setInt('userId', result['user']['id']);
_prefs.setString('userName', result['user']['name']);
_prefs.setString('userEmail', result['user']['email']);
} else {
_showSnackMessage(Text(
'Failed to register the user!', style: TextStyle(color: Colors.red),));
}
}
_showSnackMessage(message) {
var snackBar = SnackBar(
content: message,
);
_scaffoldKey.currentState.showSnackBar(snackBar);
}
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
backgroundColor: Colors.white,
appBar: AppBar(
backgroundColor: Colors.white,
elevation: 0.0,
actions: <Widget>[
IconButton(
icon: Icon(
Icons.close,
color: Colors.red,
),
onPressed: () {
Navigator.pop(context);
},
),
],
),
body: Padding(
padding: const EdgeInsets.only(top: 100.0),
child: ListView(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(
left: 28.0, top: 14.0, right: 28.0, bottom: 14.0),
child: TextField(
controller: name,
decoration: InputDecoration(
hintText: 'Enter your name', labelText: 'Enter your name'),
),
),
Padding(
padding: const EdgeInsets.only(
left: 28.0, top: 14.0, right: 28.0, bottom: 14.0),
child: TextField(
keyboardType: TextInputType.emailAddress,
controller: email,
decoration: InputDecoration(
hintText: 'Enter your email address',
labelText: 'Enter your email address'),
),
),
Padding(
padding: const EdgeInsets.only(
left: 28.0, top: 14.0, right: 28.0, bottom: 14.0),
child: TextField(
controller: password,
obscureText: true,
decoration:
InputDecoration(hintText: 'Password', labelText: '******'),
),
),
Column(
children: <Widget>[
ButtonTheme(
minWidth: 320.0,
height: 45.0,
child: FlatButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(7.0)),
color: Colors.redAccent,
onPressed: () {
var user = User();
user.name = name.text;
user.email = email.text;
user.password = password.text;
_register(context, user);
},
child:
Text('Register', style: TextStyle(color: Colors.white)),
),
),
Padding(
padding: const EdgeInsets.all(5),
child: FlatButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => LoginScreen()));
},
child: FittedBox(child: Text('Log in to your account')),
),
),
],
),
Padding(
padding: const EdgeInsets.only(
left: 65.0, top: 14.0, right: 65.0, bottom: 14.0),
child: Text(
'By signing up you accept the Terms of Service and Privacy Policy',
textAlign: TextAlign.center,
),
),
],
),
),
);
}
}
import 'package:ecom_app/repository/db_connection.dart';
import 'package:http/http.dart' as http;
import 'package:sqflite/sqflite.dart';
class Repository {
DatabaseConnection _connection;
String _baseUrl = 'http://10.0.2.2:8080/api';
var headers = {
'Content-type': 'application/json',
'Accept': 'application/json',
};
Repository() {
_connection = DatabaseConnection();
}
static Database _database;
Future<Database> get database async {
if (_database != null)
return _database;
_database = await _connection.initDatabase();
return _database;
}
httpGet(String api) async {
return await http.get(_baseUrl + "/" + api);
}
httpGetById(String api, id) async {
return await http.get(_baseUrl + "/" + api + "/" + id.toString());
}
httpPost(String api, data) async {
return await http.post(_baseUrl + "/" + api, body: data);
}
getAllLocal(table) async {
var conn = await database;
return await conn.query(table);
}
saveLocal(table, data) async {
var conn = await database;
return await conn.insert(table, data);
}
updateLocal(table, columnName, data) async {
var conn = await database;
return await conn.update(
table, data, where: '$columnName =?', whereArgs: [data['productId']]);
}
getLocalByCondition(table, columnName, conditionalValue) async {
var conn = await database;
return await conn.rawQuery(
'SELECT * FROM $table WHERE $columnName=?', ['$conditionalValue']);
}
deleteLocalById(table, id) async {
var conn = await database;
return await conn.rawDelete("DELETE FROM $table WHERE id = $id");
}
deleteLocal(table) async {
var conn = await database;
return await conn.rawDelete("DELETE FROM $table");
}
}
view raw repository.dart hosted with ❤ by GitHub
class Shipping {
int id;
String name;
String email;
String address;
toJson() {
return {
'id': id.toString(),
'name': name,
'email': email,
'address': address};
}
}
view raw shipping.dart hosted with ❤ by GitHub
import 'package:ecom_app/models/shipping.dart';import 'package:ecom_app/repository/repository.dart';
class ShippingService {
Repository _repository;
ShippingService() {
_repository = Repository();
}
addShipping(Shipping shipping) async {
return await _repository.httpPost('shipping', shipping.toJson());
}
}
import 'package:ecom_app/repository/repository.dart';
class SliderService {
Repository _repository;
SliderService() {
_repository = Repository();
}
getSliders() async {
return await _repository.httpGet('sliders');
}
}
class User {
int id;
String name;
String email;
String password;
toJson() {
return {
'id': id.toString(),
'name': name.toString(),
'email': email,
'password': password,
};
}
}
view raw user.dart hosted with ❤ by GitHub
import 'package:ecom_app/models/user.dart';
import 'package:ecom_app/repository/repository.dart';
class UserService {
Repository _repository;
UserService() {
_repository = Repository();
}
createUser(User user) async {
return await _repository.httpPost('register', user.toJson());
}
login(User user) async {
return await _repository.httpPost('login', user.toJson());
}
}
Laravel Code
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePasswordResetsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('password_resets', function (Blueprint $table) {
$table->string('email')->index();
$table->string('token');
$table->timestamp('created_at')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('password_resets');
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateFailedJobsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('failed_jobs', function (Blueprint $table) {
$table->id();
$table->string('uuid')->unique();
$table->text('connection');
$table->text('queue');
$table->longText('payload');
$table->longText('exception');
$table->timestamp('failed_at')->useCurrent();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('failed_jobs');
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('name', 60)->unique();
$table->string('icon');
$table->integer('user_id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('categories');
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateOrdersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('orders', function (Blueprint $table) {
$table->id();
$table->string('order_date');
$table->integer('user_id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('orders');
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->double('price');
$table->string('photo');
$table->double('discount');
$table->boolean('is_hot_product');
$table->boolean('is_new_arrival');
$table->integer('category_id');
$table->integer('user_id');
$table->string('detail')->nullable();
$table->timestamps();
});
Schema::table('orders', function (Blueprint $table) {
$table->integer('product_id');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('products');
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateShippingsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('shippings', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email');
$table->string('address');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('shippings');
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateSlidersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('sliders', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('message');
$table->string('image_url');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('sliders');
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use HasFactory;
}
view raw Category.php hosted with ❤ by GitHub
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Http\Resources\CategoryResource;
use App\Models\Category;
use Illuminate\Http\Request;
class CategoryController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return CategoryResource::collection(Category::all());
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class CategoryResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return
[
'id' => $this->id,
'categoryName' => $this->name,
'categoryIcon' => $this->icon
];
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Order extends Model
{
use HasFactory;
}
view raw Order.php hosted with ❤ by GitHub
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Http\Resources\OrderResource;
use App\Models\Order;
use Illuminate\Http\Request;
class OrderController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return OrderResource::collection(Order::all());
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param \App\Models\Order $order
* @return \Illuminate\Http\Response
*/
public function show(Order $order)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Order $order
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Order $order)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Order $order
* @return \Illuminate\Http\Response
*/
public function destroy(Order $order)
{
//
}
}
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class OrderResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return parent::toArray($request);
}
}
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Order;
use Carbon\Carbon;
use Illuminate\Http\Request;
class PaymentController extends Controller
{
public function makePayment(Request $request)
{
try{
$data = $request->input('cartItems');
$cartItems = json_decode($data, true);
$totalAmount = 0.0;
foreach ($cartItems as $cartItem){
$order = new Order();
$order->order_date = Carbon::now()->toDateString();
$order->product_id = $cartItem['productId'];
$order->user_id = $request->input('userId');
$order->quantity = $cartItem['productQuantity'];
$order->amount = ($cartItem['productPrice'] - $cartItem['productDiscount']);
$totalAmount+= $order->amount * $order->quantity;
$order->save();
}
\Stripe\Stripe::setApiKey('sk_test_xqiuBaqJHd9Gl5dmHmYd00Yb00KwGm5pGt');
$token = \Stripe\Token::create([
'card' => [
'number' => $request->input('cardNumber'),
'exp_month' => $request->input('expiryMonth'),
'exp_year' => $request->input('expiryYear'),
'cvc' => $request->input('cvcNumber')
]
]);
$charge = \Stripe\Charge::create([
'amount' => $totalAmount * 100,
'currency' => 'usd',
'source' => $token,
'receipt_email' => $request->input('email'),
]);
return response(['result' => true]);
} catch (\Exception $exception){
return response(['result' => $exception]);
}
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
use HasFactory;
}
view raw Product.php hosted with ❤ by GitHub
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Http\Resources\ProductResource;
use App\Models\Product;
use Illuminate\Http\Request;
class ProductController extends Controller
{
public function getAllHotProducts(){
$hotProducts = Product::where('is_hot_product', 1)->get();
return ProductResource::collection($hotProducts);
}
public function getAllNewArrivalProducts(){
$newArrivalProducts = Product::where('is_new_arrival', 1)->get();
return ProductResource::collection($newArrivalProducts);
}
public function getProductsByCategoryId($categoryId){
$productsByCategory = Product::where('category_id', $categoryId)->get();
return ProductResource::collection($productsByCategory);
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param \App\Models\Product $product
* @return \Illuminate\Http\Response
*/
public function show(Product $product)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Product $product
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Product $product)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Product $product
* @return \Illuminate\Http\Response
*/
public function destroy(Product $product)
{
//
}
}
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class ProductResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return parent::toArray($request);
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Shipping extends Model
{
use HasFactory;
}
view raw Shipping.php hosted with ❤ by GitHub
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Shipping;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
class ShippingController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$shipping = new Shipping();
$shipping->name = $request->input('name');
$shipping->email = $request->input('email');
$shipping->address = $request->input('address');
if($shipping->save()){
return response(['result' => true]);
}
return response(['result' => false]);
}
/**
* Display the specified resource.
*
* @param \App\Models\Shipping $shipping
* @return \Illuminate\Http\Response
*/
public function show(Shipping $shipping)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Shipping $shipping
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Shipping $shipping)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Shipping $shipping
* @return \Illuminate\Http\Response
*/
public function destroy(Shipping $shipping)
{
//
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Slider extends Model
{
use HasFactory;
}
view raw Slider.php hosted with ❤ by GitHub
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Http\Resources\SliderResource;
use App\Models\Slider;
use Illuminate\Http\Request;
class SliderController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return SliderResource::collection(Slider::all());
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param \App\Models\Slider $slider
* @return \Illuminate\Http\Response
*/
public function show(Slider $slider)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Slider $slider
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Slider $slider)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Slider $slider
* @return \Illuminate\Http\Response
*/
public function destroy(Slider $slider)
{
//
}
}
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class SliderResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return parent::toArray($request);
}
}
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
view raw User.php hosted with ❤ by GitHub
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Auth;
class UserController extends Controller
{
public function register(Request $request)
{
$user = new User();
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->password = Hash::make($request->input('password'));
if($user->save()){
if (Auth::attempt(['email' => $request->input('email'), 'password' => $request->input('password')])) {
return response(['result' => true, 'user' => Auth::user()]);
}
}
return response(['result' => false, 'user' => new User()]);
}
public function login(Request $request)
{
if(Auth::attempt(['email' => $request->input('email'), 'password' => $request->input('password')])){
return response(['result' => true, 'user' => Auth::user()]);
}
return response(['result' => false, 'user' => new User()]);
}
}

【VISUAL VB NET】Ping

Posted: 26 Apr 2021 08:58 AM PDT

 Imports System

Imports System.Collections.GenericImports System.ComponentModelImports System.DataImports System.DrawingImports System.LinqImports System.TextImports System.Threading.TasksImports System.Windows.Forms' make sure that using System.Diagnostics; is included Imports System.Diagnostics' make sure that using System.Security.Principal; is included Imports System.Security.Principal ' make sure that using System.Net.NetworkInformation; is included Imports System.Net.NetworkInformation' make sure that using System.Threading; is included for method Sleep Imports System.Threading Public Class Form1 Public Sub New() MyBase.New() InitializeComponent() End Sub Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click For i As Integer = 0 To 3 ' Thread.Sleep(500); Using p As New Ping() ' label1.Text = p.Send("www.google.com").RoundtripTime.ToString() + "ms"; listView1.Items.Add(p.Send("www.google.com").RoundtripTime.ToString() & "ms" & vbLf) End Using Next End SubEnd Class

【PYTHON OPENCV】Image classification OpenCV CNN module AlexNet and caffe pre trained models

Posted: 26 Apr 2021 08:54 AM PDT

 


""" Image classification using OpenCV CNN module using AlexNet and caffe pre-trained models (bvlc_alexnet.caffemodel not included because exceeds GitHub's file size limit of 100.00 MB) bvlc_alexnet.prototxt: https://github.com/opencv/opencv_extra/blob/master/testdata/dnn/bvlc_alexnet.prototxt bvlc_alexnet.caffemodel: http://dl.caffe.berkeleyvision.org/bvlc_alexnet.caffemodel """ # Import required packages:import cv2import numpy as npfrom matplotlib import pyplot as plt def show_img_with_matplotlib(color_img, title, pos): """Shows an image using matplotlib capabilities""" img_RGB = color_img[:, :, ::-1] ax = plt.subplot(1, 1, pos) plt.imshow(img_RGB) plt.title(title) plt.axis('off') # Load the names of the classes:rows = open('synset_words.txt').read().strip().split('\n')classes = [r[r.find(' ') + 1:].split(',')[0] for r in rows] # Load the serialized caffe model from disk:net = cv2.dnn.readNetFromCaffe("bvlc_alexnet.prototxt", "bvlc_alexnet.caffemodel") # Load input image:image = cv2.imread("church.jpg") # Create the blob with a size of (227,227), mean subtraction values (104, 117, 123)blob = cv2.dnn.blobFromImage(image, 1, (227, 227), (104, 117, 123))print(blob.shape) # Feed the input blob to the network, perform inference and get the output:net.setInput(blob)preds = net.forward() # Get inference time:t, _ = net.getPerfProfile()print('Inference time: %.2f ms' % (t * 1000.0 / cv2.getTickFrequency())) # Get the 10 indexes with the highest probability (in descending order)# This way, the index with the highest prob (top prediction) will be the first:indexes = np.argsort(preds[0])[::-1][:10] # We draw on the image the class and probability associated with the top prediction:text = "label: {}\nprobability: {:.2f}%".format(classes[indexes[0]], preds[0][indexes[0]] * 100)y0, dy = 30, 30for i, line in enumerate(text.split('\n')): y = y0 + i * dy cv2.putText(image, line, (5, y), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 255), 2) # Print top 10 prediction:for (index, idx) in enumerate(indexes): print("{}. label: {}, probability: {:.10}".format(index + 1, classes[idx], preds[0][idx])) # Create the dimensions of the figure and set title:fig = plt.figure(figsize=(10, 6))plt.suptitle("Image classification with OpenCV using AlexNet and caffe pre-trained models", fontsize=14, fontweight='bold')fig.patch.set_facecolor('silver') # Show the output image:show_img_with_matplotlib(image, "AlexNet and caffe pre-trained models", 1) # Show the Figure:plt.show()

【GAMEMAKER】Toggle quest complete

Posted: 26 Apr 2021 08:46 AM PDT

 Information about object: obj_example

Sprite:
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Create Event:
execute code:

///Set Up Data
quest[1,1]="Collect 100 Treasure Chests"; //name of quest
quest[1,2]=false; //whether completed (true) or not completed (false);

quest[2,1]="Defeat All Bosses"; //name of quest
quest[2,2]=true; //whether completed (true) or not completed (false);

quest[3,1]="Puzzle Room"; //name of quest
quest[3,2]=false; //whether completed (true) or not completed (false);

quest[4,1]="Find All Hidden Locations"; //name of quest
quest[4,2]=false; //whether completed (true) or not completed (false);

Step Event:
execute code:

///toggle completion true / false
//for example only
if keyboard_check_pressed(ord('1'))
{
quest[1,2]=!quest[1,2]
}

if keyboard_check_pressed(ord('2'))
{
quest[2,2]=!quest[2,2]
}

if keyboard_check_pressed(ord('3'))
{
quest[3,2]=!quest[3,2]
}

if keyboard_check_pressed(ord('4'))
{
quest[4,2]=!quest[4,2]
}
Draw Event:
execute code:

for (var loop = 1; loop <=4; loop += 1)
{
//set drawing colour based on true/false
if quest[loop,2]
{
draw_set_colour(c_green);
}
else
{
draw_set_colour(c_red);
}
//draw description
draw_text(10,100+(loop*100),quest[loop,1]);
//draw completed or not
if quest[loop,2]
{
draw_text(500,100+(loop*100),"Completed");
}
else
{
draw_text(500,100+(loop*100),"Not Completed");
}

}

draw_set_colour(c_black);
draw_text(10,700,"Press 1 2 3 4 To Toggle");

【VUE.JS】Automatic perspective correction OpenCV.js

Posted: 26 Apr 2021 05:58 AM PDT

<div id="app" class="p-3">
<div v-if="!initialized">
Preparing opencv ... Please wait.
</div>
<div v-else>
<input type="file" accept="image/*" @change="onFileChange">
<div class="pt-3" v-if="imageData">
<img id="image" class="float-left" :src="imageData" v-if="imageData">
<canvas id="outputCanvas"> </canvas>
</div>
<div class="pt-3 clear-both" v-if="imageData">
<button type="button" class="bg-indigo-500 text-indigo-50 p-2 rounded mb-3" @click="transform"> Correct distortion </button>
</div>
</div>
</div>
view raw index.html hosted with ❤ by GitHub
new Vue({
data() {
return {
initialized: false,
imageData: null,
MIN_CONTOURS_SCALE: 20, // Minimum original image ratio
THRESHOLD: 170, // Monochrome threshold
}
},
methods: {
onFileChange(e) {
const files = e.target.files;
if(files.length > 0) {
const file = files[0];
const reader = new FileReader();
reader.onload = (e) => {
this.imageData = e.target.result;
};
reader.readAsDataURL(file);
}
},
// opencv
transform() {
const imageElement = document.querySelector('#image');
const im = cv.imread(imageElement);
const pts = this.getContoursPoints(im);
if(pts) {
const transformedIm = this.getTransformedImage(im, pts);
cv.imshow('outputCanvas', transformedIm);
console.log('Done!');
} else {
console.log('Failed...');
}
im.delete();
},
getContoursPoints(im) {
// Image area
const imRectArea = im.cols * im.rows //
// Grayscale
let im_gray = new cv.Mat();
cv.cvtColor(im, im_gray, cv.COLOR_RGBA2GRAY);
// Threshold
let threshold_im = new cv.Mat();
cv.threshold(im_gray, threshold_im, this.THRESHOLD, 255, cv.THRESH_BINARY);
// Contours
let contours = new cv.MatVector();
let hierarchy = new cv.Mat();
cv.findContours(threshold_im, contours, hierarchy, cv.RETR_CCOMP, cv.CHAIN_APPROX_SIMPLE);
hierarchy.delete();
let pts = null;
let maxCntArea = 0
for (let i = 0; i <contours.size (); ++ i) {
let cnt = contours.get (i);
const cntArea = cv.contourArea (cnt)
const maxRectScale = parseInt (cntArea / imRectArea * 100) // How big is it compared to the original image (%)
if (maxRectScale>= this.MIN_CONTOURS_SCALE) {// Filter by ratio to original image
if (cntArea> maxCntArea) {// Keep larger
let approx = new cv.Mat ();
const epsilon = 0.02 * cv.arcLength (cnt, true)
cv.approxPolyDP (cnt, approx, epsilon, true)
if (approx.size (). height === 4) {// Keep if it is a rectangle
maxCntArea = cntArea;
pts = approx // Coordinates of the rectangle to be cut out (4 points)
}
}
}
}
contours.delete();
im_gray.delete();
threshold_im.delete();
pts.convertTo(pts, cv.CV_32FC2);
return pts;
},
getTransformedImage(im, fromPts) {
let transformedIm = new cv.Mat();
const rows = im.rows;
const cols = im.cols;
let dsize = new cv.Size(cols, rows);
const toPts = cv.matFromArray(4, 1, cv.CV_32FC2, [
cols, 0, 0, 0, 0, rows, cols, rows
]);
const M = cv.getPerspectiveTransform(fromPts, toPts); // Matrix of transformations
cv.warpPerspective(im, transformedIm, M, dsize);
fromPts.delete();
toPts.delete();
return transformedIm;
}
},
mounted() {
cv['onRuntimeInitialized'] = () => {
this.initialized = true;
};
}
}).$mount('#app');
view raw script.js hosted with ❤ by GitHub
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.11/vue.min.js"></script>
<script src="https://docs.opencv.org/4.5.2/opencv.js"></script>
view raw scripts hosted with ❤ by GitHub
<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet" />
view raw styles hosted with ❤ by GitHub

No comments:

Post a Comment