Improved category with colors
This commit is contained in:
@@ -6,12 +6,33 @@ import 'package:equatable/equatable.dart';
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:tunas/repositories/account/account_repository.dart';
|
||||
import 'package:tunas/repositories/account/models/account.dart';
|
||||
import 'package:tunas/repositories/account/models/category.dart';
|
||||
import 'package:tunas/repositories/account/models/transaction.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
part 'account_event.dart';
|
||||
part 'account_state.dart';
|
||||
|
||||
final colors = [
|
||||
'FF74feff',
|
||||
'FF64c0ff',
|
||||
'FF5873fe',
|
||||
'FF4b4cff',
|
||||
'FFc0fcfd',
|
||||
'FFa7caff',
|
||||
'FF8d7efd',
|
||||
'FF7f65fe',
|
||||
'FFe7ffff',
|
||||
'FFd7dafd',
|
||||
'FFd8a6ff',
|
||||
'FFc065fe',
|
||||
'FFffffff',
|
||||
'FFffe6fe',
|
||||
'FFffbdff',
|
||||
'FFff80fe',
|
||||
];
|
||||
|
||||
class AccountBloc extends Bloc<AccountEvent, AccountState> {
|
||||
final AccountRepository _accountRepository;
|
||||
|
||||
@@ -38,6 +59,7 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
|
||||
|
||||
_onAccountImportCSV(
|
||||
AccountImportCSV event, Emitter<AccountState> emit) async {
|
||||
int colorIndex = 0;
|
||||
FilePickerResult? result = await FilePicker.platform.pickFiles();
|
||||
|
||||
final csvPath = result?.files.first.path;
|
||||
@@ -46,19 +68,39 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
|
||||
final String csvFileContent = await csv.readAsString();
|
||||
final List<List<dynamic>> csvList = const CsvToListConverter(fieldDelimiter: '|', eol: '\n').convert(csvFileContent);
|
||||
|
||||
final Map<String, Category> categoriesMap = {};
|
||||
|
||||
final transactions = csvList
|
||||
.map((line) => Transaction(
|
||||
.map((line) {
|
||||
|
||||
String? categoryLabel = line[1];
|
||||
if (categoryLabel == null || categoryLabel == '') {
|
||||
categoryLabel = 'N/A';
|
||||
}
|
||||
|
||||
if (categoriesMap[categoryLabel] == null) {
|
||||
if (categoryLabel == 'N/A') {
|
||||
categoriesMap[categoryLabel] = Category(label: 'N/A', color: 'FFFF0000' );
|
||||
} else {
|
||||
String color = colorIndex >= colors.length ? 'FF0000FF' : colors[colorIndex];
|
||||
colorIndex++;
|
||||
categoriesMap[categoryLabel] = Category(label: categoryLabel, color: color );
|
||||
}
|
||||
}
|
||||
|
||||
return Transaction(
|
||||
uuid: const Uuid().v8(),
|
||||
date: DateTime.parse(line[0]),
|
||||
category: line[1],
|
||||
category: categoryLabel,
|
||||
description: line[2],
|
||||
account: line[3],
|
||||
value: _universalConvertToDouble(line[4]))
|
||||
)
|
||||
value: _universalConvertToDouble(line[4]));
|
||||
})
|
||||
.toList();
|
||||
|
||||
await _accountRepository.deleteAccount();
|
||||
await _accountRepository.saveTransactions(transactions);
|
||||
final account = Account(transactions: transactions, categories: categoriesMap.values.toList());
|
||||
await _accountRepository.saveAccount(account);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import 'dart:ui';
|
||||
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:tunas/repositories/account/account_repository.dart';
|
||||
@@ -22,6 +24,7 @@ class CategoryBloc extends Bloc<CategoryEvent, CategoryState> {
|
||||
) {
|
||||
emit(state.copyWith(
|
||||
categories: event.categories,
|
||||
categoriesColors: { for (var category in event.categories) category.label : category.rgbToColor() }
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,19 +2,23 @@ part of 'category_bloc.dart';
|
||||
|
||||
final class CategoryState extends Equatable {
|
||||
final List<Category> categories;
|
||||
final Map<String, Color> categoriesColors;
|
||||
|
||||
const CategoryState({
|
||||
this.categories = const [],
|
||||
this.categoriesColors = const {},
|
||||
});
|
||||
|
||||
CategoryState copyWith({
|
||||
List<Category>? categories,
|
||||
Map<String, Color>? categoriesColors,
|
||||
}) {
|
||||
return CategoryState(
|
||||
categories: categories ?? this.categories,
|
||||
categoriesColors: categoriesColors ?? this.categoriesColors,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object> get props => [categories];
|
||||
List<Object> get props => [categories, categoriesColors];
|
||||
}
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
import 'dart:ui';
|
||||
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:fl_chart/fl_chart.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:tunas/domains/transaction/models/transaction_line.dart';
|
||||
import 'package:tunas/domains/charts/models/chart_item.dart';
|
||||
@@ -12,29 +9,6 @@ import 'package:tunas/repositories/account/models/transaction.dart';
|
||||
part 'chart_event.dart';
|
||||
part 'chart_state.dart';
|
||||
|
||||
final colors = [
|
||||
Colors.purple.shade300,
|
||||
Colors.purple.shade500,
|
||||
Colors.purple.shade700,
|
||||
Colors.purple.shade900,
|
||||
Colors.blue.shade300,
|
||||
Colors.blue.shade500,
|
||||
Colors.blue.shade700,
|
||||
Colors.blue.shade900,
|
||||
Colors.green.shade300,
|
||||
Colors.green.shade500,
|
||||
Colors.green.shade700,
|
||||
Colors.green.shade900,
|
||||
Colors.yellow.shade300,
|
||||
Colors.yellow.shade500,
|
||||
Colors.yellow.shade700,
|
||||
Colors.yellow.shade900,
|
||||
Colors.red.shade300,
|
||||
Colors.red.shade500,
|
||||
Colors.red.shade700,
|
||||
Colors.red.shade900,
|
||||
];
|
||||
|
||||
class ChartBloc extends Bloc<ChartEvent, ChartState> {
|
||||
final AccountRepository _accountRepository;
|
||||
|
||||
@@ -56,14 +30,18 @@ class ChartBloc extends Bloc<ChartEvent, ChartState> {
|
||||
}
|
||||
|
||||
_onNextYear(ChartNextYear event, Emitter<ChartState> emit) {
|
||||
if (state.lastDate!.year >= state.currentYear + 1) {
|
||||
ChartState localState = state.copyWith(currentYear: state.currentYear + 1);
|
||||
emit(_computeStateScopedStats(localState));
|
||||
}
|
||||
}
|
||||
|
||||
_onPreviousYear(ChartPreviousYear event, Emitter<ChartState> emit) {
|
||||
if (state.firstDate!.year <= state.currentYear - 1) {
|
||||
ChartState localState = state.copyWith(currentYear: state.currentYear - 1);
|
||||
emit(_computeStateScopedStats(localState));
|
||||
}
|
||||
}
|
||||
|
||||
ChartState _computeStateStats(ChartState state) {
|
||||
double globalTotal = 0;
|
||||
@@ -90,11 +68,11 @@ class ChartBloc extends Bloc<ChartEvent, ChartState> {
|
||||
transactionsLines.add(transactionLine);
|
||||
monthlyTotals.add(FlSpot(transactionLine.transaction.date.microsecondsSinceEpoch.toDouble(), transactionLine.subTotal));
|
||||
|
||||
if (firstDate.compareTo(transaction.date) < 0) {
|
||||
if (firstDate.compareTo(transaction.date) > 0) {
|
||||
firstDate = transaction.date;
|
||||
}
|
||||
|
||||
if (lastDate.compareTo(transaction.date) > 0) {
|
||||
if (lastDate.compareTo(transaction.date) < 0) {
|
||||
lastDate = transaction.date;
|
||||
}
|
||||
|
||||
@@ -122,8 +100,6 @@ class ChartBloc extends Bloc<ChartEvent, ChartState> {
|
||||
List<ChartItem> scopedCategoriesNegativeTotals = [];
|
||||
Map<int, FlSpot> scopedMonthlyTotals = {};
|
||||
Map<int, Map<String, double>> scopedCategoriesMonthlyTotals = {};
|
||||
Map<String, Color> categoriesColors = {};
|
||||
int colorIndex = 0;
|
||||
|
||||
for(var transaction in state.transactions) {
|
||||
double subTotal = globalTotal + transaction.value;
|
||||
@@ -148,15 +124,6 @@ class ChartBloc extends Bloc<ChartEvent, ChartState> {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (categoriesColors[transaction.category] == null) {
|
||||
if (colorIndex >= colors.length) {
|
||||
categoriesColors[transaction.category] = const Color.fromARGB(255, 234, 0, 255);
|
||||
} else {
|
||||
categoriesColors[transaction.category] = colors[colorIndex];
|
||||
}
|
||||
colorIndex++;
|
||||
}
|
||||
|
||||
if (transaction.value >= 0) {
|
||||
ChartItem? chartItem = scopedCategoriesPositiveTotals.firstWhere(
|
||||
(item) => item.label == transaction.category,
|
||||
@@ -248,7 +215,6 @@ class ChartBloc extends Bloc<ChartEvent, ChartState> {
|
||||
scopedSimplifiedCategoriesNegativeTotalsPercents: scopedSimplifiedCategoriesNegativeTotalsPercents,
|
||||
scopedMonthlyTotals: scopedMonthlyTotals.values.toList(),
|
||||
scopedCategoriesMonthlyTotals: scopedCategoriesMonthlyTotals,
|
||||
categoriesColors: categoriesColors,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,8 +29,6 @@ final class ChartState extends Equatable {
|
||||
final List<FlSpot> scopedMonthlyTotals;
|
||||
final Map<int, Map<String, double>> scopedCategoriesMonthlyTotals;
|
||||
|
||||
final Map<String, Color> categoriesColors;
|
||||
|
||||
const ChartState({
|
||||
this.transactions = const [],
|
||||
this.transactionsLines = const [],
|
||||
@@ -52,7 +50,6 @@ final class ChartState extends Equatable {
|
||||
this.scopedSimplifiedCategoriesNegativeTotalsPercents = const [],
|
||||
this.scopedMonthlyTotals = const [],
|
||||
this.scopedCategoriesMonthlyTotals = const {},
|
||||
this.categoriesColors = const {},
|
||||
});
|
||||
|
||||
ChartState copyWith({
|
||||
@@ -76,7 +73,6 @@ final class ChartState extends Equatable {
|
||||
List<ChartItem>? scopedSimplifiedCategoriesNegativeTotalsPercents,
|
||||
List<FlSpot>? scopedMonthlyTotals,
|
||||
Map<int, Map<String, double>>? scopedCategoriesMonthlyTotals,
|
||||
Map<String, Color>? categoriesColors,
|
||||
}) {
|
||||
return ChartState(
|
||||
transactions: transactions ?? this.transactions,
|
||||
@@ -99,7 +95,6 @@ final class ChartState extends Equatable {
|
||||
scopedSimplifiedCategoriesNegativeTotalsPercents: scopedSimplifiedCategoriesNegativeTotalsPercents ?? this.scopedSimplifiedCategoriesNegativeTotalsPercents,
|
||||
scopedMonthlyTotals: scopedMonthlyTotals ?? this.scopedMonthlyTotals,
|
||||
scopedCategoriesMonthlyTotals: scopedCategoriesMonthlyTotals ?? this.scopedCategoriesMonthlyTotals,
|
||||
categoriesColors: categoriesColors ?? this.categoriesColors,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -123,7 +118,6 @@ final class ChartState extends Equatable {
|
||||
scopedSimplifiedCategoriesNegativeTotalsPercents,
|
||||
scopedMonthlyTotals,
|
||||
scopedCategoriesMonthlyTotals,
|
||||
categoriesColors,
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -1,33 +1,34 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:tunas/domains/account/account_bloc.dart';
|
||||
import 'package:tunas/pages/data/widgets/categories_settings.dart';
|
||||
import 'package:tunas/pages/data/widgets/import_settings.dart';
|
||||
|
||||
class DataPage extends StatelessWidget {
|
||||
const DataPage({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<AccountBloc, AccountState>(
|
||||
builder: (context, state) => Column(
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(vertical: 9, horizontal: 10),
|
||||
margin: const EdgeInsets.symmetric(vertical: 2, horizontal: 10),
|
||||
child: const Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
FilledButton(
|
||||
onPressed: () => context.read<AccountBloc>().add(const AccountImportCSV()),
|
||||
child: const Text('Import CSV')
|
||||
Text(
|
||||
'Settings',
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.w900,
|
||||
fontSize: 35,
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: () => context.read<AccountBloc>().add(const AccountImportJSON()),
|
||||
child: const Text('Import JSON')
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: () => context.read<AccountBloc>().add(const AccountExportCSV()),
|
||||
child: const Text('Export CSV')
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: () => context.read<AccountBloc>().add(const AccountExportJSON()),
|
||||
child: const Text('Export JSON')
|
||||
),
|
||||
Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
ImportSettings(),
|
||||
CategoriesSettings()
|
||||
],
|
||||
)
|
||||
]
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
57
lib/pages/data/widgets/categories_settings.dart
Normal file
57
lib/pages/data/widgets/categories_settings.dart
Normal file
@@ -0,0 +1,57 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:tunas/domains/category/category_bloc.dart';
|
||||
import 'package:tunas/repositories/account/models/category.dart';
|
||||
|
||||
class CategoriesSettings extends StatelessWidget {
|
||||
const CategoriesSettings({super.key});
|
||||
|
||||
List<Widget> _computeCategoryList(List<Category> categories) {
|
||||
return categories.map((category) => Row(
|
||||
children: [
|
||||
Container(
|
||||
height: 10,
|
||||
width: 10,
|
||||
color: category.rgbToColor(),
|
||||
),
|
||||
Container(width: 5),
|
||||
Text(category.label),
|
||||
],
|
||||
)).toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<CategoryBloc, CategoryState>(
|
||||
builder: (context, state) => Container(
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.blue,
|
||||
borderRadius: BorderRadius.circular(5),
|
||||
),
|
||||
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 15),
|
||||
margin: const EdgeInsets.all(5),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
"Categories",
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.w900,
|
||||
fontSize: 20,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
SingleChildScrollView(
|
||||
scrollDirection: Axis.vertical,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: _computeCategoryList(state.categories),
|
||||
),
|
||||
),
|
||||
],
|
||||
)
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
54
lib/pages/data/widgets/import_settings.dart
Normal file
54
lib/pages/data/widgets/import_settings.dart
Normal file
@@ -0,0 +1,54 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:tunas/domains/account/account_bloc.dart';
|
||||
|
||||
class ImportSettings extends StatelessWidget {
|
||||
const ImportSettings({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<AccountBloc, AccountState>(
|
||||
builder: (context, state) => Container(
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.blue,
|
||||
borderRadius: BorderRadius.circular(5)
|
||||
),
|
||||
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 15),
|
||||
margin: const EdgeInsets.all(5),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
"Import",
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.w900,
|
||||
fontSize: 20,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
FilledButton(
|
||||
onPressed: () => context.read<AccountBloc>().add(const AccountImportCSV()),
|
||||
child: const Text('Import CSV')
|
||||
),
|
||||
const SizedBox(height: 5),
|
||||
FilledButton(
|
||||
onPressed: () => context.read<AccountBloc>().add(const AccountImportJSON()),
|
||||
child: const Text('Import JSON')
|
||||
),
|
||||
const SizedBox(height: 5),
|
||||
FilledButton(
|
||||
onPressed: () => context.read<AccountBloc>().add(const AccountExportCSV()),
|
||||
child: const Text('Export CSV')
|
||||
),
|
||||
const SizedBox(height: 5),
|
||||
FilledButton(
|
||||
onPressed: () => context.read<AccountBloc>().add(const AccountExportJSON()),
|
||||
child: const Text('Export JSON')
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -45,15 +45,15 @@ class StatsPage extends StatelessWidget {
|
||||
),
|
||||
SizedBox(
|
||||
height: 500,
|
||||
child: MonthlyCategoriesTotalChart(categoriesMonthlyTotals: state.scopedCategoriesMonthlyTotals, categoriesColors: state.categoriesColors)
|
||||
child: MonthlyCategoriesTotalChart(categoriesMonthlyTotals: state.scopedCategoriesMonthlyTotals)
|
||||
),
|
||||
SizedBox(
|
||||
height: 450,
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
CategoriesTotalsChart(categoriesTotals: state.scopedCategoriesPositiveTotals, categoriesTotalsPercents: state.scopedCategoriesPositiveTotalsPercents, categoriesColors: state.categoriesColors),
|
||||
CategoriesTotalsChart(categoriesTotals: state.scopedCategoriesNegativeTotals, categoriesTotalsPercents: state.scopedSimplifiedCategoriesNegativeTotalsPercents, categoriesColors: state.categoriesColors),
|
||||
CategoriesTotalsChart(categoriesTotals: state.scopedCategoriesPositiveTotals, categoriesTotalsPercents: state.scopedSimplifiedCategoriesPositiveTotalsPercents),
|
||||
CategoriesTotalsChart(categoriesTotals: state.scopedCategoriesNegativeTotals, categoriesTotalsPercents: state.scopedSimplifiedCategoriesNegativeTotalsPercents),
|
||||
],
|
||||
)
|
||||
),
|
||||
|
||||
@@ -10,7 +10,7 @@ class AccountCounter extends StatelessWidget {
|
||||
return accountsTotals.entries.toList().map((entry) => Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text("${entry.key}:"),
|
||||
Text(entry.key),
|
||||
Text(
|
||||
NumberFormat('#######.00 €', 'fr_FR').format(entry.value),
|
||||
style: TextStyle(
|
||||
|
||||
@@ -1,21 +1,22 @@
|
||||
import 'package:fl_chart/fl_chart.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:tunas/domains/category/category_bloc.dart';
|
||||
import 'package:tunas/domains/charts/models/chart_item.dart';
|
||||
|
||||
class CategoriesTotalsChart extends StatelessWidget {
|
||||
final List<ChartItem> categoriesTotals;
|
||||
final List<ChartItem> categoriesTotalsPercents;
|
||||
final Map<String, Color> categoriesColors;
|
||||
|
||||
const CategoriesTotalsChart({super.key, required this.categoriesTotals, required this.categoriesTotalsPercents, required this.categoriesColors});
|
||||
const CategoriesTotalsChart({super.key, required this.categoriesTotals, required this.categoriesTotalsPercents});
|
||||
|
||||
List<PieChartSectionData> _convertDataForChart() {
|
||||
List<PieChartSectionData> _convertDataForChart(Map<String, Color> categoriesColors) {
|
||||
return categoriesTotalsPercents
|
||||
.map((item) =>
|
||||
PieChartSectionData(
|
||||
value: item.value,
|
||||
title: item.label,
|
||||
title: NumberFormat("#0 %").format(item.value / 100),
|
||||
titleStyle: const TextStyle(
|
||||
fontSize: 15,
|
||||
fontWeight: FontWeight.w300
|
||||
@@ -28,7 +29,7 @@ class CategoriesTotalsChart extends StatelessWidget {
|
||||
.toList();
|
||||
}
|
||||
|
||||
List<Row> _computeLegend() {
|
||||
List<Row> _computeLegend(Map<String, Color> categoriesColors) {
|
||||
return categoriesTotals
|
||||
.map((item) => Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
@@ -56,7 +57,8 @@ class CategoriesTotalsChart extends StatelessWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
return BlocBuilder<CategoryBloc, CategoryState>(
|
||||
builder: (context, state) => Container(
|
||||
height: 320,
|
||||
width: 560,
|
||||
padding: const EdgeInsets.all(10),
|
||||
@@ -65,17 +67,12 @@ class CategoriesTotalsChart extends StatelessWidget {
|
||||
borderRadius: BorderRadius.circular(5),
|
||||
color: Colors.blue
|
||||
),
|
||||
child: Expanded(
|
||||
child: AspectRatio(
|
||||
aspectRatio: 1.3,
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: AspectRatio(
|
||||
aspectRatio: 1,
|
||||
child: PieChart(
|
||||
PieChartData(
|
||||
sections: _convertDataForChart(),
|
||||
sections: _convertDataForChart(state.categoriesColors),
|
||||
borderData: FlBorderData(
|
||||
show: false
|
||||
),
|
||||
@@ -83,7 +80,6 @@ class CategoriesTotalsChart extends StatelessWidget {
|
||||
sectionsSpace: 2
|
||||
)
|
||||
),
|
||||
)
|
||||
),
|
||||
Container(
|
||||
height: 300,
|
||||
@@ -98,14 +94,13 @@ class CategoriesTotalsChart extends StatelessWidget {
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: _computeLegend(),
|
||||
children: _computeLegend(state.categoriesColors),
|
||||
),
|
||||
)
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,14 @@
|
||||
import 'package:fl_chart/fl_chart.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:tunas/domains/category/category_bloc.dart';
|
||||
|
||||
class MonthlyCategoriesTotalChart extends StatelessWidget {
|
||||
final Map<int, Map<String, double>> categoriesMonthlyTotals;
|
||||
final Map<String, Color> categoriesColors;
|
||||
|
||||
const MonthlyCategoriesTotalChart({super.key, required this.categoriesMonthlyTotals, required this.categoriesColors});
|
||||
const MonthlyCategoriesTotalChart({super.key, required this.categoriesMonthlyTotals});
|
||||
|
||||
BarChartRodData _computeStack(double barsWidth, MapEntry<int, Map<String, double>> entry) {
|
||||
BarChartRodData _computeStack(double barsWidth, MapEntry<int, Map<String, double>> entry, Map<String, Color> categoriesColors) {
|
||||
var subcounter = 0.0;
|
||||
var a = entry.value.entries.map((subEntry) => BarChartRodStackItem(subcounter, subcounter += subEntry.value, categoriesColors[subEntry.key] ?? Colors.red)).toList();
|
||||
return BarChartRodData(
|
||||
@@ -19,13 +20,13 @@ class MonthlyCategoriesTotalChart extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
|
||||
List<BarChartGroupData> _computeBarGroups(double barsSpace, double barsWidth) {
|
||||
List<BarChartGroupData> _computeBarGroups(double barsSpace, double barsWidth, Map<String, Color> categoriesColors) {
|
||||
var a = categoriesMonthlyTotals.entries
|
||||
.map((entry) {
|
||||
return BarChartGroupData(
|
||||
x: entry.key,
|
||||
barsSpace: barsSpace,
|
||||
barRods: [_computeStack(barsWidth, entry)]
|
||||
barRods: [_computeStack(barsWidth, entry, categoriesColors)]
|
||||
);
|
||||
})
|
||||
.toList();
|
||||
@@ -55,7 +56,8 @@ class MonthlyCategoriesTotalChart extends StatelessWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AspectRatio(
|
||||
return BlocBuilder<CategoryBloc, CategoryState>(
|
||||
builder: (context, state) => AspectRatio(
|
||||
aspectRatio: 1.66,
|
||||
child: LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
@@ -65,7 +67,7 @@ class MonthlyCategoriesTotalChart extends StatelessWidget {
|
||||
return BarChart(
|
||||
BarChartData(
|
||||
maxY: _computeMaxValue(),
|
||||
barGroups: _computeBarGroups(barsSpace, barsWidth),
|
||||
barGroups: _computeBarGroups(barsSpace, barsWidth, state.categoriesColors),
|
||||
titlesData: FlTitlesData(
|
||||
topTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false)
|
||||
@@ -81,6 +83,7 @@ class MonthlyCategoriesTotalChart extends StatelessWidget {
|
||||
);
|
||||
},
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import 'package:tunas/repositories/account/models/transaction.dart';
|
||||
|
||||
class AccountRepository {
|
||||
String accountFile = 'tunas_main_account.json';
|
||||
Account? currentAccount;
|
||||
|
||||
final StorageClient _storageClient;
|
||||
|
||||
@@ -49,20 +50,27 @@ class AccountRepository {
|
||||
|
||||
saveAccount(Account account) async {
|
||||
await _storageClient.save(accountFile, jsonEncode(account.toJson()));
|
||||
_broadcastAccountData(account);
|
||||
}
|
||||
|
||||
saveTransactions(List<Transaction> transactions) async {
|
||||
final account = Account(transactions: transactions);
|
||||
Account? account = currentAccount;
|
||||
if (account == null) {
|
||||
throw Error();
|
||||
} else {account.transactions = transactions;
|
||||
await saveAccount(account);
|
||||
_transactionsController.add(account.transactions);
|
||||
_categoriesController.add(account.categories);
|
||||
_budgetController.add(account.budgets);
|
||||
}
|
||||
}
|
||||
|
||||
deleteAccount() async {
|
||||
await _storageClient.delete(accountFile);
|
||||
_transactionsController.add(const []);
|
||||
_categoriesController.add(const []);
|
||||
_budgetController.add(const []);
|
||||
_broadcastAccountData(Account());
|
||||
}
|
||||
|
||||
_broadcastAccountData(Account account) {
|
||||
currentAccount = account;
|
||||
_transactionsController.add(account.transactions);
|
||||
_categoriesController.add(account.categories);
|
||||
_budgetController.add(account.budgets);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import 'dart:ui';
|
||||
|
||||
class Category {
|
||||
String label;
|
||||
String color;
|
||||
@@ -18,4 +20,8 @@ class Category {
|
||||
'label': label,
|
||||
'color': color,
|
||||
};
|
||||
|
||||
Color rgbToColor() {
|
||||
return Color(int.parse(color.toUpperCase().replaceAll("#", ""), radix: 16));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user