176 lines
4.5 KiB
Dart
Raw Permalink Normal View History

2024-10-18 15:27:46 +04:00
import 'package:flutter/material.dart';
2024-11-26 11:59:33 +04:00
import 'car.dart';
import 'CarEnums.dart';
2024-10-18 15:27:46 +04:00
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
2024-11-26 11:59:33 +04:00
title: 'Cars',
2024-10-18 15:27:46 +04:00
theme: ThemeData(
2024-11-26 11:59:33 +04:00
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
2024-10-18 15:27:46 +04:00
useMaterial3: true,
),
2024-11-26 11:59:33 +04:00
home: const HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Car Showroom'),
),
body: Center(
child: InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => const CarListScreen(),
),
);
},
borderRadius: BorderRadius.circular(30),
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 40, vertical: 20),
decoration: BoxDecoration(
gradient: const LinearGradient(
colors: [Colors.lightBlue, Colors.blue],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
borderRadius: BorderRadius.circular(30),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.2),
blurRadius: 5,
offset: const Offset(2, 2),
),
],
),
child: const Text(
'Show Car List',
style: TextStyle(
fontSize: 28,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
),
),
),
2024-10-18 15:27:46 +04:00
);
}
}
2024-11-26 11:59:33 +04:00
class CarListScreen extends StatefulWidget {
const CarListScreen({super.key});
2024-10-18 15:27:46 +04:00
@override
2024-11-26 11:59:33 +04:00
_CarListScreenState createState() => _CarListScreenState();
2024-10-18 15:27:46 +04:00
}
2024-11-26 11:59:33 +04:00
class _CarListScreenState extends State<CarListScreen> {
late Future<List<Car>> _carsFuture;
@override
void initState() {
super.initState();
_carsFuture = _fetchCars();
}
//Generics
Future<List<Car>> _fetchCars() async {
//Future
await Future.delayed(const Duration(seconds: 2));
final cars = [
Car(
brand: CarBrand.Toyota,
model: CarModel.Corolla,
year: 2021,
fuelType: FuelType.Petrol,
description: 'Норм тачка',
),
Car(
brand: CarBrand.Tesla,
model: CarModel.ModelS,
year: 2023,
fuelType: FuelType.Electric,
description: 'Самая дорогая тачка',
),
Car(
brand: CarBrand.BMW,
model: CarModel.X5,
year: 2022,
fuelType: FuelType.Hybrid,
description: 'Луксури машинка',
),
Car(
brand: CarBrand.Mercedes,
model: CarModel.CClass,
year: 2020,
fuelType: FuelType.Diesel,
description: 'Мощная машинка',
),
];
// Loops
for (var car in cars) {
print(car.printCar());
}
2024-10-18 15:27:46 +04:00
2024-11-26 11:59:33 +04:00
return cars;
2024-10-18 15:27:46 +04:00
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
2024-11-26 11:59:33 +04:00
title: const Text('Car List'),
2024-10-18 15:27:46 +04:00
),
2024-11-26 11:59:33 +04:00
body: FutureBuilder<List<Car>>(
future: _carsFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
} else if (snapshot.hasError) {
return Center(child: Text('Error: ${snapshot.error}'));
} else if (!snapshot.hasData || snapshot.data!.isEmpty) {
return const Center(child: Text('No cars found.'));
} else {
final cars = snapshot.data!;
return ListView.builder(
itemCount: cars.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(
cars[index].brand.toString().split('.').last,
style: const TextStyle(fontSize: 34),
),
subtitle: Text(
'${cars[index].model.toString().split('.').last} - ${cars[index].year}\n${cars[index].description}',
style: const TextStyle(fontSize: 26),
),
);
},
);
}
},
2024-10-18 15:27:46 +04:00
),
2024-11-26 11:59:33 +04:00
);
2024-10-18 15:27:46 +04:00
}
2024-11-26 11:59:33 +04:00
}