From 83178515c5897c5dee52ee02b1b4812bd3636893 Mon Sep 17 00:00:00 2001 From: ujijrujijr Date: Wed, 2 Oct 2024 22:44:53 +0400 Subject: [PATCH] lab2 --- lib/main.dart | 103 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 93 insertions(+), 10 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index de18257..612665b 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'dart:math'; void main() { runApp(const MyApp()); @@ -7,7 +8,7 @@ void main() { class MyApp extends StatelessWidget { const MyApp({super.key}); - @override + @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', @@ -24,8 +25,6 @@ class MyApp extends StatelessWidget { class MyHomePage extends StatefulWidget { const MyHomePage({super.key, required this.title}); - - final String title; @override @@ -37,20 +36,20 @@ class _MyHomePageState extends State { void _incrementCounter() { setState(() { - _counter++; + _counter++; }); } @override Widget build(BuildContext context) { - return Scaffold( + return Scaffold( appBar: AppBar( - backgroundColor: Theme.of(context).colorScheme.inversePrimary, - title: Text(widget.title), + backgroundColor: Theme.of(context).colorScheme.inversePrimary, + title: Text(widget.title), ), body: Center( - child: Column( - mainAxisAlignment: MainAxisAlignment.center, + child: Column( + mainAxisAlignment: MainAxisAlignment.center, children: [ const Text( 'You have pushed the button this many times:', @@ -66,6 +65,90 @@ class _MyHomePageState extends State { onPressed: _incrementCounter, tooltip: 'Increment', child: const Icon(Icons.add), - ), ); + ), + ); } } + +class Game extends Object { + final String name; + final int price; + final GameType type; //жанр игры + final String? description; + + @override + bool operator ==(Object other) { + if (other is Game) { + return this.name == other.name; + } + return false; + } + + //стандартный конструктор с именованными параметрами + Game( + {required this.name, + required this.price, + required this.type, + this.description}); + + //именованный конструктор с примером игры + Game.exampleLateShift() + : name = 'Late Shift', + price = 200, + type = GameType.FMV, + description = null; + + void printInfo() { + print(''' +Игра: $name +Жанр: ${GameTypeNames[type]} +Цена: $price руб. '''); + if (description != null) { + print('Описание: $description'); + } + } + +//асинхронный метод - иммитация скачивания + Future download() async { + print('Начало загрузки игры $name'); + var rand = Random(); + int time = rand.nextInt(10000) + 100; + //асинхронность (Future), анонимная функция (Anonymous function) + Future.delayed( + Duration(milliseconds: time), () => print("Игра $name загружена")); + } +} + +enum GameType { FMV, Shooter, Strategy, Horror } + +//Названия жанров на русском +const Map GameTypeNames = { + GameType.FMV: "FMV", + GameType.Shooter: "Шутер", + GameType.Strategy: "Стратегия", + GameType.Horror: "Хоррор" +}; + +void availableTypesRus() => printMapValues(GameTypeNames); + +//цикл (loop) + generic (хз, куда ещё их можно) +void printMapValues(Map map) { + for (var val in map.values) { + print(val); + } +} + +extension GameTypeRus on GameType { + String get rusName { + switch(this) { + case GameType.FMV: + return 'FMV'; + case GameType.Shooter: + return 'Шутер'; + case GameType.Strategy: + return 'Стратегия'; + case GameType.Horror: + return 'Хоррор'; + } + } +} \ No newline at end of file