Created
April 27, 2024 07:39
-
-
Save oluyoung/144d011269dc92265c318b26f0deb63f to your computer and use it in GitHub Desktop.
Generated code from pixels2flutter.dev
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
import 'package:flutter/material.dart'; | |
import 'package:http/http.dart' as http; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Auto Parts Dashboard', | |
theme: ThemeData( | |
primarySwatch: Colors.green, | |
), | |
home: DashboardScreen(), | |
debugShowCheckedModeBanner: false, | |
); | |
} | |
} | |
class DashboardScreen extends StatefulWidget { | |
@override | |
_DashboardScreenState createState() => _DashboardScreenState(); | |
} | |
class _DashboardScreenState extends State<DashboardScreen> { | |
late List<dynamic> products; | |
late Set<int> cartItems; | |
late int cartCount; | |
@override | |
void initState() { | |
super.initState(); | |
cartItems = {}; | |
cartCount = 0; | |
fetchProducts(); | |
} | |
Future<void> fetchProducts() async { | |
final response = await http.get(Uri.parse('http://192.168.1.13:3000/api/v1/public/products')); | |
if (response.statusCode == 200) { | |
setState(() { | |
products = json.decode(response.body); | |
}); | |
} else { | |
throw Exception('Failed to load products'); | |
} | |
} | |
void addToCart(int productId) { | |
setState(() { | |
if (cartItems.contains(productId)) { | |
cartItems.remove(productId); | |
} else { | |
cartItems.add(productId); | |
} | |
cartCount = cartItems.length; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('${state.vehicle.year} ${state.vehicle.brand} ${state.vehicle.model} ${state.vehicle.trim}'), | |
actions: [ | |
Stack( | |
alignment: Alignment.center, | |
children: [ | |
IconButton( | |
icon: Icon(Icons.shopping_cart), | |
onPressed: () { | |
// Navigate to cart screen | |
}, | |
), | |
if (cartCount > 0) | |
Positioned( | |
right: 8, | |
top: 8, | |
child: Container( | |
padding: EdgeInsets.all(2), | |
decoration: BoxDecoration( | |
color: Colors.red, | |
borderRadius: BorderRadius.circular(10), | |
), | |
constraints: BoxConstraints( | |
minWidth: 16, | |
minHeight: 16, | |
), | |
child: Text( | |
'$cartCount', | |
style: TextStyle( | |
color: Colors.white, | |
fontSize: 10, | |
), | |
textAlign: TextAlign.center, | |
), | |
), | |
), | |
], | |
), | |
], | |
), | |
body: Column( | |
children: [ | |
Padding( | |
padding: const EdgeInsets.all(16.0), | |
child: TextField( | |
decoration: InputDecoration( | |
hintText: 'Search car parts', | |
prefixIcon: Icon(Icons.search), | |
), | |
onChanged: (value) { | |
// Implement search functionality | |
}, | |
), | |
), | |
Expanded( | |
child: GridView.builder( | |
padding: EdgeInsets.all(16), | |
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( | |
crossAxisCount: 2, | |
crossAxisSpacing: 16, | |
mainAxisSpacing: 16, | |
childAspectRatio: 0.8, | |
), | |
itemCount: products.length, | |
itemBuilder: (context, index) { | |
final product = products[index]; | |
return Card( | |
child: Column( | |
crossAxisAlignment: CrossAxisAlignment.stretch, | |
children: [ | |
Expanded( | |
child: Image.network( | |
product['image'], | |
fit: BoxFit.contain, | |
), | |
), | |
Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Column( | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: [ | |
Text( | |
product['name'], | |
style: TextStyle(fontWeight: FontWeight.bold), | |
), | |
Text('\$${product['price']}'), | |
if (product['inStock'] > 0) | |
Text('In stock: ${product['inStock']}'), | |
if (product['inStock'] <= 0) | |
Text('Out of stock', style: TextStyle(color: Colors.red)), | |
], | |
), | |
), | |
FlatButton( | |
color: cartItems.contains(product['id']) ? Colors.transparent : Colors.green, | |
textColor: cartItems.contains(product['id']) ? Colors.red : Colors.white, | |
onPressed: () { | |
addToCart(product['id']); | |
}, | |
child: Text(cartItems.contains(product['id']) ? 'Remove' : 'Add to cart'), | |
), | |
], | |
), | |
); | |
}, | |
), | |
), | |
], | |
), | |
bottomNavigationBar: BottomNavigationBar( | |
items: const <BottomNavigationBarItem>[ | |
BottomNavigationBarItem( | |
icon: Icon(Icons.home), | |
label: 'Home', | |
), | |
BottomNavigationBarItem( | |
icon: Icon(Icons.grid_view), | |
label: 'Categories', | |
), | |
BottomNavigationBarItem( | |
icon: Icon(Icons.history), | |
label: 'Order History', | |
), | |
BottomNavigationBarItem( | |
icon: Icon(Icons.person), | |
label: 'Account', | |
), | |
], | |
), | |
); | |
} | |
} | |
// Dummy state class to simulate vehicle information | |
class VehicleState { | |
final int year = 2013; | |
final String brand = 'Hyundai'; | |
final String model = 'Verna'; | |
final String trim = 'i3'; | |
} | |
final state = VehicleState(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment