This commit is contained in:
panazoid 2024-11-13 04:43:37 +04:00
parent 6f516c08f7
commit 0bad8b5a8c
3 changed files with 162 additions and 35 deletions

18
lib/VideoCards.dart Normal file
View File

@ -0,0 +1,18 @@
import 'dart:async';
import 'main.dart';
enum VideoCardsCategory {
Nvidia,
Asus,
Msi,
AMD,
Palit,
}
class VideoCards{
final String videomemory;
final double price;
final VideoCardsCategory category;
VideoCards(this.videomemory, this.price, this.category);
}

View File

@ -0,0 +1,23 @@
import 'VideoCards.dart';
class VideoCardsManager {
final List<VideoCards> _videocards = [];
void addVideoCards(VideoCards videocards) {
_videocards.add(videocards);
}
List<VideoCards> filterVideoCardsByCategory(VideoCardsCategory category) {
return _videocards.where((videocards) => videocards.category == category).toList();
}
double calculateTotalAmount() {
return _videocards.fold(0.0, (sum,videocards) => sum + videocards.price);
}
Future<void> clearVideoCards() async {
await Future.delayed(Duration(seconds: 3));
_videocards.clear();
print('Список чист!');
}
}

View File

@ -1,3 +1,7 @@
import 'dart:math';
import 'dart:async';
import 'VideoCards.dart';
import 'VideoCardsManager.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
void main() { void main() {
@ -11,32 +15,60 @@ class MyApp extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return MaterialApp(
title: 'Flutter Demo', title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData( theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), colorScheme: ColorScheme.fromSeed(seedColor: Colors.teal),
useMaterial3: true, useMaterial3: true,
), ),
home: const MyHomePage(title: 'Batsylev Vladislav Alexeyevich'), home: const MyHomePage(title: 'Бацылев Владислав Алексеевич'),
); );
} }
} }
class MyHomePage extends StatefulWidget { class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title}); const MyHomePage({super.key, required this.title});
final String title; final String title;
@override @override
State<MyHomePage> createState() => _MyHomePageState(); State<MyHomePage> createState() => _MyHomePageState();
} }
class _MyHomePageState extends State<MyHomePage> { extension VideoCardsExtensions on VideoCards {
int _counter = 0; String get formattedPrice => '${price.toStringAsFixed(2)}';
}
class _MyHomePageState extends State<MyHomePage> {
Color _color = Colors.grey;
final VideoCardsManager _videoCardsManager = VideoCardsManager();
bool _isClearing = false;
int counter = 2;
void _addRandomVideocards() {
final random = Random();
final videomemory = '$counter Gb';
final price = random.nextDouble() * 15000;
final category = VideoCardsCategory.values[random.nextInt(
VideoCardsCategory.values.length)];
final videocards = VideoCards(videomemory, price, category);
_videoCardsManager.addVideoCards(videocards);
counter = counter + 2;
void _incrementCounter() {
setState(() { setState(() {
_counter++; switch (category) {
case VideoCardsCategory.Nvidia:
_color = Colors.green;
case VideoCardsCategory.Asus:
_color = Colors.blue;
case VideoCardsCategory.Msi:
_color = Colors.red;
case VideoCardsCategory.AMD:
_color = Colors.redAccent;
case VideoCardsCategory.Palit:
_color = Colors.brown;
}
}); });
} }
@ -44,7 +76,7 @@ class _MyHomePageState extends State<MyHomePage> {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary, backgroundColor: _color,
title: Text(widget.title), title: Text(widget.title),
), ),
body: Center( body: Center(
@ -52,19 +84,73 @@ class _MyHomePageState extends State<MyHomePage> {
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[ children: <Widget>[
const Text( const Text(
'You have pushed the button this many times:', 'Характеристики Видеокарт',
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
), ),
SizedBox (height: 20),
Expanded(
child: SingleChildScrollView(
child: Column(
children: <Widget>[
for (var category in VideoCardsCategory.values)
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text( Text(
'$_counter', 'Видеокарты: ${category.name}',
style: Theme.of(context).textTheme.headlineMedium, style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
for (var videocards in _videoCardsManager.filterVideoCardsByCategory(category))
Text(
'${videocards.videomemory} - Цена: ${videocards.formattedPrice} рублей.',
style: const TextStyle(fontSize: 18),
),
SizedBox(height: 10),
],
),
if (_isClearing)
const Padding(
padding: const EdgeInsets.all(16.0),
child: Text(
'Очищение списка...',
style: TextStyle(fontSize: 18, color: Colors.blueGrey),
),
),
],
),
),
), ),
], ],
), ),
), ),
floatingActionButton: FloatingActionButton( floatingActionButton: Row(
onPressed: _incrementCounter, mainAxisAlignment: MainAxisAlignment.end,
tooltip: 'Increment', children: [
FloatingActionButton(
onPressed: () async {
_addRandomVideocards();
},
backgroundColor: _color,
child: const Icon(Icons.add), child: const Icon(Icons.add),
), ); ),
SizedBox(width: 10),
FloatingActionButton(
onPressed: () async {
setState(() {
_isClearing = true;
});
await _videoCardsManager.clearVideoCards();
setState(() {
_isClearing = false;
});
},
backgroundColor: Colors.red,
child: const Icon(Icons.clear),
),
],
),
);
} }
} }