Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
bb2bd70230 | ||
|
2725b9e45a |
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);
|
||||||
|
}
|
111
lib/main.dart
111
lib/main.dart
@ -1,7 +1,11 @@
|
|||||||
import 'dart:math';
|
import 'dart:math';
|
||||||
|
import 'package:app/FinanceManager.dart';
|
||||||
|
|
||||||
|
import 'Transaction.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
|
import 'dart:async';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
runApp(const MyApp());
|
runApp(const MyApp());
|
||||||
}
|
}
|
||||||
@ -13,11 +17,12 @@ 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.teal),
|
colorScheme: ColorScheme.fromSeed(seedColor: Colors.teal),
|
||||||
useMaterial3: true,
|
useMaterial3: true,
|
||||||
),
|
),
|
||||||
home: const MyHomePage(title: 'Flutter rocks!'),
|
home: const MyHomePage(title: 'Пучкина Анна Алексеевна'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -31,13 +36,30 @@ class MyHomePage extends StatefulWidget {
|
|||||||
State<MyHomePage> createState() => _MyHomePageState();
|
State<MyHomePage> createState() => _MyHomePageState();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Расширение для класса Transaction
|
||||||
|
extension TransactionExtensions on Transaction {
|
||||||
|
String get formattedAmount => '${amount.toStringAsFixed(2)}';
|
||||||
|
}
|
||||||
|
|
||||||
class _MyHomePageState extends State<MyHomePage> {
|
class _MyHomePageState extends State<MyHomePage> {
|
||||||
int _counter = 0;
|
|
||||||
Color _color = Colors.teal;
|
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(() {
|
setState(() {
|
||||||
_counter++;
|
|
||||||
_color = Color((Random().nextDouble() * 0xFFFFFF).toInt()).withOpacity(1.0);
|
_color = Color((Random().nextDouble() * 0xFFFFFF).toInt()).withOpacity(1.0);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -53,26 +75,79 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
child: Column(
|
child: Column(
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
children: <Widget>[
|
children: <Widget>[
|
||||||
const Text(
|
|
||||||
'You have pushed the button this many times:',
|
|
||||||
),
|
|
||||||
Text(
|
Text(
|
||||||
'$_counter',
|
'Учет расходов',
|
||||||
style: Theme.of(context).textTheme.headlineMedium,
|
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
|
||||||
),
|
),
|
||||||
if (_counter > 10)
|
SizedBox(height: 10),
|
||||||
Text(
|
Text(
|
||||||
'Че кликаем?!',
|
'Итоговая сумма: ${_financeManager.calculateTotalAmount().toStringAsFixed(2)}',
|
||||||
style: Theme.of(context).textTheme.headlineMedium,
|
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(
|
floatingActionButton: Row(
|
||||||
onPressed: _incrementCounter,
|
mainAxisAlignment: MainAxisAlignment.end,
|
||||||
|
children: [
|
||||||
|
FloatingActionButton(
|
||||||
|
onPressed: () async {
|
||||||
|
_addRandomTransaction();
|
||||||
|
},
|
||||||
backgroundColor: _color,
|
backgroundColor: _color,
|
||||||
tooltip: 'Increment',
|
child: const Icon(Icons.add),
|
||||||
child: const Icon(Icons.accessible_forward_sharp),
|
),
|
||||||
), );
|
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