马良AI写作初始化仓库
This commit is contained in:
253
AINoval/lib/widgets/analytics/analytics_card.dart
Normal file
253
AINoval/lib/widgets/analytics/analytics_card.dart
Normal file
@@ -0,0 +1,253 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:ainoval/utils/web_theme.dart';
|
||||
|
||||
class AnalyticsCard extends StatelessWidget {
|
||||
final String title;
|
||||
final String value;
|
||||
final double? changeValue;
|
||||
final bool? isUpTrend;
|
||||
final Widget? child;
|
||||
final String? className;
|
||||
|
||||
const AnalyticsCard({
|
||||
super.key,
|
||||
required this.title,
|
||||
required this.value,
|
||||
this.changeValue,
|
||||
this.isUpTrend,
|
||||
this.child,
|
||||
this.className,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(24),
|
||||
decoration: BoxDecoration(
|
||||
color: WebTheme.getCardColor(context),
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
border: Border.all(
|
||||
color: WebTheme.getBorderColor(context).withOpacity(0.5),
|
||||
width: 1,
|
||||
),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withOpacity(0.02),
|
||||
blurRadius: 8,
|
||||
offset: const Offset(0, 2),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildHeader(context),
|
||||
const SizedBox(height: 16),
|
||||
_buildContent(context),
|
||||
if (child != null) ...[
|
||||
const SizedBox(height: 16),
|
||||
child!,
|
||||
],
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildHeader(BuildContext context) {
|
||||
return Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
if (title.isNotEmpty)
|
||||
Flexible(
|
||||
child: Text(
|
||||
title,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
letterSpacing: 0.5,
|
||||
).copyWith(
|
||||
fontFamily: 'Inter',
|
||||
),
|
||||
),
|
||||
),
|
||||
if (changeValue != null && isUpTrend != null)
|
||||
_buildTrendIndicator(context),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTrendIndicator(BuildContext context) {
|
||||
final isUp = isUpTrend ?? true;
|
||||
final color = isUp ? Colors.green[600] : Colors.red[600];
|
||||
final backgroundColor = isUp ? Colors.green[50] : Colors.red[50];
|
||||
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: backgroundColor,
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
isUp ? Icons.trending_up : Icons.trending_down,
|
||||
size: 12,
|
||||
color: color,
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
'${changeValue!.abs().toStringAsFixed(1)}%',
|
||||
style: TextStyle(
|
||||
fontSize: 10,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: color,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildContent(BuildContext context) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
if (value.isNotEmpty)
|
||||
Text(
|
||||
value,
|
||||
style: TextStyle(
|
||||
fontSize: 32,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: WebTheme.getTextColor(context),
|
||||
height: 1.0,
|
||||
).copyWith(
|
||||
fontFamily: 'Inter',
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class AnalyticsOverviewCard extends StatelessWidget {
|
||||
final String title;
|
||||
final String value;
|
||||
final double? changeValue;
|
||||
final bool? isUpTrend;
|
||||
final IconData icon;
|
||||
final String subtitle;
|
||||
|
||||
const AnalyticsOverviewCard({
|
||||
super.key,
|
||||
required this.title,
|
||||
required this.value,
|
||||
this.changeValue,
|
||||
this.isUpTrend,
|
||||
required this.icon,
|
||||
required this.subtitle,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AnalyticsCard(
|
||||
title: title,
|
||||
value: value,
|
||||
changeValue: changeValue,
|
||||
isUpTrend: isUpTrend,
|
||||
child: IntrinsicHeight(
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(
|
||||
icon,
|
||||
size: 16,
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Flexible(
|
||||
child: Text(
|
||||
subtitle,
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
maxLines: 2,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class AnalyticsInsightCard extends StatelessWidget {
|
||||
final IconData icon;
|
||||
final String title;
|
||||
final String description;
|
||||
final Color iconColor;
|
||||
final Color backgroundColor;
|
||||
|
||||
const AnalyticsInsightCard({
|
||||
super.key,
|
||||
required this.icon,
|
||||
required this.title,
|
||||
required this.description,
|
||||
required this.iconColor,
|
||||
required this.backgroundColor,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(24),
|
||||
decoration: BoxDecoration(
|
||||
color: backgroundColor.withOpacity(0.05),
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
border: Border.all(
|
||||
color: WebTheme.getBorderColor(context).withOpacity(0.5),
|
||||
width: 1,
|
||||
),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Container(
|
||||
width: 48,
|
||||
height: 48,
|
||||
decoration: BoxDecoration(
|
||||
color: iconColor.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Icon(
|
||||
icon,
|
||||
size: 24,
|
||||
color: iconColor,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Text(
|
||||
title,
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
description,
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
height: 1.4,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
180
AINoval/lib/widgets/analytics/date_range_picker.dart
Normal file
180
AINoval/lib/widgets/analytics/date_range_picker.dart
Normal file
@@ -0,0 +1,180 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:ainoval/utils/web_theme.dart';
|
||||
|
||||
class AnalyticsDateRangePicker extends StatelessWidget {
|
||||
final DateTimeRange? dateRange;
|
||||
final Function(DateTimeRange?)? onDateRangeChanged;
|
||||
final String? placeholder;
|
||||
|
||||
const AnalyticsDateRangePicker({
|
||||
super.key,
|
||||
this.dateRange,
|
||||
this.onDateRangeChanged,
|
||||
this.placeholder,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return InkWell(
|
||||
onTap: () => _showDateRangePicker(context),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
|
||||
decoration: BoxDecoration(
|
||||
color: WebTheme.getCardColor(context),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(
|
||||
color: WebTheme.getBorderColor(context),
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.calendar_today,
|
||||
size: 16,
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
_getDisplayText(),
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String _getDisplayText() {
|
||||
if (dateRange == null) {
|
||||
return placeholder ?? '选择日期范围';
|
||||
}
|
||||
|
||||
final startDate = _formatDate(dateRange!.start);
|
||||
final endDate = _formatDate(dateRange!.end);
|
||||
return '$startDate ~ $endDate';
|
||||
}
|
||||
|
||||
String _formatDate(DateTime date) {
|
||||
return '${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}';
|
||||
}
|
||||
|
||||
Future<void> _showDateRangePicker(BuildContext context) async {
|
||||
final now = DateTime.now();
|
||||
final firstDate = DateTime(now.year - 1);
|
||||
final lastDate = DateTime(now.year + 1);
|
||||
|
||||
final picked = await showDateRangePicker(
|
||||
context: context,
|
||||
firstDate: firstDate,
|
||||
lastDate: lastDate,
|
||||
initialDateRange: dateRange ?? DateTimeRange(
|
||||
start: now.subtract(const Duration(days: 7)),
|
||||
end: now,
|
||||
),
|
||||
builder: (context, child) {
|
||||
return Theme(
|
||||
data: Theme.of(context).copyWith(
|
||||
colorScheme: Theme.of(context).colorScheme.copyWith(
|
||||
primary: Theme.of(context).primaryColor,
|
||||
surface: WebTheme.getCardColor(context),
|
||||
),
|
||||
),
|
||||
child: child!,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
if (picked != null) {
|
||||
onDateRangeChanged?.call(picked);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class AnalyticsDatePicker extends StatelessWidget {
|
||||
final DateTime? selectedDate;
|
||||
final Function(DateTime?)? onDateChanged;
|
||||
final String? placeholder;
|
||||
|
||||
const AnalyticsDatePicker({
|
||||
super.key,
|
||||
this.selectedDate,
|
||||
this.onDateChanged,
|
||||
this.placeholder,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return InkWell(
|
||||
onTap: () => _showDatePicker(context),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
|
||||
decoration: BoxDecoration(
|
||||
color: WebTheme.getCardColor(context),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(
|
||||
color: WebTheme.getBorderColor(context),
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.calendar_today,
|
||||
size: 16,
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
selectedDate != null
|
||||
? _formatDate(selectedDate!)
|
||||
: placeholder ?? '选择日期',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String _formatDate(DateTime date) {
|
||||
return '${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}';
|
||||
}
|
||||
|
||||
Future<void> _showDatePicker(BuildContext context) async {
|
||||
final now = DateTime.now();
|
||||
final firstDate = DateTime(now.year - 1);
|
||||
final lastDate = DateTime(now.year + 1);
|
||||
|
||||
final picked = await showDatePicker(
|
||||
context: context,
|
||||
initialDate: selectedDate ?? now,
|
||||
firstDate: firstDate,
|
||||
lastDate: lastDate,
|
||||
builder: (context, child) {
|
||||
return Theme(
|
||||
data: Theme.of(context).copyWith(
|
||||
colorScheme: Theme.of(context).colorScheme.copyWith(
|
||||
primary: Theme.of(context).primaryColor,
|
||||
surface: WebTheme.getCardColor(context),
|
||||
),
|
||||
),
|
||||
child: child!,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
if (picked != null) {
|
||||
onDateChanged?.call(picked);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
395
AINoval/lib/widgets/analytics/function_usage_chart.dart
Normal file
395
AINoval/lib/widgets/analytics/function_usage_chart.dart
Normal file
@@ -0,0 +1,395 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:fl_chart/fl_chart.dart';
|
||||
import 'dart:math' as math;
|
||||
import 'package:ainoval/models/analytics_data.dart';
|
||||
import 'package:ainoval/utils/web_theme.dart';
|
||||
import 'package:ainoval/widgets/analytics/date_range_picker.dart';
|
||||
|
||||
class FunctionUsageChart extends StatefulWidget {
|
||||
final List<FunctionUsageData> data;
|
||||
final AnalyticsViewMode viewMode;
|
||||
final Function(AnalyticsViewMode)? onViewModeChanged;
|
||||
final DateTimeRange? dateRange;
|
||||
final Function(DateTimeRange?)? onDateRangeChanged;
|
||||
|
||||
const FunctionUsageChart({
|
||||
super.key,
|
||||
required this.data,
|
||||
this.viewMode = AnalyticsViewMode.daily,
|
||||
this.onViewModeChanged,
|
||||
this.dateRange,
|
||||
this.onDateRangeChanged,
|
||||
});
|
||||
|
||||
@override
|
||||
State<FunctionUsageChart> createState() => _FunctionUsageChartState();
|
||||
}
|
||||
|
||||
class _FunctionUsageChartState extends State<FunctionUsageChart> {
|
||||
int touchedIndex = -1;
|
||||
|
||||
static const List<Color> colors = [
|
||||
Color(0xFF3B82F6), // blue
|
||||
Color(0xFF8B5CF6), // purple
|
||||
Color(0xFF10B981), // green
|
||||
Color(0xFFF59E0B), // yellow
|
||||
Color(0xFFEF4444), // red
|
||||
Color(0xFF06B6D4), // cyan
|
||||
];
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
children: [
|
||||
_buildControls(),
|
||||
const SizedBox(height: 24),
|
||||
_buildChart(),
|
||||
const SizedBox(height: 24),
|
||||
_buildLegend(),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildControls() {
|
||||
return Row(
|
||||
children: [
|
||||
_buildViewModeButtons(),
|
||||
const Spacer(),
|
||||
if (widget.viewMode == AnalyticsViewMode.range)
|
||||
AnalyticsDateRangePicker(
|
||||
dateRange: widget.dateRange,
|
||||
onDateRangeChanged: widget.onDateRangeChanged,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildViewModeButtons() {
|
||||
final modes = [
|
||||
AnalyticsViewMode.daily,
|
||||
AnalyticsViewMode.monthly,
|
||||
AnalyticsViewMode.range,
|
||||
];
|
||||
|
||||
return Row(
|
||||
children: modes.map((mode) {
|
||||
final isSelected = widget.viewMode == mode;
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(right: 8),
|
||||
child: InkWell(
|
||||
onTap: () => widget.onViewModeChanged?.call(mode),
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
|
||||
decoration: BoxDecoration(
|
||||
color: isSelected
|
||||
? Theme.of(context).primaryColor
|
||||
: Colors.transparent,
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
border: Border.all(
|
||||
color: isSelected
|
||||
? Theme.of(context).primaryColor
|
||||
: WebTheme.getBorderColor(context),
|
||||
),
|
||||
),
|
||||
child: Text(
|
||||
mode.displayName,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: isSelected
|
||||
? Colors.white
|
||||
: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildChart() {
|
||||
if (widget.data.isEmpty) {
|
||||
return Container(
|
||||
height: 260,
|
||||
alignment: Alignment.center,
|
||||
child: Text(
|
||||
'暂无数据',
|
||||
style: TextStyle(
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final double maxY = _getMaxY();
|
||||
final double yInterval = _getNiceGridInterval(maxY);
|
||||
|
||||
return Container(
|
||||
height: 260,
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: BarChart(
|
||||
BarChartData(
|
||||
alignment: BarChartAlignment.spaceAround,
|
||||
maxY: maxY,
|
||||
barTouchData: BarTouchData(
|
||||
enabled: true,
|
||||
touchTooltipData: BarTouchTooltipData(
|
||||
getTooltipColor: (group) => WebTheme.getCardColor(context),
|
||||
getTooltipItem: (group, groupIndex, rod, rodIndex) {
|
||||
if (groupIndex >= 0 && groupIndex < widget.data.length) {
|
||||
final data = widget.data[groupIndex];
|
||||
return BarTooltipItem(
|
||||
'${data.name}\n',
|
||||
TextStyle(
|
||||
color: WebTheme.getTextColor(context),
|
||||
fontWeight: FontWeight.w600,
|
||||
fontSize: 12,
|
||||
),
|
||||
children: [
|
||||
TextSpan(
|
||||
text: '使用次数: ${data.value.toString().replaceAllMapped(RegExp(r'(\d)(?=(\d{3})+(?!\d))'), (match) => '${match[1]},')}',
|
||||
style: TextStyle(
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
fontSize: 11,
|
||||
fontWeight: FontWeight.w400,
|
||||
),
|
||||
),
|
||||
if (data.growth != 0) TextSpan(
|
||||
text: '\n增长率: ${data.growth > 0 ? '+' : ''}${data.growth.toStringAsFixed(1)}%',
|
||||
style: TextStyle(
|
||||
color: data.growth > 0 ? Colors.green[600] : Colors.red[600],
|
||||
fontSize: 11,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
touchCallback: (FlTouchEvent event, barTouchResponse) {
|
||||
setState(() {
|
||||
if (event is FlTapUpEvent &&
|
||||
barTouchResponse != null &&
|
||||
barTouchResponse.spot != null) {
|
||||
touchedIndex = barTouchResponse.spot!.touchedBarGroupIndex;
|
||||
} else {
|
||||
touchedIndex = -1;
|
||||
}
|
||||
});
|
||||
},
|
||||
),
|
||||
titlesData: FlTitlesData(
|
||||
show: true,
|
||||
rightTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
topTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
bottomTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
reservedSize: 40,
|
||||
getTitlesWidget: (value, meta) {
|
||||
final index = value.toInt();
|
||||
if (index >= 0 && index < widget.data.length) {
|
||||
final name = widget.data[index].name;
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(top: 8.0),
|
||||
child: Text(
|
||||
name.length > 4 ? '${name.substring(0, 4)}...' : name,
|
||||
style: TextStyle(
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
fontSize: 12,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
);
|
||||
}
|
||||
return const Text('');
|
||||
},
|
||||
),
|
||||
),
|
||||
leftTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
interval: yInterval,
|
||||
reservedSize: 50,
|
||||
getTitlesWidget: (value, meta) {
|
||||
return Text(
|
||||
_formatYAxisLabel(value),
|
||||
style: TextStyle(
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
fontSize: 12,
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
borderData: FlBorderData(show: false),
|
||||
barGroups: _buildBarGroups(),
|
||||
gridData: FlGridData(
|
||||
show: true,
|
||||
drawVerticalLine: false,
|
||||
horizontalInterval: yInterval,
|
||||
getDrawingHorizontalLine: (value) => FlLine(
|
||||
color: WebTheme.getBorderColor(context).withOpacity(0.3),
|
||||
strokeWidth: 1,
|
||||
dashArray: [3, 3],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildLegend() {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16),
|
||||
child: Wrap(
|
||||
spacing: 24,
|
||||
runSpacing: 12,
|
||||
children: widget.data.asMap().entries.map((entry) {
|
||||
final index = entry.key;
|
||||
final data = entry.value;
|
||||
final color = colors[index % colors.length];
|
||||
|
||||
return ConstrainedBox(
|
||||
constraints: const BoxConstraints(minHeight: 24, maxWidth: 260),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Container(
|
||||
width: 12,
|
||||
height: 12,
|
||||
decoration: BoxDecoration(
|
||||
color: color,
|
||||
borderRadius: BorderRadius.circular(3),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
data.name,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
data.value.toString().replaceAllMapped(
|
||||
RegExp(r'(\d)(?=(\d{3})+(?!\d))'),
|
||||
(match) => '${match[1]},',
|
||||
),
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
if (data.growth != 0)
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 2),
|
||||
decoration: BoxDecoration(
|
||||
color: data.growth > 0
|
||||
? Colors.green[50]
|
||||
: Colors.red[50],
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
),
|
||||
child: Text(
|
||||
'${data.growth > 0 ? '+' : ''}${data.growth.toStringAsFixed(0)}%',
|
||||
style: TextStyle(
|
||||
fontSize: 10,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: data.growth > 0
|
||||
? Colors.green[600]
|
||||
: Colors.red[600],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
List<BarChartGroupData> _buildBarGroups() {
|
||||
return widget.data.asMap().entries.map((entry) {
|
||||
final index = entry.key;
|
||||
final data = entry.value;
|
||||
final color = colors[index % colors.length];
|
||||
final isTouched = index == touchedIndex;
|
||||
|
||||
return BarChartGroupData(
|
||||
x: index,
|
||||
barRods: [
|
||||
BarChartRodData(
|
||||
toY: data.value.toDouble(),
|
||||
color: color,
|
||||
width: isTouched ? 20 : 16,
|
||||
borderRadius: const BorderRadius.only(
|
||||
topLeft: Radius.circular(8),
|
||||
topRight: Radius.circular(8),
|
||||
),
|
||||
backDrawRodData: BackgroundBarChartRodData(
|
||||
show: true,
|
||||
toY: _getMaxY(),
|
||||
color: color.withOpacity(0.1),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}).toList();
|
||||
}
|
||||
|
||||
double _getMaxY() {
|
||||
if (widget.data.isEmpty) return 1000;
|
||||
|
||||
final maxValue = widget.data
|
||||
.map((d) => d.value)
|
||||
.reduce((a, b) => a > b ? a : b);
|
||||
|
||||
// 添加20%的padding
|
||||
final maxWithPadding = maxValue * 1.2;
|
||||
return maxWithPadding;
|
||||
}
|
||||
|
||||
// 计算漂亮的网格间隔(1/2/5 x 10^k)
|
||||
double _getNiceGridInterval(double maxY) {
|
||||
final double roughStep = (maxY <= 0 ? 1000.0 : maxY) / 5.0;
|
||||
final double magnitude = math.pow(10, (math.log(roughStep) / math.ln10).floor()).toDouble();
|
||||
final double residual = roughStep / magnitude;
|
||||
double nice;
|
||||
if (residual >= 5) {
|
||||
nice = 5;
|
||||
} else if (residual >= 2) {
|
||||
nice = 2;
|
||||
} else {
|
||||
nice = 1;
|
||||
}
|
||||
return nice * magnitude;
|
||||
}
|
||||
|
||||
String _formatYAxisLabel(double value) {
|
||||
final double absVal = value.abs();
|
||||
if (absVal >= 1000000) {
|
||||
return '${(value / 1000000).toStringAsFixed(1)}M';
|
||||
}
|
||||
if (absVal >= 1000) {
|
||||
return '${(value / 1000).toStringAsFixed(0)}K';
|
||||
}
|
||||
return value.toInt().toString();
|
||||
}
|
||||
}
|
||||
343
AINoval/lib/widgets/analytics/model_usage_chart.dart
Normal file
343
AINoval/lib/widgets/analytics/model_usage_chart.dart
Normal file
@@ -0,0 +1,343 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:fl_chart/fl_chart.dart';
|
||||
import 'package:ainoval/models/analytics_data.dart';
|
||||
import 'package:ainoval/utils/web_theme.dart';
|
||||
import 'package:ainoval/widgets/analytics/date_range_picker.dart';
|
||||
|
||||
class ModelUsageChart extends StatefulWidget {
|
||||
final List<ModelUsageData> data;
|
||||
final AnalyticsViewMode viewMode;
|
||||
final Function(AnalyticsViewMode)? onViewModeChanged;
|
||||
final DateTimeRange? dateRange;
|
||||
final Function(DateTimeRange?)? onDateRangeChanged;
|
||||
|
||||
const ModelUsageChart({
|
||||
super.key,
|
||||
required this.data,
|
||||
this.viewMode = AnalyticsViewMode.daily,
|
||||
this.onViewModeChanged,
|
||||
this.dateRange,
|
||||
this.onDateRangeChanged,
|
||||
});
|
||||
|
||||
@override
|
||||
State<ModelUsageChart> createState() => _ModelUsageChartState();
|
||||
}
|
||||
|
||||
class _ModelUsageChartState extends State<ModelUsageChart> {
|
||||
int touchedIndex = -1;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
children: [
|
||||
_buildControls(),
|
||||
const SizedBox(height: 24),
|
||||
_buildChart(),
|
||||
const SizedBox(height: 24),
|
||||
_buildLegend(),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildControls() {
|
||||
return Row(
|
||||
children: [
|
||||
_buildViewModeButtons(),
|
||||
const Spacer(),
|
||||
if (widget.viewMode == AnalyticsViewMode.range)
|
||||
AnalyticsDateRangePicker(
|
||||
dateRange: widget.dateRange,
|
||||
onDateRangeChanged: widget.onDateRangeChanged,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildViewModeButtons() {
|
||||
final modes = [
|
||||
AnalyticsViewMode.daily,
|
||||
AnalyticsViewMode.monthly,
|
||||
AnalyticsViewMode.range,
|
||||
];
|
||||
|
||||
return Row(
|
||||
children: modes.map((mode) {
|
||||
final isSelected = widget.viewMode == mode;
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(right: 8),
|
||||
child: InkWell(
|
||||
onTap: () => widget.onViewModeChanged?.call(mode),
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
|
||||
decoration: BoxDecoration(
|
||||
color: isSelected
|
||||
? Theme.of(context).primaryColor
|
||||
: Colors.transparent,
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
border: Border.all(
|
||||
color: isSelected
|
||||
? Theme.of(context).primaryColor
|
||||
: WebTheme.getBorderColor(context),
|
||||
),
|
||||
),
|
||||
child: Text(
|
||||
mode.displayName,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: isSelected
|
||||
? Colors.white
|
||||
: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildChart() {
|
||||
if (widget.data.isEmpty) {
|
||||
return Container(
|
||||
height: 260,
|
||||
alignment: Alignment.center,
|
||||
child: Text(
|
||||
'暂无数据',
|
||||
style: TextStyle(
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return Container(
|
||||
height: 260,
|
||||
child: PieChart(
|
||||
PieChartData(
|
||||
pieTouchData: PieTouchData(
|
||||
enabled: true,
|
||||
touchCallback: (FlTouchEvent event, pieTouchResponse) {
|
||||
setState(() {
|
||||
if (event is FlTapUpEvent &&
|
||||
pieTouchResponse != null &&
|
||||
pieTouchResponse.touchedSection != null) {
|
||||
touchedIndex = pieTouchResponse.touchedSection!.touchedSectionIndex;
|
||||
} else {
|
||||
touchedIndex = -1;
|
||||
}
|
||||
});
|
||||
},
|
||||
),
|
||||
borderData: FlBorderData(show: false),
|
||||
sectionsSpace: 2,
|
||||
centerSpaceRadius: 40,
|
||||
sections: _buildPieSections(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildLegend() {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16),
|
||||
child: LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final int crossAxisCount = constraints.maxWidth < 480 ? 1 : 2;
|
||||
return GridView.builder(
|
||||
shrinkWrap: true,
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: crossAxisCount,
|
||||
mainAxisExtent: 70, // 增加高度,确保有足够空间
|
||||
crossAxisSpacing: 16,
|
||||
mainAxisSpacing: 16, // 增加间距,避免溢出
|
||||
),
|
||||
itemCount: widget.data.length,
|
||||
itemBuilder: (context, index) {
|
||||
final data = widget.data[index];
|
||||
final color = Color(int.parse(data.color.substring(1, 7), radix: 16) + 0xFF000000);
|
||||
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: WebTheme.getCardColor(context).withOpacity(0.5),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(
|
||||
color: WebTheme.getBorderColor(context).withOpacity(0.3),
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Container(
|
||||
width: 16,
|
||||
height: 16,
|
||||
decoration: BoxDecoration(
|
||||
color: color,
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text(
|
||||
data.modelName,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(
|
||||
fontSize: 13, // 稍微减小字体,确保不溢出
|
||||
fontWeight: FontWeight.w500,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4), // 增加间距
|
||||
Row(
|
||||
children: [
|
||||
Text(
|
||||
'${data.percentage}%',
|
||||
style: TextStyle(
|
||||
fontSize: 15, // 稍微减小字体
|
||||
fontWeight: FontWeight.w600,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
const Spacer(),
|
||||
Text(
|
||||
'${(data.totalTokens / 1000).toStringAsFixed(0)}K',
|
||||
style: TextStyle(
|
||||
fontSize: 11, // 稍微减小字体
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
List<PieChartSectionData> _buildPieSections() {
|
||||
return widget.data.asMap().entries.map((entry) {
|
||||
final index = entry.key;
|
||||
final data = entry.value;
|
||||
final color = Color(int.parse(data.color.substring(1, 7), radix: 16) + 0xFF000000);
|
||||
final isTouched = index == touchedIndex;
|
||||
final fontSize = isTouched ? 16.0 : 14.0;
|
||||
final radius = isTouched ? 85.0 : 80.0;
|
||||
|
||||
return PieChartSectionData(
|
||||
color: color,
|
||||
value: data.percentage.toDouble(),
|
||||
title: '${data.percentage}%',
|
||||
radius: radius,
|
||||
titleStyle: TextStyle(
|
||||
fontSize: fontSize,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.white,
|
||||
shadows: [
|
||||
Shadow(
|
||||
color: Colors.black.withOpacity(0.3),
|
||||
offset: const Offset(1, 1),
|
||||
blurRadius: 2,
|
||||
),
|
||||
],
|
||||
),
|
||||
// 将标题放置在更靠内的位置,避免与边缘碰撞
|
||||
titlePositionPercentageOffset: 0.55,
|
||||
borderSide: isTouched
|
||||
? const BorderSide(color: Colors.white, width: 2)
|
||||
: BorderSide.none,
|
||||
showTitle: data.percentage >= 5, // 只有大于5%的才显示标题
|
||||
);
|
||||
}).toList();
|
||||
}
|
||||
}
|
||||
|
||||
// 数据聚合工具类
|
||||
class ModelUsageAnalytics {
|
||||
/// 根据Token使用数据按模型名聚合统计
|
||||
static List<ModelUsageData> aggregateModelUsage(List<TokenUsageData> tokenData) {
|
||||
final Map<String, int> modelTotals = {};
|
||||
int totalTokens = 0;
|
||||
|
||||
// 聚合所有模型的token使用量
|
||||
for (final data in tokenData) {
|
||||
for (final entry in data.modelTokens.entries) {
|
||||
final modelName = entry.key;
|
||||
final tokens = entry.value;
|
||||
modelTotals[modelName] = (modelTotals[modelName] ?? 0) + tokens;
|
||||
totalTokens += tokens;
|
||||
}
|
||||
}
|
||||
|
||||
if (totalTokens == 0) return [];
|
||||
|
||||
// 按使用量排序并生成ModelUsageData列表
|
||||
final sortedEntries = modelTotals.entries.toList()
|
||||
..sort((a, b) => b.value.compareTo(a.value));
|
||||
|
||||
final List<ModelUsageData> result = [];
|
||||
final colors = ['#3B82F6', '#8B5CF6', '#10B981', '#F59E0B', '#EF4444', '#06B6D4'];
|
||||
|
||||
for (int i = 0; i < sortedEntries.length; i++) {
|
||||
final entry = sortedEntries[i];
|
||||
final percentage = ((entry.value / totalTokens) * 100).round();
|
||||
|
||||
result.add(ModelUsageData(
|
||||
modelName: entry.key,
|
||||
percentage: percentage,
|
||||
totalTokens: entry.value,
|
||||
color: colors[i % colors.length],
|
||||
));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// 获取模型使用的颜色
|
||||
static String getModelColor(String modelName) {
|
||||
switch (modelName) {
|
||||
case 'GPT-4':
|
||||
return '#3B82F6';
|
||||
case 'Claude-3.5':
|
||||
return '#8B5CF6';
|
||||
case 'Gemini Pro':
|
||||
return '#10B981';
|
||||
case '其他模型':
|
||||
return '#F59E0B';
|
||||
default:
|
||||
return '#6B7280';
|
||||
}
|
||||
}
|
||||
|
||||
/// 获取模型的显示名称
|
||||
static String getModelDisplayName(String modelName) {
|
||||
switch (modelName) {
|
||||
case 'gpt-4':
|
||||
case 'gpt-4-turbo':
|
||||
return 'GPT-4';
|
||||
case 'claude-3-5-sonnet':
|
||||
case 'claude-3.5':
|
||||
return 'Claude-3.5';
|
||||
case 'gemini-pro':
|
||||
case 'gemini-1.5-pro':
|
||||
return 'Gemini Pro';
|
||||
default:
|
||||
return modelName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
470
AINoval/lib/widgets/analytics/token_usage_chart.dart
Normal file
470
AINoval/lib/widgets/analytics/token_usage_chart.dart
Normal file
@@ -0,0 +1,470 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:fl_chart/fl_chart.dart';
|
||||
import 'dart:math' as math;
|
||||
import 'package:ainoval/models/analytics_data.dart';
|
||||
import 'package:ainoval/utils/web_theme.dart';
|
||||
import 'package:ainoval/widgets/analytics/date_range_picker.dart';
|
||||
|
||||
class TokenUsageChart extends StatefulWidget {
|
||||
final List<TokenUsageData> data;
|
||||
final AnalyticsViewMode viewMode;
|
||||
final Function(AnalyticsViewMode)? onViewModeChanged;
|
||||
final DateTimeRange? dateRange;
|
||||
final Function(DateTimeRange?)? onDateRangeChanged;
|
||||
|
||||
const TokenUsageChart({
|
||||
super.key,
|
||||
required this.data,
|
||||
this.viewMode = AnalyticsViewMode.monthly,
|
||||
this.onViewModeChanged,
|
||||
this.dateRange,
|
||||
this.onDateRangeChanged,
|
||||
});
|
||||
|
||||
@override
|
||||
State<TokenUsageChart> createState() => _TokenUsageChartState();
|
||||
}
|
||||
|
||||
class _TokenUsageChartState extends State<TokenUsageChart> {
|
||||
int touchedIndex = -1;
|
||||
|
||||
List<TokenUsageData> get _sortedData {
|
||||
final List<TokenUsageData> copy = List<TokenUsageData>.from(widget.data);
|
||||
copy.sort((a, b) {
|
||||
final DateTime? da = _parseDate(a.date);
|
||||
final DateTime? db = _parseDate(b.date);
|
||||
if (da == null && db == null) return 0;
|
||||
if (da == null) return -1;
|
||||
if (db == null) return 1;
|
||||
return da.compareTo(db);
|
||||
});
|
||||
return copy;
|
||||
}
|
||||
|
||||
DateTime? _parseDate(String raw) {
|
||||
final DateTime? direct = DateTime.tryParse(raw);
|
||||
if (direct != null) return DateTime(direct.year, direct.month, direct.day);
|
||||
if (RegExp(r'^\d{1,2}-\d{1,2}$').hasMatch(raw)) {
|
||||
final parts = raw.split('-');
|
||||
final m = int.tryParse(parts[0]) ?? 1;
|
||||
final d = int.tryParse(parts[1]) ?? 1;
|
||||
final now = DateTime.now();
|
||||
return DateTime(now.year, m, d);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
children: [
|
||||
_buildControls(),
|
||||
const SizedBox(height: 24),
|
||||
_buildChart(),
|
||||
const SizedBox(height: 24),
|
||||
_buildLegend(),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildControls() {
|
||||
return Row(
|
||||
children: [
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
|
||||
decoration: BoxDecoration(
|
||||
color: WebTheme.getCardColor(context),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(
|
||||
color: WebTheme.getBorderColor(context).withOpacity(0.5),
|
||||
),
|
||||
),
|
||||
child: Text(
|
||||
'Token 使用趋势',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
),
|
||||
),
|
||||
const Spacer(),
|
||||
_buildViewModeButtons(),
|
||||
const SizedBox(width: 16),
|
||||
if (widget.viewMode == AnalyticsViewMode.range)
|
||||
AnalyticsDateRangePicker(
|
||||
dateRange: widget.dateRange,
|
||||
onDateRangeChanged: widget.onDateRangeChanged,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildViewModeButtons() {
|
||||
return Row(
|
||||
children: AnalyticsViewMode.values.map((mode) {
|
||||
final isSelected = widget.viewMode == mode;
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(right: 8),
|
||||
child: InkWell(
|
||||
onTap: () => widget.onViewModeChanged?.call(mode),
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
|
||||
decoration: BoxDecoration(
|
||||
color: isSelected
|
||||
? Theme.of(context).primaryColor
|
||||
: Colors.transparent,
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
border: Border.all(
|
||||
color: isSelected
|
||||
? Theme.of(context).primaryColor
|
||||
: WebTheme.getBorderColor(context),
|
||||
),
|
||||
),
|
||||
child: Text(
|
||||
mode.displayName,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: isSelected
|
||||
? Colors.white
|
||||
: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildChart() {
|
||||
if (widget.data.isEmpty) {
|
||||
return Container(
|
||||
height: 320,
|
||||
alignment: Alignment.center,
|
||||
child: Text(
|
||||
'暂无数据',
|
||||
style: TextStyle(
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final double maxY = _getMaxY();
|
||||
final double yInterval = _getNiceGridInterval(maxY);
|
||||
final double xInterval = _computeXLabelInterval(_sortedData.length).toDouble();
|
||||
|
||||
return Container(
|
||||
height: 320,
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: LineChart(
|
||||
LineChartData(
|
||||
gridData: FlGridData(
|
||||
show: true,
|
||||
drawVerticalLine: false,
|
||||
horizontalInterval: yInterval,
|
||||
getDrawingHorizontalLine: (value) => FlLine(
|
||||
color: WebTheme.getBorderColor(context).withOpacity(0.3),
|
||||
strokeWidth: 1,
|
||||
dashArray: [3, 3],
|
||||
),
|
||||
),
|
||||
titlesData: FlTitlesData(
|
||||
show: true,
|
||||
rightTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
topTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
bottomTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
reservedSize: 30,
|
||||
interval: xInterval,
|
||||
getTitlesWidget: (value, meta) {
|
||||
final index = value.toInt();
|
||||
if (index >= 0 && index < _sortedData.length) {
|
||||
final date = _sortedData[index].date;
|
||||
final label = _formatXAxisLabel(widget.viewMode, date);
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(top: 8.0),
|
||||
child: Text(
|
||||
label,
|
||||
style: TextStyle(
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
return const Text('');
|
||||
},
|
||||
),
|
||||
),
|
||||
leftTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
interval: yInterval,
|
||||
reservedSize: 50,
|
||||
getTitlesWidget: (value, meta) {
|
||||
return Text(
|
||||
_formatYAxisLabel(value),
|
||||
style: TextStyle(
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
fontSize: 12,
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
borderData: FlBorderData(show: false),
|
||||
minX: 0,
|
||||
maxX: (_sortedData.length - 1).toDouble(),
|
||||
minY: 0,
|
||||
maxY: maxY,
|
||||
lineBarsData: [
|
||||
// 输入Token线
|
||||
LineChartBarData(
|
||||
spots: _getInputSpots(),
|
||||
isCurved: true,
|
||||
color: const Color(0xFF3B82F6),
|
||||
barWidth: 3,
|
||||
isStrokeCapRound: true,
|
||||
dotData: const FlDotData(show: false),
|
||||
belowBarData: BarAreaData(
|
||||
show: true,
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topCenter,
|
||||
end: Alignment.bottomCenter,
|
||||
colors: [
|
||||
const Color(0xFF3B82F6).withOpacity(0.3),
|
||||
const Color(0xFF3B82F6).withOpacity(0.0),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
// 输出Token线
|
||||
LineChartBarData(
|
||||
spots: _getOutputSpots(),
|
||||
isCurved: true,
|
||||
color: const Color(0xFF8B5CF6),
|
||||
barWidth: 3,
|
||||
isStrokeCapRound: true,
|
||||
dotData: const FlDotData(show: false),
|
||||
belowBarData: BarAreaData(
|
||||
show: true,
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topCenter,
|
||||
end: Alignment.bottomCenter,
|
||||
colors: [
|
||||
const Color(0xFF8B5CF6).withOpacity(0.3),
|
||||
const Color(0xFF8B5CF6).withOpacity(0.0),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
lineTouchData: LineTouchData(
|
||||
enabled: true,
|
||||
touchTooltipData: LineTouchTooltipData(
|
||||
getTooltipColor: (touchedSpot) => WebTheme.getCardColor(context),
|
||||
getTooltipItems: (touchedSpots) {
|
||||
return touchedSpots.map((spot) {
|
||||
final dataIndex = spot.x.toInt();
|
||||
if (dataIndex >= 0 && dataIndex < _sortedData.length) {
|
||||
final data = _sortedData[dataIndex];
|
||||
|
||||
return LineTooltipItem(
|
||||
'${data.date}\n输入: ${data.inputTokens.toString().replaceAllMapped(RegExp(r'(\d)(?=(\d{3})+(?!\d))'), (match) => '${match[1]},')} tokens\n输出: ${data.outputTokens.toString().replaceAllMapped(RegExp(r'(\d)(?=(\d{3})+(?!\d))'), (match) => '${match[1]},')} tokens',
|
||||
TextStyle(
|
||||
color: WebTheme.getTextColor(context),
|
||||
fontWeight: FontWeight.w600,
|
||||
fontSize: 12,
|
||||
),
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}).where((item) => item != null).cast<LineTooltipItem>().toList();
|
||||
},
|
||||
),
|
||||
touchCallback: (FlTouchEvent event, LineTouchResponse? response) {
|
||||
setState(() {
|
||||
if (response == null || response.lineBarSpots == null) {
|
||||
touchedIndex = -1;
|
||||
} else {
|
||||
touchedIndex = response.lineBarSpots!.first.x.toInt();
|
||||
}
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildLegend() {
|
||||
final List<TokenUsageData> dataList = _sortedData;
|
||||
final currentData = dataList.isNotEmpty ? dataList.last : null;
|
||||
|
||||
return Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
_buildLegendItem(
|
||||
color: const Color(0xFF3B82F6),
|
||||
label: '输入Token',
|
||||
value: currentData != null ? '${(currentData.inputTokens / 1000).toStringAsFixed(0)}K' : '0K',
|
||||
),
|
||||
const SizedBox(width: 32),
|
||||
_buildLegendItem(
|
||||
color: const Color(0xFF8B5CF6),
|
||||
label: '输出Token',
|
||||
value: currentData != null ? '${(currentData.outputTokens / 1000).toStringAsFixed(0)}K' : '0K',
|
||||
),
|
||||
const SizedBox(width: 32),
|
||||
_buildLegendItem(
|
||||
color: Theme.of(context).primaryColor,
|
||||
label: '总计',
|
||||
value: currentData != null ? '${(currentData.totalTokens / 1000).toStringAsFixed(0)}K' : '0K',
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildLegendItem({
|
||||
required Color color,
|
||||
required String label,
|
||||
required String value,
|
||||
}) {
|
||||
return Row(
|
||||
children: [
|
||||
Container(
|
||||
width: 12,
|
||||
height: 12,
|
||||
decoration: BoxDecoration(
|
||||
color: color,
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
label,
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
value,
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
List<FlSpot> _getInputSpots() {
|
||||
final List<TokenUsageData> dataList = _sortedData;
|
||||
return dataList.asMap().entries.map((entry) {
|
||||
return FlSpot(entry.key.toDouble(), entry.value.inputTokens.toDouble());
|
||||
}).toList();
|
||||
}
|
||||
|
||||
List<FlSpot> _getOutputSpots() {
|
||||
final List<TokenUsageData> dataList = _sortedData;
|
||||
return dataList.asMap().entries.map((entry) {
|
||||
return FlSpot(entry.key.toDouble(), entry.value.outputTokens.toDouble());
|
||||
}).toList();
|
||||
}
|
||||
|
||||
double _getMaxY() {
|
||||
final List<TokenUsageData> dataList = _sortedData;
|
||||
if (dataList.isEmpty) return 100000;
|
||||
|
||||
final maxInput = dataList.map((d) => d.inputTokens).reduce((a, b) => a > b ? a : b);
|
||||
final maxOutput = dataList.map((d) => d.outputTokens).reduce((a, b) => a > b ? a : b);
|
||||
final max = maxInput > maxOutput ? maxInput : maxOutput;
|
||||
|
||||
// 添加20%的padding,并保证最小正数上限,避免全零导致maxY=0
|
||||
final withPadding = (max * 1.2).ceilToDouble();
|
||||
return withPadding <= 0 ? 1000 : withPadding;
|
||||
}
|
||||
|
||||
// 计算漂亮的网格间隔(1/2/5 x 10^k)
|
||||
double _getNiceGridInterval(double maxY) {
|
||||
final double roughStep = (maxY <= 0 ? 1000.0 : maxY) / 5.0;
|
||||
final double magnitude = math.pow(10, (math.log(roughStep) / math.ln10).floor()).toDouble();
|
||||
final double residual = roughStep / magnitude;
|
||||
double nice;
|
||||
if (residual >= 5) {
|
||||
nice = 5;
|
||||
} else if (residual >= 2) {
|
||||
nice = 2;
|
||||
} else {
|
||||
nice = 1;
|
||||
}
|
||||
return nice * magnitude;
|
||||
}
|
||||
|
||||
// 控制底部x轴标签密度,避免挤在一起
|
||||
int _computeXLabelInterval(int length) {
|
||||
if (length <= 10) return 1;
|
||||
if (length <= 20) return 2;
|
||||
if (length <= 40) return 4;
|
||||
return (length / 10).ceil();
|
||||
}
|
||||
|
||||
String _formatYAxisLabel(double value) {
|
||||
final double absVal = value.abs();
|
||||
if (absVal >= 1000000) {
|
||||
return '${(value / 1000000).toStringAsFixed(1)}M';
|
||||
}
|
||||
if (absVal >= 1000) {
|
||||
return '${(value / 1000).toStringAsFixed(0)}K';
|
||||
}
|
||||
return value.toInt().toString();
|
||||
}
|
||||
|
||||
String _formatXAxisLabel(AnalyticsViewMode mode, String raw) {
|
||||
switch (mode) {
|
||||
case AnalyticsViewMode.monthly:
|
||||
// 期望显示 MM
|
||||
final parts = raw.split('-');
|
||||
if (parts.length >= 2) {
|
||||
return parts[1];
|
||||
}
|
||||
return raw;
|
||||
case AnalyticsViewMode.daily:
|
||||
case AnalyticsViewMode.range:
|
||||
case AnalyticsViewMode.cumulative:
|
||||
// 期望显示 MM-dd
|
||||
// 支持 'yyyy-MM-dd' / 'yyyy-MM' / 'MM-dd'
|
||||
if (RegExp(r'^\d{4}-\d{1,2}-\d{1,2}$').hasMatch(raw)) {
|
||||
return raw.substring(raw.length - 5);
|
||||
}
|
||||
if (RegExp(r'^\d{4}-\d{1,2}$').hasMatch(raw)) {
|
||||
final parts = raw.split('-');
|
||||
return '${parts[1].padLeft(2, '0')}-01';
|
||||
}
|
||||
if (RegExp(r'^\d{1,2}-\d{1,2}$').hasMatch(raw)) {
|
||||
final parts = raw.split('-');
|
||||
return '${parts[0].padLeft(2, '0')}-${parts[1].padLeft(2, '0')}';
|
||||
}
|
||||
final parts2 = raw.split('-');
|
||||
if (parts2.length >= 3) {
|
||||
return '${parts2[1]}-${parts2[2].padLeft(2, '0')}';
|
||||
}
|
||||
return raw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
466
AINoval/lib/widgets/analytics/token_usage_list.dart
Normal file
466
AINoval/lib/widgets/analytics/token_usage_list.dart
Normal file
@@ -0,0 +1,466 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:ainoval/models/analytics_data.dart';
|
||||
import 'package:ainoval/utils/web_theme.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
class TokenUsageList extends StatelessWidget {
|
||||
final List<TokenUsageRecord> records;
|
||||
final Map<String, dynamic>? todaySummary;
|
||||
|
||||
const TokenUsageList({
|
||||
super.key,
|
||||
required this.records,
|
||||
this.todaySummary,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
children: [
|
||||
_buildSummaryStats(),
|
||||
const SizedBox(height: 24),
|
||||
_buildRecordsList(context),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSummaryStats() {
|
||||
// 从records数据中计算统计,不依赖后端汇总接口
|
||||
final stats = _calculateStats();
|
||||
|
||||
return Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: _buildSummaryCard(
|
||||
title: stats['isToday'] ? '今日调用次数' : '最近调用次数',
|
||||
value: stats['totalRecords'].toString(),
|
||||
color: const Color(0xFF3B82F6),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
Expanded(
|
||||
child: _buildSummaryCard(
|
||||
title: stats['isToday'] ? '今日 Token 消耗' : '最近 Token 消耗',
|
||||
value: _formatNumber(stats['totalTokens']),
|
||||
color: const Color(0xFF8B5CF6),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
Expanded(
|
||||
child: _buildSummaryCard(
|
||||
title: stats['isToday'] ? '今日成本' : '最近成本',
|
||||
value: '\$${stats['totalCost'].toStringAsFixed(4)}',
|
||||
color: const Color(0xFF10B981),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSummaryCard({
|
||||
required String title,
|
||||
required String value,
|
||||
required Color color,
|
||||
}) {
|
||||
return Builder(
|
||||
builder: (context) => Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: WebTheme.getCardColor(context).withOpacity(0.5),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(
|
||||
color: WebTheme.getBorderColor(context).withOpacity(0.5),
|
||||
),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
value,
|
||||
style: TextStyle(
|
||||
fontSize: 24,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
title,
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildRecordsList(BuildContext context) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(
|
||||
'Token 使用记录',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
|
||||
decoration: BoxDecoration(
|
||||
color: WebTheme.getCardColor(context),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(
|
||||
color: WebTheme.getBorderColor(context).withOpacity(0.5),
|
||||
),
|
||||
),
|
||||
child: Text(
|
||||
'最近 ${records.length} 条记录',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
ListView.separated(
|
||||
shrinkWrap: true,
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
itemCount: records.length,
|
||||
separatorBuilder: (context, index) => const SizedBox(height: 8),
|
||||
itemBuilder: (context, index) => _buildRecordItem(context, records[index]),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildRecordItem(BuildContext context, TokenUsageRecord record) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: WebTheme.getCardColor(context).withOpacity(0.5),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(
|
||||
color: WebTheme.getBorderColor(context).withOpacity(0.5),
|
||||
),
|
||||
),
|
||||
child: IntrinsicHeight(
|
||||
child: Row(
|
||||
children: [
|
||||
_buildFeatureAvatar(context, record.taskType),
|
||||
const SizedBox(width: 16),
|
||||
Expanded(
|
||||
child: _buildRecordContent(context, record),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
_buildTokenStats(context, record),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建功能类型头像,使用图标替代文字
|
||||
Widget _buildFeatureAvatar(BuildContext context, String taskType) {
|
||||
final color = _getFeatureTypeColor(taskType);
|
||||
final icon = _getFeatureTypeIcon(taskType);
|
||||
|
||||
return Container(
|
||||
width: 40,
|
||||
height: 40,
|
||||
decoration: BoxDecoration(
|
||||
color: color,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Center(
|
||||
child: Icon(
|
||||
icon,
|
||||
color: Colors.white,
|
||||
size: 20,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildRecordContent(BuildContext context, TokenUsageRecord record) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Text(
|
||||
record.taskType,
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.transparent,
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
border: Border.all(
|
||||
color: WebTheme.getBorderColor(context),
|
||||
),
|
||||
),
|
||||
child: Text(
|
||||
record.model,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Row(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.access_time,
|
||||
size: 12,
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
_formatDateTime(record.timestamp),
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTokenStats(BuildContext context, TokenUsageRecord record) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
children: [
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
_buildTokenStatItem(
|
||||
context: context,
|
||||
icon: Icons.unfold_more, // Text Expansion 风格,代表输入
|
||||
value: record.inputTokens,
|
||||
color: const Color(0xFF3B82F6),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
_buildTokenStatItem(
|
||||
context: context,
|
||||
icon: Icons.notes, // Text Summary 风格,代表输出
|
||||
value: record.outputTokens,
|
||||
color: const Color(0xFF8B5CF6),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
'成本: \$${record.cost.toStringAsFixed(4)}',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTokenStatItem({
|
||||
required BuildContext context,
|
||||
required IconData icon,
|
||||
required int value,
|
||||
required Color color,
|
||||
}) {
|
||||
return Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
icon,
|
||||
size: 12,
|
||||
color: color,
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
_formatNumber(value),
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: color,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
String _formatNumber(int number) {
|
||||
return number.toString().replaceAllMapped(
|
||||
RegExp(r'(\d)(?=(\d{3})+(?!\d))'),
|
||||
(match) => '${match[1]},',
|
||||
);
|
||||
}
|
||||
|
||||
String _formatDateTime(DateTime dateTime) {
|
||||
final now = DateTime.now();
|
||||
final difference = now.difference(dateTime);
|
||||
|
||||
if (difference.inMinutes < 60) {
|
||||
return '${difference.inMinutes}分钟前';
|
||||
} else if (difference.inHours < 24) {
|
||||
return '${difference.inHours}小时前';
|
||||
} else if (difference.inDays < 7) {
|
||||
return '${difference.inDays}天前';
|
||||
} else {
|
||||
return DateFormat('MM-dd HH:mm').format(dateTime);
|
||||
}
|
||||
}
|
||||
|
||||
/// 根据任务类型中文名称获取对应的AI功能图标
|
||||
IconData _getFeatureTypeIcon(String taskType) {
|
||||
switch (taskType) {
|
||||
case '场景摘要':
|
||||
return Icons.summarize;
|
||||
case '摘要扩写':
|
||||
return Icons.expand_more;
|
||||
case '文本扩写':
|
||||
return Icons.unfold_more;
|
||||
case '文本重构':
|
||||
return Icons.edit;
|
||||
case '文本总结':
|
||||
return Icons.notes;
|
||||
case 'AI聊天':
|
||||
return Icons.chat;
|
||||
case '小说生成':
|
||||
return Icons.create;
|
||||
case '设定编排':
|
||||
return Icons.dashboard_customize;
|
||||
case '专业续写':
|
||||
return Icons.auto_stories;
|
||||
case '场景节拍生成':
|
||||
return Icons.timeline;
|
||||
case '设定树生成':
|
||||
return Icons.account_tree;
|
||||
case '设定生成':
|
||||
return Icons.settings_applications; // 设定生成专用图标
|
||||
// 兼容其他功能类型
|
||||
case '智能续写':
|
||||
return Icons.unfold_more;
|
||||
case 'AI对话':
|
||||
return Icons.chat;
|
||||
case '内容优化':
|
||||
return Icons.tune;
|
||||
case '语法检查':
|
||||
return Icons.spellcheck;
|
||||
case '风格改进':
|
||||
return Icons.auto_fix_high;
|
||||
default:
|
||||
return Icons.smart_toy; // 默认AI图标
|
||||
}
|
||||
}
|
||||
|
||||
/// 根据任务类型中文名称获取对应的颜色
|
||||
Color _getFeatureTypeColor(String taskType) {
|
||||
switch (taskType) {
|
||||
case '场景摘要':
|
||||
return const Color(0xFF3B82F6); // 蓝色
|
||||
case '摘要扩写':
|
||||
return const Color(0xFF8B5CF6); // 紫色
|
||||
case '文本扩写':
|
||||
return const Color(0xFF10B981); // 绿色
|
||||
case '文本重构':
|
||||
return const Color(0xFFF59E0B); // 黄色
|
||||
case '文本总结':
|
||||
return const Color(0xFFEF4444); // 红色
|
||||
case 'AI聊天':
|
||||
return const Color(0xFF06B6D4); // 青色
|
||||
case '小说生成':
|
||||
return const Color(0xFF8B5CF6); // 紫色
|
||||
case '设定编排':
|
||||
return const Color(0xFF059669); // 深绿色
|
||||
case '专业续写':
|
||||
return const Color(0xFFDC2626); // 深红色
|
||||
case '场景节拍生成':
|
||||
return const Color(0xFF7C3AED); // 深紫色
|
||||
case '设定树生成':
|
||||
return const Color(0xFF0891B2); // 深青色
|
||||
case '设定生成':
|
||||
return const Color(0xFF6366F1); // 靛蓝色
|
||||
// 兼容其他功能类型
|
||||
case '智能续写':
|
||||
return const Color(0xFF10B981); // 绿色
|
||||
case 'AI对话':
|
||||
return const Color(0xFF06B6D4); // 青色
|
||||
case '内容优化':
|
||||
return const Color(0xFF8B5CF6); // 紫色
|
||||
case '语法检查':
|
||||
return const Color(0xFFF59E0B); // 黄色
|
||||
case '风格改进':
|
||||
return const Color(0xFFEF4444); // 红色
|
||||
default:
|
||||
return const Color(0xFF6B7280); // 灰色
|
||||
}
|
||||
}
|
||||
|
||||
/// 从records数据中计算统计,不依赖后端汇总接口
|
||||
Map<String, dynamic> _calculateStats() {
|
||||
// 如果没有记录数据,返回空统计
|
||||
if (records.isEmpty) {
|
||||
return {
|
||||
'totalRecords': 0,
|
||||
'totalTokens': 0,
|
||||
'totalCost': 0.0,
|
||||
'isToday': false,
|
||||
};
|
||||
}
|
||||
|
||||
final now = DateTime.now();
|
||||
final todayDate = DateTime(now.year, now.month, now.day);
|
||||
|
||||
// 筛选今日的记录
|
||||
final todayRecords = records.where((record) {
|
||||
final recordDate = DateTime(record.timestamp.year, record.timestamp.month, record.timestamp.day);
|
||||
return recordDate.isAtSameMomentAs(todayDate);
|
||||
}).toList();
|
||||
|
||||
// 如果有今日记录,返回今日统计
|
||||
if (todayRecords.isNotEmpty) {
|
||||
int totalRecords = todayRecords.length;
|
||||
int totalTokens = todayRecords.fold(0, (sum, record) => sum + record.totalTokens);
|
||||
double totalCost = todayRecords.fold(0.0, (sum, record) => sum + record.cost);
|
||||
|
||||
return {
|
||||
'totalRecords': totalRecords,
|
||||
'totalTokens': totalTokens,
|
||||
'totalCost': totalCost,
|
||||
'isToday': true,
|
||||
};
|
||||
}
|
||||
|
||||
// 没有今日记录,使用所有可见记录的统计
|
||||
int totalRecords = records.length;
|
||||
int totalTokens = records.fold(0, (sum, record) => sum + record.totalTokens);
|
||||
double totalCost = records.fold(0.0, (sum, record) => sum + record.cost);
|
||||
|
||||
return {
|
||||
'totalRecords': totalRecords,
|
||||
'totalTokens': totalTokens,
|
||||
'totalCost': totalCost,
|
||||
'isToday': false,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user