Skip to content

Instantly share code, notes, and snippets.

@prakhart111
Created February 4, 2025 08:04
Show Gist options
  • Select an option

  • Save prakhart111/a86cacbb358c6cd8f1d226175e4344a0 to your computer and use it in GitHub Desktop.

Select an option

Save prakhart111/a86cacbb358c6cd8f1d226175e4344a0 to your computer and use it in GitHub Desktop.
Snippet created via Next.js API
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
void main() {
runApp(const MyApp());
}
class Product {
final String id;
final String name;
final double price;
final String imageUrl;
final String description;
Product({
required this.id,
required this.name,
required this.price,
required this.imageUrl,
required this.description,
});
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'E-Commerce App',
theme: ThemeData(
primarySwatch: Colors.blue,
fontFamily: 'Roboto',
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final List<Product> products = [
Product(
id: '1',
name: 'Wireless Headphones',
price: 99.99,
imageUrl: 'https://placehold.co/300x200',
description: 'High quality wireless headphones with noise cancellation',
),
Product(
id: '2',
name: 'Smartphone',
price: 699.99,
imageUrl: 'https://placehold.co/300x200',
description: 'Latest smartphone with amazing camera',
),
Product(
id: '3',
name: 'Laptop',
price: 1299.99,
imageUrl: 'https://placehold.co/300x200',
description: 'Powerful laptop for work and gaming',
),
Product(
id: '4',
name: 'Smartwatch',
price: 199.99,
imageUrl: 'https://placehold.co/300x200',
description: 'Track your fitness and stay connected',
),
];
int cartCount = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('E-Commerce Store'),
actions: [
Stack(
children: [
IconButton(
icon: const Icon(Icons.shopping_cart),
onPressed: () {},
),
if (cartCount > 0)
Positioned(
right: 8,
top: 8,
child: Container(
padding: const EdgeInsets.all(2),
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(10),
),
constraints: const BoxConstraints(
minWidth: 16,
minHeight: 16,
),
child: Text(
'$cartCount',
style: const TextStyle(
color: Colors.white,
fontSize: 10,
),
textAlign: TextAlign.center,
),
),
),
],
),
],
),
body: LayoutBuilder(
builder: (context, constraints) {
return GridView.builder(
padding: const EdgeInsets.all(16.0),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: constraints.maxWidth > 1200
? 4
: constraints.maxWidth > 800
? 3
: constraints.maxWidth > 600
? 2
: 1,
childAspectRatio: 0.75,
crossAxisSpacing: 16,
mainAxisSpacing: 16,
),
itemCount: products.length,
itemBuilder: (context, index) {
return Card(
elevation: 4,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
child: Image.network(
products[index].imageUrl,
fit: BoxFit.cover,
width: double.infinity,
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
products[index].name,
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 4),
Text(
'\$${products[index].price.toStringAsFixed(2)}',
style: const TextStyle(
fontSize: 14,
color: Colors.green,
),
),
const SizedBox(height: 8),
Text(
products[index].description,
style: const TextStyle(
fontSize: 12,
color: Colors.grey,
),
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
const SizedBox(height: 8),
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: () {
setState(() {
cartCount++;
});
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('${products[index].name} added to cart'),
duration: const Duration(seconds: 1),
),
);
},
child: const Text('Add to Cart'),
),
),
],
),
),
],
),
);
},
);
},
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment