лабораторная 2 пажаласта!
This commit is contained in:
parent
2725b9e45a
commit
bb2bd70230
24
lib/FinanceManager.dart
Normal file
24
lib/FinanceManager.dart
Normal file
@ -0,0 +1,24 @@
|
||||
import 'Transaction.dart';
|
||||
|
||||
// Класс для управления списком транзакций
|
||||
class FinanceManager {
|
||||
final List<Transaction> _transactions = [];
|
||||
|
||||
void addTransaction(Transaction transaction) {
|
||||
_transactions.add(transaction);
|
||||
}
|
||||
|
||||
List<Transaction> filterTransactionsByCategory(TransactionCategory category) {
|
||||
return _transactions.where((transaction) => transaction.category == category).toList();
|
||||
}
|
||||
|
||||
double calculateTotalAmount() {
|
||||
return _transactions.fold(0.0, (sum, transaction) => sum + transaction.amount);
|
||||
}
|
||||
|
||||
Future<void> clearTransactions() async {
|
||||
await Future.delayed(Duration(seconds: 2)); // Имитация задержки
|
||||
_transactions.clear();
|
||||
print('Список успешно очищен');
|
||||
}
|
||||
}
|
17
lib/Transaction.dart
Normal file
17
lib/Transaction.dart
Normal file
@ -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);
|
||||
}
|
116
lib/main.dart
116
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<MyHomePage> createState() => _MyHomePageState();
|
||||
}
|
||||
|
||||
// Расширение для класса Transaction
|
||||
extension TransactionExtensions on Transaction {
|
||||
String get formattedAmount => '${amount.toStringAsFixed(2)}';
|
||||
}
|
||||
|
||||
class _MyHomePageState extends State<MyHomePage> {
|
||||
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<MyHomePage> {
|
||||
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,
|
||||
'Учет расходов',
|
||||
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: <Widget>[
|
||||
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),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user