2025-08-12 22:33:31 +07:00
|
|
|
import 'package:fl_chart/fl_chart.dart';
|
|
|
|
|
import 'package:flutter/material.dart';
|
2025-08-18 01:50:50 +07:00
|
|
|
import '../../../../common/extension/extension.dart';
|
2025-08-12 22:33:31 +07:00
|
|
|
import '../../../../common/theme/theme.dart';
|
2025-08-18 01:50:50 +07:00
|
|
|
import '../../../../domain/analytic/analytic.dart';
|
2025-08-12 22:33:31 +07:00
|
|
|
import '../../../components/spacer/spacer.dart';
|
2025-08-18 01:50:50 +07:00
|
|
|
import '../../../components/widgets/empty_widget.dart';
|
2025-08-12 22:33:31 +07:00
|
|
|
|
|
|
|
|
class ReportSales extends StatelessWidget {
|
2025-08-18 01:50:50 +07:00
|
|
|
final List<DashboardRecentSale> salesData;
|
|
|
|
|
|
|
|
|
|
const ReportSales({super.key, required this.salesData});
|
2025-08-12 22:33:31 +07:00
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
return Container(
|
|
|
|
|
padding: const EdgeInsets.all(24),
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
color: AppColor.surface,
|
|
|
|
|
borderRadius: BorderRadius.circular(24),
|
|
|
|
|
boxShadow: [
|
|
|
|
|
BoxShadow(
|
|
|
|
|
color: AppColor.textSecondary.withOpacity(0.1),
|
|
|
|
|
blurRadius: 20,
|
|
|
|
|
offset: const Offset(0, 8),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
child: Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
2025-08-18 01:50:50 +07:00
|
|
|
// Header Section
|
2025-08-12 22:33:31 +07:00
|
|
|
Row(
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
|
children: [
|
|
|
|
|
Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
|
|
|
|
Text(
|
2025-08-20 13:52:49 +07:00
|
|
|
context.lang.sales_chart,
|
2025-08-12 22:33:31 +07:00
|
|
|
style: AppStyle.xxl.copyWith(
|
|
|
|
|
color: AppColor.textPrimary,
|
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const SpaceHeight(4),
|
|
|
|
|
Text(
|
2025-08-18 01:50:50 +07:00
|
|
|
salesData.isEmpty
|
2025-08-20 13:52:49 +07:00
|
|
|
? context.lang.no_data_available
|
|
|
|
|
: context.lang.total_days_overview(salesData.length),
|
2025-08-12 22:33:31 +07:00
|
|
|
style: AppStyle.md.copyWith(color: AppColor.textSecondary),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
Container(
|
|
|
|
|
padding: const EdgeInsets.all(8),
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
color: AppColor.primary.withOpacity(0.1),
|
|
|
|
|
borderRadius: BorderRadius.circular(12),
|
|
|
|
|
),
|
|
|
|
|
child: const Icon(
|
|
|
|
|
Icons.show_chart,
|
|
|
|
|
color: AppColor.primary,
|
|
|
|
|
size: 24,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
2025-08-18 01:50:50 +07:00
|
|
|
|
2025-08-12 22:33:31 +07:00
|
|
|
const SpaceHeight(20),
|
|
|
|
|
|
2025-08-18 01:50:50 +07:00
|
|
|
// Sales Summary Cards
|
|
|
|
|
if (salesData.isNotEmpty) ...[
|
2025-08-20 13:52:49 +07:00
|
|
|
_buildSalesSummary(context),
|
2025-08-18 01:50:50 +07:00
|
|
|
const SpaceHeight(20),
|
|
|
|
|
],
|
|
|
|
|
|
2025-08-12 22:33:31 +07:00
|
|
|
// Chart Container
|
2025-08-18 01:50:50 +07:00
|
|
|
salesData.isEmpty
|
2025-08-20 13:52:49 +07:00
|
|
|
? _buildEmptyChart(context)
|
2025-08-18 01:50:50 +07:00
|
|
|
: Container(
|
|
|
|
|
height: 300,
|
|
|
|
|
padding: const EdgeInsets.all(16),
|
|
|
|
|
decoration: BoxDecoration(
|
2025-08-12 22:33:31 +07:00
|
|
|
gradient: LinearGradient(
|
2025-08-18 01:50:50 +07:00
|
|
|
colors: [
|
|
|
|
|
AppColor.primary.withOpacity(0.05),
|
|
|
|
|
AppColor.backgroundLight,
|
|
|
|
|
],
|
|
|
|
|
begin: Alignment.topCenter,
|
|
|
|
|
end: Alignment.bottomCenter,
|
2025-08-12 22:33:31 +07:00
|
|
|
),
|
2025-08-18 01:50:50 +07:00
|
|
|
borderRadius: BorderRadius.circular(16),
|
|
|
|
|
border: Border.all(
|
|
|
|
|
color: AppColor.primary.withOpacity(0.1),
|
|
|
|
|
width: 2,
|
2025-08-12 22:33:31 +07:00
|
|
|
),
|
|
|
|
|
),
|
2025-08-18 01:50:50 +07:00
|
|
|
child: _buildSalesChart(),
|
2025-08-12 22:33:31 +07:00
|
|
|
),
|
|
|
|
|
|
|
|
|
|
const SpaceHeight(16),
|
|
|
|
|
|
|
|
|
|
// Legend
|
2025-08-18 01:50:50 +07:00
|
|
|
if (salesData.isNotEmpty)
|
|
|
|
|
Row(
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
2025-08-20 13:52:49 +07:00
|
|
|
children: [
|
|
|
|
|
_buildLegendItem(context.lang.sales_data, AppColor.primary),
|
|
|
|
|
],
|
2025-08-18 01:50:50 +07:00
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-20 13:52:49 +07:00
|
|
|
Widget _buildSalesSummary(BuildContext context) {
|
2025-08-18 01:50:50 +07:00
|
|
|
final totalSales = salesData.fold<int>(0, (sum, item) => sum + item.sales);
|
|
|
|
|
final totalOrders = salesData.fold<int>(
|
|
|
|
|
0,
|
|
|
|
|
(sum, item) => sum + item.orders,
|
|
|
|
|
);
|
|
|
|
|
final totalItems = salesData.fold<int>(0, (sum, item) => sum + item.items);
|
|
|
|
|
final totalNetSales = salesData.fold<int>(
|
|
|
|
|
0,
|
|
|
|
|
(sum, item) => sum + item.netSales,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return Container(
|
|
|
|
|
padding: const EdgeInsets.all(16),
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
color: AppColor.backgroundLight,
|
|
|
|
|
borderRadius: BorderRadius.circular(16),
|
|
|
|
|
border: Border.all(color: AppColor.border.withOpacity(0.3), width: 1),
|
|
|
|
|
),
|
|
|
|
|
child: Column(
|
|
|
|
|
children: [
|
2025-08-12 22:33:31 +07:00
|
|
|
Row(
|
|
|
|
|
children: [
|
2025-08-18 01:50:50 +07:00
|
|
|
Expanded(
|
|
|
|
|
child: _buildSummaryItem(
|
2025-08-20 13:52:49 +07:00
|
|
|
context.lang.total_sales,
|
2025-08-18 01:50:50 +07:00
|
|
|
totalSales.currencyFormatRp,
|
|
|
|
|
Icons.attach_money,
|
|
|
|
|
AppColor.success,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
Container(
|
|
|
|
|
width: 1,
|
|
|
|
|
height: 40,
|
|
|
|
|
color: AppColor.border.withOpacity(0.3),
|
|
|
|
|
margin: const EdgeInsets.symmetric(horizontal: 16),
|
|
|
|
|
),
|
|
|
|
|
Expanded(
|
|
|
|
|
child: _buildSummaryItem(
|
2025-08-20 13:52:49 +07:00
|
|
|
context.lang.net_sales,
|
2025-08-18 01:50:50 +07:00
|
|
|
totalNetSales.currencyFormatRp,
|
|
|
|
|
Icons.trending_up,
|
|
|
|
|
AppColor.primary,
|
|
|
|
|
),
|
|
|
|
|
),
|
2025-08-12 22:33:31 +07:00
|
|
|
],
|
|
|
|
|
),
|
2025-08-18 01:50:50 +07:00
|
|
|
const SpaceHeight(16),
|
|
|
|
|
Row(
|
|
|
|
|
children: [
|
|
|
|
|
Expanded(
|
|
|
|
|
child: _buildSummaryItem(
|
2025-08-20 13:52:49 +07:00
|
|
|
context.lang.total_orders,
|
2025-08-18 01:50:50 +07:00
|
|
|
totalOrders.toString(),
|
|
|
|
|
Icons.shopping_cart,
|
|
|
|
|
AppColor.info,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
Container(
|
|
|
|
|
width: 1,
|
|
|
|
|
height: 40,
|
|
|
|
|
color: AppColor.border.withOpacity(0.3),
|
|
|
|
|
margin: const EdgeInsets.symmetric(horizontal: 16),
|
|
|
|
|
),
|
|
|
|
|
Expanded(
|
|
|
|
|
child: _buildSummaryItem(
|
2025-08-20 13:52:49 +07:00
|
|
|
context.lang.total_items,
|
2025-08-18 01:50:50 +07:00
|
|
|
totalItems.toString(),
|
|
|
|
|
Icons.inventory,
|
|
|
|
|
AppColor.warning,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget _buildSummaryItem(
|
|
|
|
|
String label,
|
|
|
|
|
String value,
|
|
|
|
|
IconData icon,
|
|
|
|
|
Color color,
|
|
|
|
|
) {
|
|
|
|
|
return Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
|
|
|
|
Row(
|
|
|
|
|
children: [
|
|
|
|
|
Icon(icon, size: 16, color: color),
|
|
|
|
|
const SpaceWidth(6),
|
2025-08-20 13:52:49 +07:00
|
|
|
Expanded(
|
|
|
|
|
child: Text(
|
|
|
|
|
label,
|
|
|
|
|
style: AppStyle.xs.copyWith(
|
|
|
|
|
color: AppColor.textSecondary,
|
|
|
|
|
fontWeight: FontWeight.w500,
|
|
|
|
|
),
|
|
|
|
|
maxLines: 1,
|
|
|
|
|
overflow: TextOverflow.ellipsis,
|
2025-08-18 01:50:50 +07:00
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
const SpaceHeight(6),
|
|
|
|
|
Text(
|
|
|
|
|
value,
|
|
|
|
|
style: AppStyle.md.copyWith(
|
|
|
|
|
color: color,
|
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
|
),
|
|
|
|
|
maxLines: 1,
|
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget _buildSalesChart() {
|
|
|
|
|
final maxValue = _getMaxValue();
|
|
|
|
|
final spots = _generateSpots(salesData);
|
|
|
|
|
|
|
|
|
|
return LineChart(
|
|
|
|
|
LineChartData(
|
|
|
|
|
gridData: FlGridData(
|
|
|
|
|
show: true,
|
|
|
|
|
drawHorizontalLine: true,
|
|
|
|
|
drawVerticalLine: false,
|
|
|
|
|
horizontalInterval: maxValue / 5,
|
|
|
|
|
getDrawingHorizontalLine: (value) {
|
|
|
|
|
return FlLine(
|
|
|
|
|
color: AppColor.border.withOpacity(0.3),
|
|
|
|
|
strokeWidth: 1,
|
|
|
|
|
dashArray: [5, 5],
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
titlesData: FlTitlesData(
|
|
|
|
|
leftTitles: AxisTitles(
|
|
|
|
|
sideTitles: SideTitles(
|
|
|
|
|
showTitles: true,
|
|
|
|
|
reservedSize: 70,
|
|
|
|
|
getTitlesWidget: (value, meta) {
|
|
|
|
|
return Text(
|
|
|
|
|
_formatCurrency(value),
|
|
|
|
|
style: AppStyle.xs.copyWith(
|
|
|
|
|
color: AppColor.textSecondary,
|
|
|
|
|
fontWeight: FontWeight.w500,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
bottomTitles: AxisTitles(
|
|
|
|
|
sideTitles: SideTitles(
|
|
|
|
|
showTitles: true,
|
|
|
|
|
reservedSize: 32,
|
|
|
|
|
getTitlesWidget: (value, meta) {
|
|
|
|
|
final index = value.toInt();
|
|
|
|
|
if (index >= 0 && index < salesData.length) {
|
|
|
|
|
final date = DateTime.parse(salesData[index].date);
|
|
|
|
|
final dayName = _getDayName(date.weekday);
|
|
|
|
|
return Padding(
|
|
|
|
|
padding: const EdgeInsets.only(top: 8),
|
|
|
|
|
child: Text(
|
|
|
|
|
dayName,
|
|
|
|
|
style: AppStyle.xs.copyWith(
|
|
|
|
|
color: AppColor.textSecondary,
|
|
|
|
|
fontWeight: FontWeight.w500,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return const Text('');
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
rightTitles: AxisTitles(sideTitles: SideTitles(showTitles: false)),
|
|
|
|
|
topTitles: AxisTitles(sideTitles: SideTitles(showTitles: false)),
|
|
|
|
|
),
|
|
|
|
|
borderData: FlBorderData(show: false),
|
|
|
|
|
minX: 0,
|
|
|
|
|
maxX: (salesData.length - 1).toDouble(),
|
|
|
|
|
minY: 0,
|
|
|
|
|
maxY: maxValue,
|
|
|
|
|
lineBarsData: [
|
|
|
|
|
// Main sales line
|
|
|
|
|
LineChartBarData(
|
|
|
|
|
spots: spots,
|
|
|
|
|
isCurved: true,
|
|
|
|
|
curveSmoothness: 0.35,
|
|
|
|
|
gradient: LinearGradient(
|
|
|
|
|
colors: [AppColor.primary, AppColor.primaryLight],
|
|
|
|
|
begin: Alignment.centerLeft,
|
|
|
|
|
end: Alignment.centerRight,
|
|
|
|
|
),
|
|
|
|
|
barWidth: 4,
|
|
|
|
|
isStrokeCapRound: true,
|
|
|
|
|
belowBarData: BarAreaData(
|
|
|
|
|
show: true,
|
|
|
|
|
gradient: LinearGradient(
|
|
|
|
|
colors: [
|
|
|
|
|
AppColor.primary.withOpacity(0.3),
|
|
|
|
|
AppColor.primary.withOpacity(0.1),
|
|
|
|
|
Colors.transparent,
|
|
|
|
|
],
|
|
|
|
|
begin: Alignment.topCenter,
|
|
|
|
|
end: Alignment.bottomCenter,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
dotData: FlDotData(
|
|
|
|
|
show: true,
|
|
|
|
|
getDotPainter: (spot, percent, barData, index) {
|
|
|
|
|
return FlDotCirclePainter(
|
|
|
|
|
radius: 6,
|
|
|
|
|
color: AppColor.surface,
|
|
|
|
|
strokeWidth: 3,
|
|
|
|
|
strokeColor: AppColor.primary,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
),
|
2025-08-12 22:33:31 +07:00
|
|
|
],
|
2025-08-18 01:50:50 +07:00
|
|
|
lineTouchData: LineTouchData(
|
|
|
|
|
enabled: true,
|
|
|
|
|
touchTooltipData: LineTouchTooltipData(
|
|
|
|
|
tooltipPadding: const EdgeInsets.all(12),
|
|
|
|
|
getTooltipItems: (List<LineBarSpot> touchedBarSpots) {
|
|
|
|
|
return touchedBarSpots
|
|
|
|
|
.map((barSpot) {
|
|
|
|
|
final index = barSpot.x.toInt();
|
|
|
|
|
|
|
|
|
|
if (index >= 0 && index < salesData.length) {
|
|
|
|
|
final sale = salesData[index];
|
|
|
|
|
final date = DateTime.parse(sale.date);
|
|
|
|
|
final dayName = _getDayName(date.weekday);
|
|
|
|
|
|
|
|
|
|
return LineTooltipItem(
|
|
|
|
|
'$dayName\n',
|
|
|
|
|
const TextStyle(
|
|
|
|
|
color: AppColor.textWhite,
|
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
|
fontSize: 14,
|
|
|
|
|
),
|
|
|
|
|
children: [
|
|
|
|
|
TextSpan(
|
|
|
|
|
text: 'Sales: ${sale.sales.currencyFormatRp}\n',
|
|
|
|
|
style: AppStyle.sm.copyWith(
|
|
|
|
|
color: AppColor.textWhite,
|
|
|
|
|
fontWeight: FontWeight.w500,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
TextSpan(
|
|
|
|
|
text: 'Orders: ${sale.orders}\n',
|
|
|
|
|
style: AppStyle.sm.copyWith(
|
|
|
|
|
color: AppColor.textWhite,
|
|
|
|
|
fontWeight: FontWeight.w400,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
TextSpan(
|
|
|
|
|
text: 'Net: ${sale.netSales.currencyFormatRp}',
|
|
|
|
|
style: AppStyle.sm.copyWith(
|
|
|
|
|
color: AppColor.textWhite,
|
|
|
|
|
fontWeight: FontWeight.w400,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
})
|
|
|
|
|
.where((item) => item != null)
|
|
|
|
|
.cast<LineTooltipItem>()
|
|
|
|
|
.toList();
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
touchCallback:
|
|
|
|
|
(FlTouchEvent event, LineTouchResponse? touchResponse) {
|
|
|
|
|
// Handle touch events here if needed
|
|
|
|
|
},
|
|
|
|
|
handleBuiltInTouches: true,
|
|
|
|
|
),
|
2025-08-12 22:33:31 +07:00
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-20 13:52:49 +07:00
|
|
|
Widget _buildEmptyChart(BuildContext context) {
|
2025-08-18 01:50:50 +07:00
|
|
|
return EmptyWidget(
|
2025-08-20 13:52:49 +07:00
|
|
|
title: context.lang.no_sales_data,
|
|
|
|
|
message: context.lang.no_sales_data_desc,
|
2025-08-18 01:50:50 +07:00
|
|
|
emptyIcon: Icons.show_chart,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<FlSpot> _generateSpots(List<DashboardRecentSale> data) {
|
|
|
|
|
return data.asMap().entries.map((entry) {
|
|
|
|
|
final index = entry.key;
|
|
|
|
|
final sale = entry.value;
|
|
|
|
|
return FlSpot(index.toDouble(), sale.sales.toDouble());
|
|
|
|
|
}).toList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double _getMaxValue() {
|
|
|
|
|
if (salesData.isEmpty) return 1000000;
|
|
|
|
|
|
|
|
|
|
double maxValue = salesData
|
|
|
|
|
.map((e) => e.sales.toDouble())
|
|
|
|
|
.reduce((a, b) => a > b ? a : b);
|
|
|
|
|
|
|
|
|
|
// Add 20% padding to max value
|
|
|
|
|
return maxValue * 1.2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String _formatCurrency(double value) {
|
|
|
|
|
if (value >= 1000000) {
|
|
|
|
|
return '${(value / 1000000).toStringAsFixed(1)}M';
|
|
|
|
|
} else if (value >= 1000) {
|
|
|
|
|
return '${(value / 1000).toStringAsFixed(0)}K';
|
|
|
|
|
}
|
|
|
|
|
return value.toStringAsFixed(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String _getDayName(int weekday) {
|
|
|
|
|
const days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
|
|
|
|
|
return days[weekday - 1];
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-12 22:33:31 +07:00
|
|
|
Widget _buildLegendItem(String label, Color color) {
|
|
|
|
|
return Row(
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
|
children: [
|
|
|
|
|
Container(
|
|
|
|
|
width: 16,
|
|
|
|
|
height: 3,
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
color: color,
|
|
|
|
|
borderRadius: BorderRadius.circular(2),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const SpaceWidth(8),
|
|
|
|
|
Text(
|
|
|
|
|
label,
|
|
|
|
|
style: AppStyle.sm.copyWith(
|
|
|
|
|
color: AppColor.textSecondary,
|
|
|
|
|
fontWeight: FontWeight.w500,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|