lab2
This commit is contained in:
parent
8fe30565a1
commit
a5d526899d
25
lib/CarEnums.dart
Normal file
25
lib/CarEnums.dart
Normal 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
45
lib/car.dart
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
185
lib/main.dart
185
lib/main.dart
@ -1,6 +1,6 @@
|
|||||||
import 'dart:math';
|
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'car.dart';
|
||||||
|
import 'CarEnums.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
runApp(const MyApp());
|
runApp(const MyApp());
|
||||||
@ -12,67 +12,164 @@ class MyApp extends StatelessWidget {
|
|||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return MaterialApp(
|
return MaterialApp(
|
||||||
title: 'Flutter Demo',
|
title: 'Cars',
|
||||||
theme: ThemeData(
|
theme: ThemeData(
|
||||||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepOrange),
|
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
|
||||||
useMaterial3: true,
|
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
|
@override
|
||||||
State<MyHomePage> createState() => _MyHomePageState();
|
_CarListScreenState createState() => _CarListScreenState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class _MyHomePageState extends State<MyHomePage> {
|
class _CarListScreenState extends State<CarListScreen> {
|
||||||
int _counter = 0;
|
late Future<List<Car>> _carsFuture;
|
||||||
Color _color = Colors.orangeAccent;
|
|
||||||
|
|
||||||
void _incrementCounter() {
|
@override
|
||||||
setState(() {
|
void initState() {
|
||||||
_counter++;
|
super.initState();
|
||||||
_color = Color((Random().nextDouble() * 0xFFFFFF).toInt()).withOpacity(1.0);
|
_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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
backgroundColor: _color,
|
title: const Text('Car List'),
|
||||||
title: Text(widget.title),
|
|
||||||
),
|
),
|
||||||
body: Center(
|
body: FutureBuilder<List<Car>>(
|
||||||
child: Column(
|
future: _carsFuture,
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
builder: (context, snapshot) {
|
||||||
children: <Widget>[
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||||
const Text(
|
return const Center(child: CircularProgressIndicator());
|
||||||
'You have pushed the button this many times:',
|
} else if (snapshot.hasError) {
|
||||||
),
|
return Center(child: Text('Error: ${snapshot.error}'));
|
||||||
Text(
|
} else if (!snapshot.hasData || snapshot.data!.isEmpty) {
|
||||||
'$_counter',
|
return const Center(child: Text('No cars found.'));
|
||||||
style: Theme.of(context).textTheme.headlineMedium,
|
} else {
|
||||||
),
|
final cars = snapshot.data!;
|
||||||
if (_counter > 6)
|
return ListView.builder(
|
||||||
Text(
|
itemCount: cars.length,
|
||||||
'You pipipupu',
|
itemBuilder: (context, index) {
|
||||||
style: Theme.of(context).textTheme.headlineMedium,
|
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),
|
|
||||||
), );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user