Created
April 27, 2024 05:25
-
-
Save oluyoung/97ea89e91ae96ee0b5cab3b6a37d4d81 to your computer and use it in GitHub Desktop.
Generated code from pixels2flutter.dev
This file contains 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 'package:flutter/material.dart'; | |
import 'dart:convert'; | |
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 Future<List<Part>> partsFuture; | |
Set<int> cartItems = {}; | |
@override | |
void initState() { | |
super.initState(); | |
partsFuture = fetchParts(); | |
} | |
Future<List<Part>> fetchParts() async { | |
final response = await http.get(Uri.parse('http://localhost:3000')); | |
if (response.statusCode == 200) { | |
List<dynamic> partsJson = json.decode(response.body); | |
return partsJson.map((json) => Part.fromJson(json)).toList(); | |
} else { | |
throw Exception('Failed to load parts'); | |
} | |
} | |
void toggleCart(int id) { | |
setState(() { | |
if (cartItems.contains(id)) { | |
cartItems.remove(id); | |
} else { | |
cartItems.add(id); | |
} | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('2013 Hyundai Verna i3 Engine'), | |
actions: [ | |
Stack( | |
alignment: Alignment.topRight, | |
children: [ | |
IconButton( | |
icon: Icon(Icons.shopping_cart), | |
onPressed: () { | |
// Navigate to cart screen | |
}, | |
), | |
if (cartItems.isNotEmpty) | |
CircleAvatar( | |
radius: 10.0, | |
backgroundColor: Colors.red, | |
child: Text( | |
cartItems.length.toString(), | |
style: TextStyle(fontSize: 12.0, color: Colors.white), | |
), | |
), | |
], | |
), | |
], | |
), | |
body: Column( | |
children: [ | |
Padding( | |
padding: const EdgeInsets.all(16.0), | |
child: TextField( | |
decoration: InputDecoration( | |
hintText: 'Search car parts', | |
prefixIcon: Icon(Icons.search), | |
), | |
), | |
), | |
Expanded( | |
child: FutureBuilder<List<Part>>( | |
future: partsFuture, | |
builder: (context, snapshot) { | |
if (snapshot.connectionState == ConnectionState.waiting) { | |
return Center(child: CircularProgressIndicator()); | |
} else if (snapshot.hasError) { | |
return Center(child: Text('Error: ${snapshot.error}')); | |
} else if (snapshot.hasData) { | |
return GridView.builder( | |
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( | |
crossAxisCount: 2, | |
childAspectRatio: 0.8, | |
), | |
itemCount: snapshot.data!.length, | |
itemBuilder: (context, index) { | |
Part part = snapshot.data![index]; | |
return Card( | |
child: Column( | |
crossAxisAlignment: CrossAxisAlignment.stretch, | |
children: [ | |
Expanded( | |
child: Image.network( | |
part.imageUrl, | |
fit: BoxFit.contain, | |
), | |
), | |
Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Column( | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: [ | |
Text( | |
part.name, | |
style: TextStyle( | |
fontWeight: FontWeight.bold, | |
), | |
), | |
Text('\$${part.price.toStringAsFixed(2)}'), | |
ElevatedButton( | |
onPressed: () => toggleCart(part.id), | |
child: Text(cartItems.contains(part.id) | |
? 'Remove' | |
: 'Add to cart'), | |
style: ElevatedButton.styleFrom( | |
primary: cartItems.contains(part.id) | |
? Colors.transparent | |
: Colors.green, | |
onPrimary: cartItems.contains(part.id) | |
? Colors.red | |
: Colors.white, | |
), | |
), | |
], | |
), | |
), | |
], | |
), | |
); | |
}, | |
); | |
} else { | |
return Center(child: Text('No parts found')); | |
} | |
}, | |
), | |
), | |
], | |
), | |
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', | |
), | |
], | |
selectedItemColor: Colors.amber[800], | |
), | |
); | |
} | |
} | |
class Part { | |
final int id; | |
final String name; | |
final double price; | |
final String imageUrl; | |
Part({ | |
required this.id, | |
required this.name, | |
required this.price, | |
required this.imageUrl, | |
}); | |
factory Part.fromJson(Map<String, dynamic> json) { | |
return Part( | |
id: json['id'], | |
name: json['name'], | |
price: json['price'].toDouble(), | |
imageUrl: json['imageUrl'], | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment