PMU_Puchkina_A.A_PIbd-32/lib/main.dart
2024-09-23 21:40:45 +04:00

154 lines
4.6 KiB
Dart

import 'dart:math';
import 'package:app/FinanceManager.dart';
import 'Transaction.dart';
import 'package:flutter/material.dart';
import 'dart:async';
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.teal),
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();
}
// Расширение для класса Transaction
extension TransactionExtensions on Transaction {
String get formattedAmount => '${amount.toStringAsFixed(2)}';
}
class _MyHomePageState extends State<MyHomePage> {
Color _color = Colors.teal;
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(() {
_color = Color((Random().nextDouble() * 0xFFFFFF).toInt()).withOpacity(1.0);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: _color,
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Учет расходов',
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
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: 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),
),
],
),
);
}
}