Loading repository data…
Loading repository data…
Ismail-Mouyahada / repository
Creating a mobile e-commerce app with Dart (using Flutter for the frontend) and Laravel for the backend API involves several steps. Here's a detailed guide to help you get started.
Creating a mobile e-commerce app with Dart (using Flutter for the frontend) and Laravel for the backend API involves several steps. Here's a detailed guide to help you get started.
Install Laravel
Create a New Laravel Project
composer create-project --prefer-dist laravel/laravel delizio-backend
cd delizio-backend
Setup Database Configuration
.env file with your database credentials.DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=delizio
DB_USERNAME=root
DB_PASSWORD=secret
Run Migrations
php artisan migrate
Create Models and Migrations
php artisan make:model Product -m
php artisan make:model Order -m
php artisan make:model User -m
Define Database Schema
database/migrations.Example for create_products_table.php:
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->text('description');
$table->decimal('price', 8, 2);
$table->integer('stock');
$table->timestamps();
});
}
Run Migrations
php artisan migrate
Create API Controllers
php artisan make:controller API/ProductController
php artisan make:controller API/OrderController
php artisan make:controller API/UserController
Define Routes
routes/api.php.use App\Http\Controllers\API\ProductController;
use App\Http\Controllers\API\OrderController;
use App\Http\Controllers\API\UserController;
Route::middleware('auth:sanctum')->group(function () {
Route::get('/products', [ProductController::class, 'index']);
Route::get('/products/{id}', [ProductController::class, 'show']);
Route::post('/orders', [OrderController::class, 'store']);
Route::get('/orders/{id}', [OrderController::class, 'show']);
});
Route::post('/register', [UserController::class, 'register']);
Route::post('/login', [UserController::class, 'login']);
Implement Controller Methods
ProductController.php:namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use App\Models\Product;
use Illuminate\Http\Request;
class ProductController extends Controller
{
public function index()
{
return Product::all();
}
public function show($id)
{
return Product::findOrFail($id);
}
}
Install Flutter
Create a New Flutter Project
flutter create delizio_app
cd delizio_app
Add Dependencies
pubspec.yaml to include necessary packages.dependencies:
flutter:
sdk: flutter
http: ^0.13.3
provider: ^5.0.0
Setup Project Structure
Create Models
product.dart:class Product {
final int id;
final String name;
final String description;
final double price;
final int stock;
Product({required this.id, required this.name, required this.description, required this.price, required this.stock});
factory Product.fromJson(Map<String, dynamic> json) {
return Product(
id: json['id'],
name: json['name'],
description: json['description'],
price: json['price'],
stock: json['stock'],
);
}
}
Create Providers
product_provider.dart:import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import '../models/product.dart';
class ProductProvider with ChangeNotifier {
List<Product> _products = [];
List<Product> get products => _products;
Future<void> fetchProducts() async {
final response = await http.get(Uri.parse('http://localhost:8000/api/products'));
if (response.statusCode == 200) {
List jsonResponse = json.decode(response.body);
_products = jsonResponse.map((product) => Product.fromJson(product)).toList();
notifyListeners();
} else {
throw Exception('Failed to load products');
}
}
}
Create Screens
product_list_screen.dart:import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../providers/product_provider.dart';
class ProductListScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Products'),
),
body: FutureBuilder(
future: Provider.of<ProductProvider>(context, listen: false).fetchProducts(),
builder: (ctx, snapshot) => snapshot.connectionState == ConnectionState.waiting
? Center(child: CircularProgressIndicator())
: Consumer<ProductProvider>(
builder: (ctx, productProvider, _) => ListView.builder(
itemCount: productProvider.products.length,
itemBuilder: (ctx, i) => ListTile(
title: Text(productProvider.products[i].name),
subtitle: Text('\$${productProvider.products[i].price}'),
),
),
),
),
);
}
}
Add Routes
main.dart:import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import './screens/product_list_screen.dart';
import './providers/product_provider.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (_) => ProductProvider()),
],
child: MaterialApp(
title: 'Delizio',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: ProductListScreen(),
),
);
}
}
Improve UI/UX
Testing
Run Laravel Server
php artisan serve
Run Flutter App
flutter run
Now you have a basic e-commerce mobile app with a Flutter frontend and a Laravel backend. Expand the app by adding more features, refining the UI, and ensuring a seamless user experience.