From a5d526899dc7b180fda9b8b09a4eea08707d5c38 Mon Sep 17 00:00:00 2001 From: goldfest228 Date: Tue, 26 Nov 2024 11:59:33 +0400 Subject: [PATCH] lab2 --- lib/CarEnums.dart | 25 +++++++ lib/car.dart | 45 +++++++++++ lib/main.dart | 185 +++++++++++++++++++++++++++++++++++----------- 3 files changed, 211 insertions(+), 44 deletions(-) create mode 100644 lib/CarEnums.dart create mode 100644 lib/car.dart diff --git a/lib/CarEnums.dart b/lib/CarEnums.dart new file mode 100644 index 0000000..4f11ff0 --- /dev/null +++ b/lib/CarEnums.dart @@ -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, +} diff --git a/lib/car.dart b/lib/car.dart new file mode 100644 index 0000000..46de1f1 --- /dev/null +++ b/lib/car.dart @@ -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; + } +} diff --git a/lib/main.dart b/lib/main.dart index ec20be6..c67f38f 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -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 createState() => _MyHomePageState(); + _CarListScreenState createState() => _CarListScreenState(); } -class _MyHomePageState extends State { - int _counter = 0; - Color _color = Colors.orangeAccent; +class _CarListScreenState extends State { + late Future> _carsFuture; - void _incrementCounter() { - setState(() { - _counter++; - _color = Color((Random().nextDouble() * 0xFFFFFF).toInt()).withOpacity(1.0); - }); + @override + void initState() { + super.initState(); + _carsFuture = _fetchCars(); + } + + //Generics + Future> _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: [ - 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>( + 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), - ), ); + ); } -} \ No newline at end of file +}