adguard-home-manager/lib/screens/home/chart.dart

199 lines
7.5 KiB
Dart
Raw Normal View History

2022-10-04 15:19:44 +02:00
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
2023-12-17 14:32:46 +01:00
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
2022-10-04 15:19:44 +02:00
import 'package:adguard_home_manager/widgets/line_chart.dart';
import 'package:adguard_home_manager/providers/app_config_provider.dart';
2023-12-17 14:14:31 +01:00
class HomeChart extends StatefulWidget {
2022-10-04 15:19:44 +02:00
final List<int> data;
final String label;
final String primaryValue;
final String secondaryValue;
final Color color;
2023-07-10 20:45:03 +02:00
final int hoursInterval;
2023-12-17 14:14:31 +01:00
final void Function() onTapTitle;
2023-12-17 14:32:46 +01:00
final bool isDesktop;
2022-10-04 15:19:44 +02:00
const HomeChart({
2023-11-29 11:56:28 +01:00
super.key,
2022-10-04 15:19:44 +02:00
required this.data,
required this.label,
required this.primaryValue,
required this.secondaryValue,
2023-07-10 20:45:03 +02:00
required this.color,
2023-12-17 14:14:31 +01:00
required this.hoursInterval,
required this.onTapTitle,
2023-12-17 14:32:46 +01:00
required this.isDesktop,
2023-11-29 11:56:28 +01:00
});
2022-10-04 15:19:44 +02:00
2023-12-17 14:14:31 +01:00
@override
State<HomeChart> createState() => _HomeChartState();
}
class _HomeChartState extends State<HomeChart> {
bool _isHover = false;
2022-10-04 15:19:44 +02:00
@override
Widget build(BuildContext context) {
final appConfigProvider = Provider.of<AppConfigProvider>(context);
2023-12-17 14:14:31 +01:00
final bool isEmpty = widget.data.every((i) => i == 0);
2022-10-04 15:19:44 +02:00
if (!(appConfigProvider.hideZeroValues == true && isEmpty == true)) {
2023-07-10 20:45:03 +02:00
List<DateTime> dateTimes = [];
2024-02-29 15:29:56 +01:00
DateTime currentDate = DateTime.now().subtract(Duration(hours: widget.hoursInterval*widget.data.length));
2023-12-17 14:14:31 +01:00
for (var i = 0; i < widget.data.length; i++) {
currentDate = currentDate.add(Duration(hours: widget.hoursInterval));
2023-07-10 20:45:03 +02:00
dateTimes.add(currentDate);
}
return Column(
2022-10-04 15:19:44 +02:00
children: [
Padding(
2023-05-20 22:57:38 +02:00
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Column(
2022-10-04 15:19:44 +02:00
children: [
Padding(
padding: EdgeInsets.only(
bottom: !isEmpty ? 10 : 15
2022-10-04 15:19:44 +02:00
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
2023-12-17 14:32:46 +01:00
crossAxisAlignment: isEmpty
? CrossAxisAlignment.center
: CrossAxisAlignment.start,
children: [
2023-05-01 14:38:46 +02:00
Flexible(
2023-12-17 14:14:31 +01:00
child: MouseRegion(
cursor: SystemMouseCursors.click,
onEnter: (_) => setState(() => _isHover = true),
onExit: (_) => setState(() => _isHover = false),
child: GestureDetector(
onTapDown: (_) => setState(() => _isHover = true),
onTapUp: (_) => setState(() => _isHover = false),
2023-12-17 14:32:46 +01:00
onTap: !isEmpty ? () => widget.onTapTitle () : null,
2023-12-17 14:14:31 +01:00
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Flexible(
child: Text(
widget.label,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.w500,
2023-12-17 14:32:46 +01:00
color: _isHover && !isEmpty
2023-12-17 14:14:31 +01:00
? Theme.of(context).colorScheme.onSurfaceVariant
: Theme.of(context).colorScheme.onSurface,
),
),
),
2023-12-17 14:32:46 +01:00
if (!isEmpty) ...[
const SizedBox(width: 4),
Icon(
Icons.chevron_right_rounded,
size: 20,
color: _isHover && !isEmpty
? Theme.of(context).colorScheme.onSurfaceVariant
: Theme.of(context).colorScheme.onSurface,
)
],
2023-12-17 14:14:31 +01:00
],
),
2023-05-01 14:38:46 +02:00
),
2022-10-04 15:19:44 +02:00
),
),
2023-12-17 14:32:46 +01:00
if (!isEmpty) Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
widget.primaryValue,
style: TextStyle(
color: widget.color,
fontSize: 18,
fontWeight: FontWeight.w500
),
),
Text(
widget.secondaryValue,
style: TextStyle(
fontSize: 12,
color: widget.color
),
)
2023-12-17 14:32:46 +01:00
],
),
if (isEmpty && !widget.isDesktop) Column(
children: [
Icon(
Icons.show_chart_rounded,
color: Theme.of(context).colorScheme.onSurfaceVariant,
size: 16,
),
Text(
AppLocalizations.of(context)!.noData,
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w500,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
)
2023-12-17 14:32:46 +01:00
],
)
],
),
),
if (!isEmpty) SizedBox(
width: double.maxFinite,
height: 150,
child: CustomLineChart(
2023-12-17 14:14:31 +01:00
data: widget.data,
color: widget.color,
2023-07-10 20:45:03 +02:00
dates: dateTimes,
2023-12-17 14:14:31 +01:00
daysInterval: widget.hoursInterval == 24,
2023-07-10 20:45:03 +02:00
context: context,
)
),
2023-12-17 14:32:46 +01:00
if (isEmpty && widget.isDesktop) SizedBox(
height: 150,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.show_chart_rounded,
color: Theme.of(context).colorScheme.onSurfaceVariant,
size: 30,
),
const SizedBox(height: 20),
Text(
AppLocalizations.of(context)!.noDataChart,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 16,
color: Theme.of(context).colorScheme.onSurfaceVariant
),
)
],
),
)
2022-10-04 15:19:44 +02:00
],
),
),
2022-11-04 22:56:00 +01:00
Padding(
2023-05-20 22:57:38 +02:00
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Divider(
thickness: 1,
2022-11-04 22:56:00 +01:00
color: Theme.of(context).colorScheme.onSurface.withOpacity(0.2),
),
2022-10-04 15:19:44 +02:00
),
2023-05-20 22:57:38 +02:00
const SizedBox(height: 16),
2022-10-04 15:19:44 +02:00
],
);
}
else {
return const SizedBox();
}
2022-10-04 15:19:44 +02:00
}
}