From bb2bd702309b8e061d3fb65ff9b568f1799a06d1 Mon Sep 17 00:00:00 2001 From: "a.puchkina" Date: Mon, 23 Sep 2024 21:40:45 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=202=20=D0=BF=D0=B0=D0=B6=D0=B0?= =?UTF-8?q?=D0=BB=D0=B0=D1=81=D1=82=D0=B0!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/FinanceManager.dart | 24 +++++++++ lib/Transaction.dart | 17 ++++++ lib/file | 0 lib/main.dart | 116 ++++++++++++++++++++++++++++++++-------- 4 files changed, 136 insertions(+), 21 deletions(-) create mode 100644 lib/FinanceManager.dart create mode 100644 lib/Transaction.dart delete mode 100644 lib/file diff --git a/lib/FinanceManager.dart b/lib/FinanceManager.dart new file mode 100644 index 0000000..3a0c583 --- /dev/null +++ b/lib/FinanceManager.dart @@ -0,0 +1,24 @@ +import 'Transaction.dart'; + +// Класс для управления списком транзакций +class FinanceManager { + final List _transactions = []; + + void addTransaction(Transaction transaction) { + _transactions.add(transaction); + } + + List filterTransactionsByCategory(TransactionCategory category) { + return _transactions.where((transaction) => transaction.category == category).toList(); + } + + double calculateTotalAmount() { + return _transactions.fold(0.0, (sum, transaction) => sum + transaction.amount); + } + + Future clearTransactions() async { + await Future.delayed(Duration(seconds: 2)); // Имитация задержки + _transactions.clear(); + print('Список успешно очищен'); + } +} \ No newline at end of file diff --git a/lib/Transaction.dart b/lib/Transaction.dart new file mode 100644 index 0000000..2378562 --- /dev/null +++ b/lib/Transaction.dart @@ -0,0 +1,17 @@ +import 'dart:async'; +import 'main.dart'; + +enum TransactionCategory { + food, + transport, + entertainment, + other, +} + +class Transaction { + final String description; + final double amount; + final TransactionCategory category; + + Transaction(this.description, this.amount, this.category); +} \ No newline at end of file diff --git a/lib/file b/lib/file deleted file mode 100644 index e69de29..0000000 diff --git a/lib/main.dart b/lib/main.dart index b7777d2..6802c6d 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,7 +1,11 @@ import 'dart:math'; +import 'package:app/FinanceManager.dart'; +import 'Transaction.dart'; import 'package:flutter/material.dart'; +import 'dart:async'; + void main() { runApp(const MyApp()); } @@ -18,7 +22,7 @@ class MyApp extends StatelessWidget { colorScheme: ColorScheme.fromSeed(seedColor: Colors.teal), useMaterial3: true, ), - home: const MyHomePage(title: 'Flutter rocks!'), + home: const MyHomePage(title: 'Пучкина Анна Алексеевна'), ); } } @@ -32,13 +36,30 @@ class MyHomePage extends StatefulWidget { State createState() => _MyHomePageState(); } +// Расширение для класса Transaction +extension TransactionExtensions on Transaction { + String get formattedAmount => '${amount.toStringAsFixed(2)}'; +} + class _MyHomePageState extends State { - int _counter = 0; Color _color = Colors.teal; - void _incrementCounter() { + final FinanceManager _financeManager = FinanceManager(); + bool _isClearing = false; + + int counter = 1; + + void _addRandomTransaction() { + final random = Random(); + final description = 'Транзакция $counter'; + final amount = random.nextDouble() * 1000; + final category = TransactionCategory.values[random.nextInt(TransactionCategory.values.length)]; + + final transaction = Transaction(description, amount, category); + _financeManager.addTransaction(transaction); + counter++; + setState(() { - _counter++; _color = Color((Random().nextDouble() * 0xFFFFFF).toInt()).withOpacity(1.0); }); } @@ -54,26 +75,79 @@ class _MyHomePageState extends State { child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ - const Text( - 'You have pushed the button this many times:', - ), Text( - '$_counter', - style: Theme.of(context).textTheme.headlineMedium, + 'Учет расходов', + style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold), ), - if (_counter > 10) - Text( - 'Че кликаем?!', - style: Theme.of(context).textTheme.headlineMedium, - ) - ], + SizedBox(height: 10), + Text( + 'Итоговая сумма: ${_financeManager.calculateTotalAmount().toStringAsFixed(2)}', + style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold), + ), + SizedBox(height: 20), + Expanded( + child: SingleChildScrollView( + child: Column( + children: [ + for (var category in TransactionCategory.values) + Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + 'Категория: ${category.name}', + style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold), + ), + for (var transaction in _financeManager.filterTransactionsByCategory(category)) + Text( + '${transaction.description} - Сумма: ${transaction.formattedAmount}', + style: const TextStyle(fontSize: 18), + ), + SizedBox(height: 10), + ], + ), + if (_isClearing) + Padding( + padding: const EdgeInsets.all(16.0), + child: Text( + 'Очищение списка...', + style: TextStyle(fontSize: 18, color: Colors.red), + ), + ), + ], + + ), + + ), + ), + ], ), ), - floatingActionButton: FloatingActionButton( - onPressed: _incrementCounter, - backgroundColor: _color, - tooltip: 'Increment', - child: const Icon(Icons.accessible_forward_sharp), - ), ); + floatingActionButton: Row( + mainAxisAlignment: MainAxisAlignment.end, + children: [ + FloatingActionButton( + onPressed: () async { + _addRandomTransaction(); + }, + backgroundColor: _color, + child: const Icon(Icons.add), + ), + SizedBox(width: 10), + FloatingActionButton( + onPressed: () async { + setState(() { + _isClearing = true; + }); + await _financeManager.clearTransactions(); + setState(() { + _isClearing = false; + }); + }, + backgroundColor: Colors.red, + child: const Icon(Icons.clear), + ), + ], + ), + ); } }