2025-08-15 23:53:05 +07:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:line_icons/line_icons.dart';
|
2025-08-17 23:11:31 +07:00
|
|
|
import 'package:intl/intl.dart';
|
2025-08-15 23:53:05 +07:00
|
|
|
|
2025-08-17 23:11:31 +07:00
|
|
|
import '../../../../common/extension/extension.dart';
|
2025-08-15 23:53:05 +07:00
|
|
|
import '../../../../common/theme/theme.dart';
|
2025-08-17 23:11:31 +07:00
|
|
|
import '../../../../domain/analytic/analytic.dart';
|
|
|
|
|
import '../../../components/widgets/empty_widget.dart';
|
2025-08-15 23:53:05 +07:00
|
|
|
|
|
|
|
|
class FinanceCategory extends StatelessWidget {
|
2025-08-17 23:11:31 +07:00
|
|
|
final List<CategoryAnalyticItem> categories;
|
|
|
|
|
|
|
|
|
|
const FinanceCategory({super.key, required this.categories});
|
2025-08-15 23:53:05 +07:00
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2025-08-17 23:11:31 +07:00
|
|
|
final totalRevenue = _calculateTotalRevenue();
|
|
|
|
|
final sortedCategories = _sortCategoriesByRevenue();
|
2025-08-15 23:53:05 +07:00
|
|
|
|
|
|
|
|
return Container(
|
|
|
|
|
margin: const EdgeInsets.all(16),
|
|
|
|
|
padding: const EdgeInsets.all(20),
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
color: AppColor.white,
|
|
|
|
|
borderRadius: BorderRadius.circular(16),
|
|
|
|
|
boxShadow: [
|
|
|
|
|
BoxShadow(
|
|
|
|
|
color: AppColor.textLight.withOpacity(0.1),
|
|
|
|
|
spreadRadius: 1,
|
|
|
|
|
blurRadius: 8,
|
|
|
|
|
offset: const Offset(0, 2),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
child: Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
|
|
|
|
Row(
|
|
|
|
|
children: [
|
|
|
|
|
Container(
|
|
|
|
|
padding: const EdgeInsets.all(8),
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
color: AppColor.secondary.withOpacity(0.1),
|
|
|
|
|
borderRadius: BorderRadius.circular(8),
|
|
|
|
|
),
|
|
|
|
|
child: const Icon(
|
|
|
|
|
LineIcons.pieChart,
|
|
|
|
|
color: AppColor.secondary,
|
|
|
|
|
size: 20,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(width: 12),
|
|
|
|
|
Text(
|
2025-08-20 13:52:49 +07:00
|
|
|
context.lang.sales_category,
|
2025-08-15 23:53:05 +07:00
|
|
|
style: AppStyle.lg.copyWith(fontWeight: FontWeight.bold),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 20),
|
|
|
|
|
|
2025-08-17 23:11:31 +07:00
|
|
|
// Show empty state if no categories
|
|
|
|
|
if (categories.isEmpty)
|
2025-08-20 13:52:49 +07:00
|
|
|
_buildEmptyState(context)
|
2025-08-17 23:11:31 +07:00
|
|
|
else
|
|
|
|
|
...sortedCategories.asMap().entries.map(
|
|
|
|
|
(entry) => _buildCategoryItem(
|
2025-08-20 13:52:49 +07:00
|
|
|
context,
|
2025-08-17 23:11:31 +07:00
|
|
|
entry.value,
|
|
|
|
|
_calculatePercentage(entry.value.totalRevenue, totalRevenue),
|
|
|
|
|
_getCategoryColor(entry.key),
|
|
|
|
|
),
|
|
|
|
|
),
|
2025-08-15 23:53:05 +07:00
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget _buildCategoryItem(
|
2025-08-20 13:52:49 +07:00
|
|
|
BuildContext context,
|
2025-08-17 23:11:31 +07:00
|
|
|
CategoryAnalyticItem category,
|
|
|
|
|
double percentage,
|
2025-08-15 23:53:05 +07:00
|
|
|
Color color,
|
|
|
|
|
) {
|
|
|
|
|
return Container(
|
|
|
|
|
margin: const EdgeInsets.only(bottom: 16),
|
|
|
|
|
child: Column(
|
|
|
|
|
children: [
|
|
|
|
|
Row(
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
|
children: [
|
2025-08-17 23:11:31 +07:00
|
|
|
Expanded(
|
|
|
|
|
child: Row(
|
|
|
|
|
children: [
|
|
|
|
|
Container(
|
|
|
|
|
width: 12,
|
|
|
|
|
height: 12,
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
color: color,
|
|
|
|
|
borderRadius: BorderRadius.circular(6),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(width: 12),
|
|
|
|
|
Expanded(
|
|
|
|
|
child: Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
|
|
|
|
Text(
|
|
|
|
|
category.categoryName,
|
|
|
|
|
style: AppStyle.md.copyWith(
|
|
|
|
|
fontWeight: FontWeight.w600,
|
|
|
|
|
),
|
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 2),
|
|
|
|
|
Text(
|
2025-08-20 13:52:49 +07:00
|
|
|
'${category.productCount} ${context.lang.product} • ${category.orderCount} ${context.lang.orders}',
|
2025-08-17 23:11:31 +07:00
|
|
|
style: AppStyle.xs.copyWith(
|
|
|
|
|
color: AppColor.textSecondary,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(width: 12),
|
|
|
|
|
Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.end,
|
2025-08-15 23:53:05 +07:00
|
|
|
children: [
|
2025-08-17 23:11:31 +07:00
|
|
|
Text(
|
|
|
|
|
category.totalRevenue.currencyFormatRp,
|
|
|
|
|
style: AppStyle.md.copyWith(
|
|
|
|
|
fontWeight: FontWeight.bold,
|
2025-08-15 23:53:05 +07:00
|
|
|
color: color,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
Text(
|
2025-08-20 13:52:49 +07:00
|
|
|
'${NumberFormat('#,###', 'id_ID').format(category.totalQuantity)} ${context.lang.unit}',
|
2025-08-17 23:11:31 +07:00
|
|
|
style: AppStyle.xs.copyWith(color: AppColor.textSecondary),
|
2025-08-15 23:53:05 +07:00
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 8),
|
|
|
|
|
LinearProgressIndicator(
|
|
|
|
|
value: percentage / 100,
|
|
|
|
|
backgroundColor: AppColor.borderLight,
|
|
|
|
|
valueColor: AlwaysStoppedAnimation<Color>(color),
|
|
|
|
|
minHeight: 6,
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 4),
|
|
|
|
|
Align(
|
|
|
|
|
alignment: Alignment.centerRight,
|
|
|
|
|
child: Text(
|
2025-08-17 23:11:31 +07:00
|
|
|
'${percentage.toStringAsFixed(1)}%',
|
2025-08-15 23:53:05 +07:00
|
|
|
style: AppStyle.xs.copyWith(color: AppColor.textSecondary),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-08-17 23:11:31 +07:00
|
|
|
|
2025-08-20 13:52:49 +07:00
|
|
|
Widget _buildEmptyState(BuildContext context) {
|
2025-08-17 23:11:31 +07:00
|
|
|
return EmptyWidget(
|
2025-08-20 13:52:49 +07:00
|
|
|
title: context.lang.category_no_data,
|
|
|
|
|
message: context.lang.category_no_data_desc,
|
2025-08-17 23:11:31 +07:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Helper methods
|
|
|
|
|
int _calculateTotalRevenue() {
|
|
|
|
|
return categories.fold(0, (sum, category) => sum + category.totalRevenue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<CategoryAnalyticItem> _sortCategoriesByRevenue() {
|
|
|
|
|
final sorted = List<CategoryAnalyticItem>.from(categories);
|
|
|
|
|
sorted.sort((a, b) => b.totalRevenue.compareTo(a.totalRevenue));
|
|
|
|
|
return sorted;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double _calculatePercentage(int categoryRevenue, int totalRevenue) {
|
|
|
|
|
if (totalRevenue == 0) return 0;
|
|
|
|
|
return (categoryRevenue / totalRevenue) * 100;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Color _getCategoryColor(int index) {
|
|
|
|
|
// Predefined color palette for categories
|
|
|
|
|
const colors = [
|
|
|
|
|
AppColor.primary,
|
|
|
|
|
AppColor.secondary,
|
|
|
|
|
AppColor.success,
|
|
|
|
|
AppColor.warning,
|
|
|
|
|
AppColor.error,
|
|
|
|
|
AppColor.info,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// Generate additional colors if needed
|
|
|
|
|
if (index < colors.length) {
|
|
|
|
|
return colors[index];
|
|
|
|
|
} else {
|
|
|
|
|
// Generate colors based on index for unlimited categories
|
|
|
|
|
final hue = (index * 137.5) % 360; // Golden angle approximation
|
|
|
|
|
return HSLColor.fromAHSL(1.0, hue, 0.7, 0.5).toColor();
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-15 23:53:05 +07:00
|
|
|
}
|