154 lines
3.8 KiB
Dart
154 lines
3.8 KiB
Dart
import 'package:flutter/material.dart';
|
||
import 'dart:math';
|
||
|
||
void main() {
|
||
runApp(const MyApp());
|
||
}
|
||
|
||
class MyApp extends StatelessWidget {
|
||
const MyApp({super.key});
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
return MaterialApp(
|
||
title: 'Flutter Demo',
|
||
debugShowCheckedModeBanner: false,
|
||
theme: ThemeData(
|
||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.cyan),
|
||
useMaterial3: true,
|
||
),
|
||
home: const MyHomePage(title: 'Чернышев Георгий Янович'),
|
||
);
|
||
}
|
||
}
|
||
|
||
class MyHomePage extends StatefulWidget {
|
||
const MyHomePage({super.key, required this.title});
|
||
|
||
final String title;
|
||
|
||
@override
|
||
State<MyHomePage> createState() => _MyHomePageState();
|
||
}
|
||
|
||
class _MyHomePageState extends State<MyHomePage> {
|
||
int _counter = 0;
|
||
|
||
void _incrementCounter() {
|
||
setState(() {
|
||
_counter++;
|
||
});
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
return Scaffold(
|
||
appBar: AppBar(
|
||
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
||
title: Text(widget.title),
|
||
),
|
||
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,
|
||
),
|
||
],
|
||
),
|
||
),
|
||
floatingActionButton: FloatingActionButton(
|
||
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<void> 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<GameType, String> GameTypeNames = {
|
||
GameType.FMV: "FMV",
|
||
GameType.Shooter: "Шутер",
|
||
GameType.Strategy: "Стратегия",
|
||
GameType.Horror: "Хоррор"
|
||
};
|
||
|
||
void availableTypesRus() => printMapValues(GameTypeNames);
|
||
|
||
//цикл (loop) + generic (хз, куда ещё их можно)
|
||
void printMapValues<T, U>(Map<T, U> 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 'Хоррор';
|
||
}
|
||
}
|
||
} |