You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.8 KiB
Dart
63 lines
1.8 KiB
Dart
import 'package:expense_planner/widgets/chart_bar.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:intl/intl.dart';
|
|
|
|
import '../models/transaction.dart';
|
|
|
|
class Chart extends StatelessWidget {
|
|
const Chart({Key? key, required this.transactions}) : super(key: key);
|
|
|
|
final List<Transaction> transactions;
|
|
|
|
List<Map<String, Object>> get groupedTransactions {
|
|
return List.generate(7, (index) {
|
|
final weekDay = DateTime.now().subtract(Duration(days: index));
|
|
double totalSum = 0;
|
|
|
|
for (var transaction in transactions) {
|
|
if (transaction.date.day == weekDay.day &&
|
|
transaction.date.month == weekDay.month &&
|
|
transaction.date.year == weekDay.year) {
|
|
totalSum += transaction.amount;
|
|
}
|
|
}
|
|
|
|
return {
|
|
'day': DateFormat.E().format(weekDay).substring(0, 1).toUpperCase(),
|
|
'amount': totalSum,
|
|
};
|
|
}).reversed.toList();
|
|
}
|
|
|
|
double get maxSpending {
|
|
return groupedTransactions.fold(
|
|
0.0, (sum, transaction) => sum + (transaction['amount'] as double));
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Card(
|
|
elevation: 1,
|
|
margin: EdgeInsets.all(10),
|
|
child: Padding(
|
|
padding: EdgeInsets.all(5),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
|
children: groupedTransactions.map((data) {
|
|
return Flexible(
|
|
fit: FlexFit.tight,
|
|
child: ChartBar(
|
|
label: (data['day'] as String),
|
|
amount: (data['amount'] as double),
|
|
spendingPercentTotal: maxSpending == 0.0
|
|
? 0.0
|
|
: (data['amount'] as double) / maxSpending,
|
|
),
|
|
);
|
|
}).toList(),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|