2023-12-25 19:03:52 +01:00
|
|
|
import 'package:currency_picker/currency_picker.dart';
|
2023-12-25 19:54:30 +01:00
|
|
|
import 'package:dynamic_color/dynamic_color.dart';
|
2023-11-21 20:23:14 +01:00
|
|
|
import 'package:fl_chart/fl_chart.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2023-12-25 19:03:52 +01:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
2024-01-08 15:23:42 +01:00
|
|
|
import 'package:intl/intl.dart';
|
2023-12-29 19:43:27 +01:00
|
|
|
import 'package:prasule/api/category.dart';
|
|
|
|
import 'package:prasule/api/walletentry.dart';
|
2024-01-08 15:23:42 +01:00
|
|
|
import 'package:prasule/main.dart';
|
|
|
|
import 'package:prasule/util/get_last_date.dart';
|
|
|
|
import 'package:prasule/util/text_color.dart';
|
2023-11-21 20:23:14 +01:00
|
|
|
|
2023-11-22 15:09:05 +01:00
|
|
|
/// Monthly/Yearly expense/income [LineChart]
|
2023-12-25 19:03:52 +01:00
|
|
|
class ExpensesLineChart extends StatelessWidget {
|
2023-12-29 21:39:54 +01:00
|
|
|
/// Monthly/Yearly expense/income [LineChart]
|
|
|
|
const ExpensesLineChart({
|
|
|
|
required this.date,
|
|
|
|
required this.locale,
|
|
|
|
required this.expenseData,
|
|
|
|
required this.incomeData,
|
|
|
|
required this.currency,
|
|
|
|
super.key,
|
|
|
|
this.yearly = false,
|
|
|
|
});
|
|
|
|
|
|
|
|
/// If the graph will be shown yearly
|
2023-11-21 20:23:14 +01:00
|
|
|
final bool yearly;
|
2023-12-29 21:39:54 +01:00
|
|
|
|
|
|
|
/// Selected date
|
|
|
|
///
|
|
|
|
/// Used to get either month or year
|
2023-11-21 20:23:14 +01:00
|
|
|
final DateTime date;
|
2023-12-29 21:39:54 +01:00
|
|
|
|
|
|
|
/// Current locale
|
|
|
|
///
|
|
|
|
/// Used mainly for formatting
|
2023-11-21 20:23:14 +01:00
|
|
|
final String locale;
|
2023-12-29 21:39:54 +01:00
|
|
|
|
|
|
|
/// The expense data used for the graph
|
2023-11-22 15:09:05 +01:00
|
|
|
final List<double> expenseData;
|
2023-12-29 21:39:54 +01:00
|
|
|
|
|
|
|
/// Wallet currency
|
|
|
|
///
|
|
|
|
/// Used to show currency symbol
|
2023-12-25 19:03:52 +01:00
|
|
|
final Currency currency;
|
2023-11-21 20:23:14 +01:00
|
|
|
|
2023-12-29 21:39:54 +01:00
|
|
|
/// Expense data, but sorted
|
|
|
|
List<double> get expenseDataSorted =>
|
|
|
|
List<double>.from(expenseData)..sort((a, b) => a.compareTo(b));
|
|
|
|
|
|
|
|
/// Income data used for the graph
|
2023-11-22 15:09:05 +01:00
|
|
|
final List<double> incomeData;
|
|
|
|
|
2023-12-29 21:39:54 +01:00
|
|
|
/// Income data, but sorted
|
|
|
|
List<double> get incomeDataSorted =>
|
|
|
|
List<double>.from(incomeData)..sort((a, b) => a.compareTo(b));
|
|
|
|
|
|
|
|
/// Calculates maxY for the graph
|
2023-11-22 15:09:05 +01:00
|
|
|
double get maxY {
|
|
|
|
if (incomeData.isEmpty) return expenseDataSorted.last;
|
|
|
|
if (expenseData.isEmpty) return incomeDataSorted.last;
|
|
|
|
if (expenseDataSorted.last > incomeDataSorted.last) {
|
|
|
|
return expenseDataSorted.last;
|
|
|
|
} else {
|
|
|
|
return incomeDataSorted.last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-21 20:23:14 +01:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return LineChart(
|
|
|
|
LineChartData(
|
2023-12-25 19:03:52 +01:00
|
|
|
lineTouchData: LineTouchData(
|
|
|
|
touchTooltipData: LineTouchTooltipData(
|
|
|
|
getTooltipItems: (spots) => List<LineTooltipItem>.generate(
|
|
|
|
spots.length,
|
|
|
|
(index) => LineTooltipItem(
|
2024-01-08 15:23:42 +01:00
|
|
|
// Changes what's rendered on the tooltip
|
|
|
|
// when clicked in the chart
|
|
|
|
(spots[index].barIndex == 0) // income chart
|
2023-12-25 19:26:46 +01:00
|
|
|
? (yearly
|
|
|
|
? AppLocalizations.of(context).incomeForMonth(
|
2023-12-29 21:39:54 +01:00
|
|
|
DateFormat.MMMM(locale).format(
|
|
|
|
DateTime(
|
|
|
|
date.year,
|
|
|
|
spots[index].x.toInt() + 1,
|
|
|
|
),
|
|
|
|
),
|
2023-12-25 19:26:46 +01:00
|
|
|
NumberFormat.compactCurrency(
|
2023-12-29 21:39:54 +01:00
|
|
|
locale: locale,
|
|
|
|
symbol: currency.symbol,
|
|
|
|
name: currency.name,
|
|
|
|
).format(spots[index].y),
|
|
|
|
)
|
2023-12-25 19:26:46 +01:00
|
|
|
: AppLocalizations.of(context).incomeForDay(
|
|
|
|
NumberFormat.compactCurrency(
|
2023-12-29 21:39:54 +01:00
|
|
|
locale: locale,
|
|
|
|
symbol: currency.symbol,
|
|
|
|
name: currency.name,
|
|
|
|
).format(spots[index].y),
|
2023-12-25 19:26:46 +01:00
|
|
|
))
|
2024-01-08 15:23:42 +01:00
|
|
|
: (yearly // expense chart
|
2023-12-25 19:26:46 +01:00
|
|
|
? AppLocalizations.of(context).expensesForMonth(
|
2023-12-29 21:39:54 +01:00
|
|
|
DateFormat.MMMM(locale).format(
|
|
|
|
DateTime(
|
|
|
|
date.year,
|
|
|
|
spots[index].x.toInt() + 1,
|
|
|
|
),
|
|
|
|
),
|
2023-12-25 19:26:46 +01:00
|
|
|
NumberFormat.compactCurrency(
|
2023-12-29 21:39:54 +01:00
|
|
|
locale: locale,
|
|
|
|
symbol: currency.symbol,
|
|
|
|
name: currency.name,
|
|
|
|
).format(spots[index].y),
|
|
|
|
)
|
2023-12-25 19:26:46 +01:00
|
|
|
: AppLocalizations.of(context).expensesForDay(
|
|
|
|
NumberFormat.compactCurrency(
|
2023-12-29 21:39:54 +01:00
|
|
|
locale: locale,
|
|
|
|
symbol: currency.symbol,
|
|
|
|
name: currency.name,
|
|
|
|
).format(spots[index].y),
|
2023-12-25 19:26:46 +01:00
|
|
|
)),
|
2023-12-25 19:03:52 +01:00
|
|
|
TextStyle(color: spots[index].bar.color),
|
2024-01-08 15:23:42 +01:00
|
|
|
children: [
|
|
|
|
TextSpan(
|
|
|
|
text: "\n${yearly ? DateFormat.MMMM(locale).format(
|
|
|
|
DateTime(
|
|
|
|
date.year,
|
|
|
|
index + 1,
|
|
|
|
),
|
|
|
|
) : DateFormat.yMMMMd(locale).format(DateTime(date.year, date.month, spots[index].spotIndex + 1))}",
|
|
|
|
),
|
|
|
|
],
|
2023-12-25 19:03:52 +01:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2023-11-22 15:09:05 +01:00
|
|
|
maxY: maxY,
|
2024-01-08 15:23:42 +01:00
|
|
|
maxX: yearly
|
|
|
|
? 12
|
|
|
|
: date.lastDay.toDouble() -
|
|
|
|
1, // remove 1 because we are indexing from 0
|
2023-11-21 20:23:14 +01:00
|
|
|
minY: 0,
|
2023-12-25 19:03:52 +01:00
|
|
|
minX: 0,
|
2023-11-21 20:23:14 +01:00
|
|
|
backgroundColor: Theme.of(context).colorScheme.background,
|
|
|
|
lineBarsData: [
|
2023-11-22 15:09:05 +01:00
|
|
|
if (incomeData.isNotEmpty)
|
|
|
|
LineChartBarData(
|
|
|
|
isCurved: true,
|
|
|
|
barWidth: 8,
|
|
|
|
isStrokeCapRound: true,
|
|
|
|
dotData: const FlDotData(show: false),
|
2023-12-29 21:39:54 +01:00
|
|
|
belowBarData: BarAreaData(),
|
2024-01-08 15:23:42 +01:00
|
|
|
color:
|
|
|
|
(MediaQuery.of(context).platformBrightness == Brightness.dark)
|
|
|
|
? Colors.green.shade300
|
|
|
|
: Colors.green
|
|
|
|
.harmonizeWith(Theme.of(context).colorScheme.primary),
|
2023-11-22 15:09:05 +01:00
|
|
|
spots: List.generate(
|
2024-01-08 15:23:42 +01:00
|
|
|
yearly ? 12 : date.lastDay,
|
2023-12-25 19:03:52 +01:00
|
|
|
(index) => FlSpot(index.toDouble(), incomeData[index]),
|
2023-11-22 15:09:05 +01:00
|
|
|
),
|
|
|
|
),
|
|
|
|
if (expenseData.isNotEmpty)
|
|
|
|
LineChartBarData(
|
|
|
|
isCurved: true,
|
|
|
|
barWidth: 8,
|
|
|
|
isStrokeCapRound: true,
|
|
|
|
dotData: const FlDotData(show: false),
|
2023-12-29 21:39:54 +01:00
|
|
|
belowBarData: BarAreaData(),
|
2024-01-08 15:23:42 +01:00
|
|
|
color:
|
|
|
|
(MediaQuery.of(context).platformBrightness == Brightness.dark)
|
|
|
|
? Colors.red.shade300
|
|
|
|
: Colors.red
|
|
|
|
.harmonizeWith(Theme.of(context).colorScheme.primary),
|
2023-11-22 15:09:05 +01:00
|
|
|
spots: List.generate(
|
2024-01-08 15:23:42 +01:00
|
|
|
yearly
|
|
|
|
? 12
|
|
|
|
: date.lastDay, // no -1 because it's the length, not index
|
|
|
|
(index) => FlSpot(index.toDouble(), expenseData[index]),
|
2023-11-22 15:09:05 +01:00
|
|
|
),
|
2023-11-21 20:23:14 +01:00
|
|
|
),
|
|
|
|
], // actual data
|
|
|
|
titlesData: FlTitlesData(
|
2023-12-29 21:39:54 +01:00
|
|
|
rightTitles: const AxisTitles(),
|
|
|
|
topTitles: const AxisTitles(),
|
2024-01-08 15:23:42 +01:00
|
|
|
leftTitles: AxisTitles(
|
|
|
|
sideTitles: SideTitles(
|
|
|
|
reservedSize: (NumberFormat.compact()
|
|
|
|
.format(expenseDataSorted.last)
|
|
|
|
.length >=
|
|
|
|
5 ||
|
|
|
|
NumberFormat.compact()
|
|
|
|
.format(incomeDataSorted.last)
|
|
|
|
.length >=
|
|
|
|
5)
|
|
|
|
? 50
|
|
|
|
: 25,
|
|
|
|
showTitles: true,
|
|
|
|
),
|
|
|
|
),
|
2023-11-21 20:23:14 +01:00
|
|
|
bottomTitles: AxisTitles(
|
|
|
|
sideTitles: SideTitles(
|
2023-12-25 19:03:52 +01:00
|
|
|
reservedSize: 30,
|
2023-11-21 20:23:14 +01:00
|
|
|
showTitles: true,
|
|
|
|
getTitlesWidget: (value, meta) {
|
|
|
|
String text;
|
|
|
|
if (yearly) {
|
|
|
|
text = DateFormat.MMM(locale).format(
|
2023-12-29 21:39:54 +01:00
|
|
|
DateTime(date.year, value.toInt() + 1),
|
2023-11-21 20:23:14 +01:00
|
|
|
);
|
|
|
|
} else {
|
2023-12-25 19:03:52 +01:00
|
|
|
text = (value.toInt() + 1).toString();
|
2023-11-21 20:23:14 +01:00
|
|
|
}
|
|
|
|
return SideTitleWidget(
|
2023-12-29 21:39:54 +01:00
|
|
|
axisSide: meta.axisSide,
|
|
|
|
child: Text(text),
|
|
|
|
);
|
2023-11-21 20:23:14 +01:00
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
), // axis descriptions
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2023-12-25 19:03:52 +01:00
|
|
|
|
2023-12-29 21:39:54 +01:00
|
|
|
/// Renders expenses/income as a [BarChart]
|
2023-12-25 19:03:52 +01:00
|
|
|
class ExpensesBarChart extends StatelessWidget {
|
2023-12-29 21:39:54 +01:00
|
|
|
/// Renders expenses/income as a [BarChart]
|
|
|
|
const ExpensesBarChart({
|
|
|
|
required this.yearly,
|
|
|
|
required this.date,
|
|
|
|
required this.locale,
|
|
|
|
required this.expenseData,
|
|
|
|
required this.incomeData,
|
|
|
|
required this.currency,
|
|
|
|
super.key,
|
|
|
|
});
|
|
|
|
|
|
|
|
/// If the graph will be shown yearly
|
2023-12-25 19:03:52 +01:00
|
|
|
final bool yearly;
|
2023-12-29 21:39:54 +01:00
|
|
|
|
|
|
|
/// Selected date
|
|
|
|
///
|
|
|
|
/// Used to get either month or year
|
2023-12-25 19:03:52 +01:00
|
|
|
final DateTime date;
|
2023-12-29 21:39:54 +01:00
|
|
|
|
|
|
|
/// Current locale
|
|
|
|
///
|
|
|
|
/// Used mainly for formatting
|
2023-12-25 19:03:52 +01:00
|
|
|
final String locale;
|
2023-12-29 21:39:54 +01:00
|
|
|
|
|
|
|
/// The expense data used for the graph
|
2023-12-25 19:03:52 +01:00
|
|
|
final List<double> expenseData;
|
|
|
|
|
2023-12-29 21:39:54 +01:00
|
|
|
/// Wallet currency
|
|
|
|
///
|
|
|
|
/// Used to show currency symbol
|
2023-12-25 19:03:52 +01:00
|
|
|
final Currency currency;
|
|
|
|
|
2023-12-29 21:39:54 +01:00
|
|
|
/// Expense data, but sorted
|
|
|
|
List<double> get expenseDataSorted =>
|
|
|
|
List<double>.from(expenseData)..sort((a, b) => a.compareTo(b));
|
|
|
|
|
|
|
|
/// Income data used for the graph
|
2023-12-25 19:03:52 +01:00
|
|
|
final List<double> incomeData;
|
|
|
|
|
2023-12-29 21:39:54 +01:00
|
|
|
/// Income data, but sorted
|
|
|
|
List<double> get incomeDataSorted =>
|
|
|
|
List<double>.from(incomeData)..sort((a, b) => a.compareTo(b));
|
|
|
|
|
|
|
|
/// Calculates maxY for the graph
|
2023-12-25 19:03:52 +01:00
|
|
|
double get maxY {
|
|
|
|
if (incomeData.isEmpty) return expenseDataSorted.last;
|
|
|
|
if (expenseData.isEmpty) return incomeDataSorted.last;
|
|
|
|
if (expenseDataSorted.last > incomeDataSorted.last) {
|
|
|
|
return expenseDataSorted.last;
|
|
|
|
} else {
|
|
|
|
return incomeDataSorted.last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) => BarChart(
|
|
|
|
BarChartData(
|
|
|
|
barTouchData: BarTouchData(
|
|
|
|
enabled: true,
|
|
|
|
touchTooltipData: BarTouchTooltipData(
|
|
|
|
getTooltipItem: (group, groupIndex, rod, rodIndex) =>
|
2023-12-29 21:39:54 +01:00
|
|
|
yearly // create custom tooltips for graph bars
|
2023-12-25 19:03:52 +01:00
|
|
|
? BarTooltipItem(
|
|
|
|
(rodIndex == 1)
|
|
|
|
? AppLocalizations.of(context).expensesForMonth(
|
|
|
|
DateFormat.MMMM(locale).format(
|
2023-12-29 21:39:54 +01:00
|
|
|
DateTime(date.year, groupIndex + 1),
|
|
|
|
),
|
2023-12-25 19:03:52 +01:00
|
|
|
NumberFormat.compactCurrency(
|
2023-12-29 21:39:54 +01:00
|
|
|
locale: locale,
|
|
|
|
symbol: currency.symbol,
|
|
|
|
name: currency.name,
|
|
|
|
).format(rod.toY),
|
2023-12-25 19:03:52 +01:00
|
|
|
)
|
|
|
|
: AppLocalizations.of(context).incomeForMonth(
|
|
|
|
DateFormat.MMMM(locale).format(
|
2023-12-29 21:39:54 +01:00
|
|
|
DateTime(date.year, groupIndex + 1),
|
|
|
|
),
|
2023-12-25 19:03:52 +01:00
|
|
|
NumberFormat.compactCurrency(
|
2023-12-29 21:39:54 +01:00
|
|
|
locale: locale,
|
|
|
|
symbol: currency.symbol,
|
|
|
|
name: currency.name,
|
|
|
|
).format(rod.toY),
|
2023-12-25 19:03:52 +01:00
|
|
|
),
|
|
|
|
TextStyle(color: rod.color),
|
|
|
|
)
|
|
|
|
: BarTooltipItem(
|
|
|
|
(rodIndex == 1)
|
|
|
|
? AppLocalizations.of(context).expensesForDay(
|
|
|
|
NumberFormat.compactCurrency(
|
2023-12-29 21:39:54 +01:00
|
|
|
locale: locale,
|
|
|
|
symbol: currency.symbol,
|
|
|
|
name: currency.name,
|
|
|
|
).format(rod.toY),
|
2023-12-25 19:03:52 +01:00
|
|
|
)
|
|
|
|
: AppLocalizations.of(context).incomeForDay(
|
|
|
|
NumberFormat.compactCurrency(
|
2023-12-29 21:39:54 +01:00
|
|
|
locale: locale,
|
|
|
|
symbol: currency.symbol,
|
|
|
|
name: currency.name,
|
|
|
|
).format(rod.toY),
|
2023-12-25 19:03:52 +01:00
|
|
|
),
|
|
|
|
TextStyle(color: rod.color),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
titlesData: FlTitlesData(
|
2023-12-29 21:39:54 +01:00
|
|
|
rightTitles: const AxisTitles(),
|
|
|
|
topTitles: const AxisTitles(),
|
2023-12-25 19:03:52 +01:00
|
|
|
bottomTitles: AxisTitles(
|
|
|
|
sideTitles: SideTitles(
|
|
|
|
showTitles: true,
|
|
|
|
reservedSize: 30,
|
|
|
|
getTitlesWidget: (value, meta) {
|
|
|
|
String text;
|
|
|
|
if (yearly) {
|
|
|
|
text = DateFormat.MMM(locale).format(
|
2023-12-29 21:39:54 +01:00
|
|
|
DateTime(date.year, value.toInt() + 1),
|
2023-12-25 19:03:52 +01:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
text = (value.toInt() + 1).toString();
|
|
|
|
}
|
|
|
|
return SideTitleWidget(
|
2023-12-29 21:39:54 +01:00
|
|
|
axisSide: meta.axisSide,
|
|
|
|
child: Text(text),
|
|
|
|
);
|
2023-12-25 19:03:52 +01:00
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
), // axis descriptions,
|
|
|
|
minY: 0,
|
|
|
|
maxY: maxY,
|
|
|
|
barGroups: List<BarChartGroupData>.generate(
|
2024-01-08 15:23:42 +01:00
|
|
|
yearly ? 12 : date.lastDay - 1,
|
2023-12-25 19:03:52 +01:00
|
|
|
(index) => BarChartGroupData(
|
|
|
|
x: index,
|
|
|
|
barRods: [
|
|
|
|
if (incomeData.isNotEmpty)
|
|
|
|
BarChartRodData(
|
|
|
|
toY: incomeData[index],
|
2023-12-25 19:54:30 +01:00
|
|
|
color: Colors.green
|
2024-01-08 15:23:42 +01:00
|
|
|
.harmonizeWith(Theme.of(context).colorScheme.primary),
|
2023-12-25 19:03:52 +01:00
|
|
|
),
|
|
|
|
if (expenseData.isNotEmpty)
|
|
|
|
BarChartRodData(
|
|
|
|
toY: expenseData[index],
|
2023-12-25 19:54:30 +01:00
|
|
|
color: Colors.red
|
2024-01-08 15:23:42 +01:00
|
|
|
.harmonizeWith(Theme.of(context).colorScheme.primary),
|
2023-12-25 19:03:52 +01:00
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2023-12-29 19:43:27 +01:00
|
|
|
|
2024-01-08 15:23:42 +01:00
|
|
|
/// [PieChart] used to display expenses/income visualized
|
|
|
|
/// under their respective category
|
|
|
|
class CategoriesPieChart extends StatefulWidget {
|
|
|
|
/// [PieChart] used to display expenses/income visualized
|
|
|
|
/// under their respective category
|
|
|
|
const CategoriesPieChart({
|
|
|
|
required this.entries,
|
|
|
|
required this.categories,
|
|
|
|
required this.symbol,
|
|
|
|
super.key,
|
|
|
|
});
|
2023-12-29 19:43:27 +01:00
|
|
|
|
2024-01-08 15:23:42 +01:00
|
|
|
/// Entries to be used
|
2023-12-29 19:43:27 +01:00
|
|
|
final List<WalletSingleEntry> entries;
|
2024-01-08 15:23:42 +01:00
|
|
|
|
|
|
|
/// Categories to be displayed
|
2023-12-29 19:43:27 +01:00
|
|
|
final List<WalletCategory> categories;
|
|
|
|
|
2024-01-08 15:23:42 +01:00
|
|
|
/// Currency symbol displayed on the chart
|
|
|
|
final String symbol;
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<CategoriesPieChart> createState() => _CategoriesPieChartState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _CategoriesPieChartState extends State<CategoriesPieChart> {
|
|
|
|
int touchedIndex = -1;
|
|
|
|
|
2023-12-29 19:43:27 +01:00
|
|
|
@override
|
2024-01-08 15:23:42 +01:00
|
|
|
Widget build(BuildContext context) => Column(
|
|
|
|
children: [
|
|
|
|
SizedBox(
|
|
|
|
width: MediaQuery.of(context).size.width,
|
|
|
|
child: Wrap(
|
|
|
|
alignment: WrapAlignment.center,
|
|
|
|
spacing: 4,
|
|
|
|
children: List<Widget>.generate(
|
|
|
|
widget.categories.length,
|
|
|
|
(index) => Padding(
|
|
|
|
padding: const EdgeInsets.all(8),
|
|
|
|
child: Indicator(
|
|
|
|
size: touchedIndex == index ? 18 : 16,
|
|
|
|
color: widget.categories[index].color,
|
|
|
|
text: widget.categories[index].name,
|
|
|
|
textStyle: TextStyle(
|
|
|
|
fontWeight: (touchedIndex == index)
|
|
|
|
? FontWeight.bold
|
|
|
|
: FontWeight.normal,
|
|
|
|
),
|
2023-12-29 19:43:27 +01:00
|
|
|
),
|
2024-01-08 15:23:42 +01:00
|
|
|
),
|
|
|
|
),
|
2023-12-29 19:43:27 +01:00
|
|
|
),
|
|
|
|
),
|
2024-01-08 15:23:42 +01:00
|
|
|
const SizedBox(
|
|
|
|
height: 5,
|
|
|
|
),
|
|
|
|
Expanded(
|
|
|
|
child: PieChart(
|
|
|
|
PieChartData(
|
|
|
|
centerSpaceRadius: double.infinity,
|
|
|
|
pieTouchData: PieTouchData(
|
|
|
|
touchCallback: (event, response) {
|
|
|
|
// Set touchedIndex so we can highlight
|
|
|
|
// the corresponding indicator
|
|
|
|
setState(() {
|
|
|
|
if (!event.isInterestedForInteractions ||
|
|
|
|
response == null ||
|
|
|
|
response.touchedSection == null) {
|
|
|
|
touchedIndex = -1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
touchedIndex =
|
|
|
|
response.touchedSection!.touchedSectionIndex;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
),
|
|
|
|
sections: List<PieChartSectionData>.generate(
|
|
|
|
widget.categories.length,
|
|
|
|
(index) => PieChartSectionData(
|
|
|
|
title: NumberFormat.compactCurrency(symbol: widget.symbol)
|
|
|
|
.format(
|
|
|
|
widget.entries
|
|
|
|
.where(
|
|
|
|
(element) =>
|
|
|
|
element.category.id ==
|
|
|
|
widget.categories[index].id,
|
|
|
|
)
|
|
|
|
.fold<double>(
|
|
|
|
0,
|
|
|
|
(previousValue, element) =>
|
|
|
|
previousValue + element.data.amount,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
titleStyle: TextStyle(
|
|
|
|
color:
|
|
|
|
widget.categories[index].color.calculateTextColor(),
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
),
|
|
|
|
color: widget.categories[index].color,
|
|
|
|
value: widget.entries
|
|
|
|
.where(
|
|
|
|
(element) =>
|
|
|
|
element.category.id ==
|
|
|
|
widget.categories[index].id,
|
|
|
|
)
|
|
|
|
.fold<double>(
|
|
|
|
0,
|
|
|
|
(previousValue, element) =>
|
|
|
|
previousValue + element.data.amount,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Used to indicate which part of a chart is for what
|
|
|
|
class Indicator extends StatelessWidget {
|
|
|
|
/// Used to indicate which part of a chart is for what
|
|
|
|
const Indicator({
|
|
|
|
required this.size,
|
|
|
|
required this.color,
|
|
|
|
required this.text,
|
|
|
|
this.textStyle = const TextStyle(),
|
|
|
|
super.key,
|
|
|
|
});
|
|
|
|
|
|
|
|
/// Size of the indicator circle
|
|
|
|
final double size;
|
|
|
|
|
|
|
|
/// Color of the indicator circle
|
|
|
|
final Color color;
|
|
|
|
|
|
|
|
/// Text shown next to the indicator circle
|
|
|
|
final String text;
|
|
|
|
|
|
|
|
final TextStyle textStyle;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) => Row(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
|
|
|
Container(
|
|
|
|
width: size,
|
|
|
|
height: size,
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
shape: BoxShape.circle,
|
|
|
|
color: color,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
const SizedBox(
|
|
|
|
width: 4,
|
|
|
|
),
|
|
|
|
Text(text, style: textStyle),
|
|
|
|
],
|
2023-12-29 19:43:27 +01:00
|
|
|
);
|
|
|
|
}
|