This commit is contained in:
goldfest228 2024-11-26 11:59:33 +04:00
parent 8fe30565a1
commit a5d526899d
3 changed files with 211 additions and 44 deletions

25
lib/CarEnums.dart Normal file
View File

@ -0,0 +1,25 @@
// Enums
enum CarBrand {
Toyota,
Ford,
BMW,
Mercedes,
Honda,
Tesla,
}
enum CarModel {
Corolla,
Mustang,
X5,
CClass,
Civic,
ModelS,
}
enum FuelType {
Petrol,
Diesel,
Electric,
Hybrid,
}

45
lib/car.dart Normal file
View File

@ -0,0 +1,45 @@
import 'CarEnums.dart';
// Classes
class Car {
final CarBrand brand;
final CarModel model;
final int year;
final FuelType fuelType;
final String description;
Car({
required this.brand,
required this.model,
required this.year,
required this.fuelType,
required this.description,
});
// Methods
String printCar() {
return 'Brand: ${brand.toStringName()}, Model: ${model.toStringName()}, '
'Year: $year, Fuel Type: ${fuelType.toStringName()}, Description: $description';
}
}
// Extension for CarBrand
extension CarBrandExtension on CarBrand {
String toStringName() {
return toString().split('.').last;
}
}
// Extension for CarModel
extension CarModelExtension on CarModel {
String toStringName() {
return toString().split('.').last;
}
}
// Extension for FuelType
extension FuelTypeExtension on FuelType {
String toStringName() {
return toString().split('.').last;
}
}

View File

@ -1,6 +1,6 @@
import 'dart:math';
import 'package:flutter/material.dart';
import 'car.dart';
import 'CarEnums.dart';
void main() {
runApp(const MyApp());
@ -12,67 +12,164 @@ class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
title: 'Cars',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepOrange),
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
useMaterial3: true,
),
home: const MyHomePage(title: 'Лобашов Иван Дмитриевич'),
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,
),
),
),
),
),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
class CarListScreen extends StatefulWidget {
const CarListScreen({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
_CarListScreenState createState() => _CarListScreenState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
Color _color = Colors.orangeAccent;
class _CarListScreenState extends State<CarListScreen> {
late Future<List<Car>> _carsFuture;
void _incrementCounter() {
setState(() {
_counter++;
_color = Color((Random().nextDouble() * 0xFFFFFF).toInt()).withOpacity(1.0);
});
@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());
}
return cars;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: _color,
title: Text(widget.title),
title: const Text('Car List'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
if (_counter > 6)
Text(
'You pipipupu',
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
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),
),
);
},
);
}
},
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
backgroundColor: _color,
tooltip: 'Increment',
child: const Icon(Icons.add),
), );
);
}
}
}