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';
|
2023-12-29 21:39:54 +01:00
|
|
|
import 'package:intl/intl.dart';
|
2024-01-08 15:38:31 +01:00
|
|
|
import 'package:prasule/api/category.dart';
|
2024-01-08 21:19:15 +01:00
|
|
|
import 'package:prasule/api/wallet_entry.dart';
|
2024-01-08 15:38:31 +01:00
|
|
|
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(
|
2024-01-29 23:51:25 +01:00
|
|
|
tooltipBgColor: Theme.of(context).colorScheme.secondaryContainer,
|
2023-12-25 19:03:52 +01:00
|
|
|
getTooltipItems: (spots) => List<LineTooltipItem>.generate(
|
|
|
|
spots.length,
|
|
|
|
(index) => LineTooltipItem(
|
2024-01-08 15:38:31 +01:00
|
|
|
// Changes what's rendered on the tooltip
|
|
|
|
// when clicked in the chart
|
2024-02-10 13:11:22 +01:00
|
|
|
(spots[index].barIndex == 0 &&
|
|
|
|
incomeData.isNotEmpty) // 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:38:31 +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:38:31 +01:00
|
|
|
children: [
|
2024-02-10 13:11:22 +01:00
|
|
|
if (!yearly)
|
|
|
|
TextSpan(
|
|
|
|
text:
|
|
|
|
"\n${DateFormat.yMMMMd(locale).format(DateTime(date.year, date.month, spots[index].spotIndex + 1))}",
|
|
|
|
),
|
2024-01-08 15:38:31 +01:00
|
|
|
],
|
2023-12-25 19:03:52 +01:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2023-11-22 15:09:05 +01:00
|
|
|
maxY: maxY,
|
2024-01-08 15:38:31 +01:00
|
|
|
maxX: yearly
|
2024-02-10 13:11:22 +01:00
|
|
|
? 11
|
2024-01-08 15:38:31 +01:00
|
|
|
: 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
|
|
|
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-22 15:15:10 +01:00
|
|
|
color: ((MediaQuery.of(context).platformBrightness ==
|
|
|
|
Brightness.dark)
|
2024-01-08 15:38:31 +01:00
|
|
|
? Colors.green.shade300
|
2024-01-22 15:15:10 +01:00
|
|
|
: Colors.green)
|
|
|
|
.harmonizeWith(Theme.of(context).colorScheme.primary),
|
2023-11-22 15:09:05 +01:00
|
|
|
spots: List.generate(
|
2024-01-08 15:38:31 +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-22 15:15:10 +01:00
|
|
|
color: ((MediaQuery.of(context).platformBrightness ==
|
|
|
|
Brightness.dark)
|
2024-01-08 15:38:31 +01:00
|
|
|
? Colors.red.shade300
|
2024-01-22 15:15:10 +01:00
|
|
|
: Colors.red)
|
|
|
|
.harmonizeWith(Theme.of(context).colorScheme.primary),
|
2023-11-22 15:09:05 +01:00
|
|
|
spots: List.generate(
|
2024-01-08 15:38:31 +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:38:31 +01:00
|
|
|
leftTitles: AxisTitles(
|
|
|
|
sideTitles: SideTitles(
|
2024-02-09 17:07:50 +01:00
|
|
|
reservedSize: ((expenseDataSorted.isNotEmpty &&
|
|
|
|
NumberFormat.compact(locale: locale)
|
|
|
|
.format(expenseDataSorted.last)
|
|
|
|
.length >=
|
|
|
|
5) ||
|
|
|
|
(incomeDataSorted.isNotEmpty &&
|
|
|
|
NumberFormat.compact(locale: locale)
|
|
|
|
.format(incomeDataSorted.last)
|
|
|
|
.length >=
|
|
|
|
5))
|
2024-01-08 15:38:31 +01:00
|
|
|
? 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(
|
2024-02-10 13:11:22 +01:00
|
|
|
(rodIndex == 1 || incomeData.isEmpty) // expense
|
2023-12-25 19:03:52 +01:00
|
|
|
? 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(
|
2024-02-10 13:11:22 +01:00
|
|
|
// income
|
2023-12-25 19:03:52 +01:00
|
|
|
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:38:31 +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:38:31 +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:38:31 +01:00
|
|
|
.harmonizeWith(Theme.of(context).colorScheme.primary),
|
2023-12-25 19:03:52 +01:00
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2024-01-08 15:38:31 +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,
|
2024-02-10 13:11:22 +01:00
|
|
|
required this.locale,
|
2024-01-08 15:38:31 +01:00
|
|
|
super.key,
|
|
|
|
});
|
|
|
|
|
|
|
|
/// Entries to be used
|
|
|
|
final List<WalletSingleEntry> entries;
|
|
|
|
|
|
|
|
/// Categories to be displayed
|
|
|
|
final List<WalletCategory> categories;
|
|
|
|
|
|
|
|
/// Currency symbol displayed on the chart
|
|
|
|
final String symbol;
|
|
|
|
|
2024-02-10 13:11:22 +01:00
|
|
|
/// User locale
|
|
|
|
final String locale;
|
|
|
|
|
2024-01-08 15:38:31 +01:00
|
|
|
@override
|
|
|
|
State<CategoriesPieChart> createState() => _CategoriesPieChartState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _CategoriesPieChartState extends State<CategoriesPieChart> {
|
|
|
|
int touchedIndex = -1;
|
|
|
|
|
|
|
|
@override
|
|
|
|
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,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
const SizedBox(
|
|
|
|
height: 5,
|
|
|
|
),
|
2024-01-29 23:51:25 +01:00
|
|
|
LimitedBox(
|
|
|
|
maxHeight: MediaQuery.of(context).size.height * 0.23,
|
|
|
|
maxWidth: MediaQuery.of(context).size.width * 0.9,
|
2024-01-08 15:38:31 +01:00
|
|
|
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(
|
2024-02-10 13:11:22 +01:00
|
|
|
title: NumberFormat.compactCurrency(
|
|
|
|
symbol: widget.symbol,
|
|
|
|
locale: widget.locale,
|
|
|
|
).format(
|
2024-01-08 15:38:31 +01:00
|
|
|
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,
|
2024-02-10 13:11:22 +01:00
|
|
|
backgroundColor: widget.categories[index].color,
|
2024-01-08 15:38:31 +01:00
|
|
|
),
|
|
|
|
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;
|
|
|
|
|
2024-01-22 14:41:16 +01:00
|
|
|
/// Text style of the indicator
|
2024-01-08 15:38:31 +01:00
|
|
|
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),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|