adguard-home-manager/lib/screens/home/top_items/top_items_section.dart

347 lines
11 KiB
Dart
Raw Normal View History

2023-11-01 20:47:37 +01:00
// ignore_for_file: use_build_context_synchronously
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:adguard_home_manager/screens/home/top_items/row_item.dart';
2023-11-29 17:18:35 +01:00
import 'package:adguard_home_manager/screens/home/top_items/top_items_screen.dart';
import 'package:adguard_home_manager/widgets/custom_pie_chart.dart';
2023-11-01 20:47:37 +01:00
2024-01-25 00:39:36 +01:00
import 'package:adguard_home_manager/functions/number_format.dart';
import 'package:adguard_home_manager/models/menu_option.dart';
import 'package:adguard_home_manager/constants/enums.dart';
2023-11-01 20:47:37 +01:00
2024-01-25 00:47:20 +01:00
class TopItemsSection extends StatelessWidget {
final HomeTopItems type;
2023-11-01 20:47:37 +01:00
final String label;
final List<Map<String, dynamic>> data;
final bool withChart;
final bool withProgressBar;
final String Function(dynamic) buildValue;
final List<MenuOption> Function(dynamic) menuOptions;
final void Function(dynamic)? onTapEntry;
2023-11-01 20:47:37 +01:00
2023-11-29 17:18:35 +01:00
const TopItemsSection({
super.key,
2023-11-01 20:47:37 +01:00
required this.type,
required this.label,
required this.data,
required this.withChart,
required this.withProgressBar,
required this.buildValue,
required this.menuOptions,
this.onTapEntry,
});
2023-11-01 20:47:37 +01:00
@override
Widget build(BuildContext context) {
2024-01-25 00:47:20 +01:00
final colors = [
Colors.red,
Colors.green,
Colors.blue,
Colors.orange,
Colors.teal,
Colors.grey
];
2023-11-01 20:47:37 +01:00
final width = MediaQuery.of(context).size.width;
2024-01-25 00:47:20 +01:00
final withChart = type != HomeTopItems.avgUpstreamResponseTime;
2024-01-25 00:39:36 +01:00
Map<String, double> ringData() {
2023-11-01 20:47:37 +01:00
Map<String, double> values = {};
2024-01-25 00:47:20 +01:00
data.sublist(0, data.length > 5 ? 5 : data.length).forEach((element) {
2023-11-01 20:47:37 +01:00
values = {
...values,
element.keys.first: element.values.first.toDouble()
};
});
2024-01-25 00:47:20 +01:00
if (data.length > 5) {
2023-11-01 20:47:37 +01:00
final int rest = List<int>.from(
2024-01-25 00:47:20 +01:00
data.sublist(5, data.length).map((e) => e.values.first.toInt())
2023-11-01 20:47:37 +01:00
).reduce((a, b) => a + b);
values = {
...values,
AppLocalizations.of(context)!.others: rest.toDouble()
};
}
return values;
}
2024-01-25 00:39:36 +01:00
List<Map<String, dynamic>> lineData() {
List<Map<String, dynamic>> values = [];
2024-01-25 00:47:20 +01:00
data.sublist(0, data.length > 5 ? 5 : data.length).forEach((element) {
2024-01-25 00:39:36 +01:00
values.add({
"label": element.keys.first,
"value": element.values.first.toDouble()
});
});
2024-01-25 00:47:20 +01:00
if (data.length > 5) {
2024-01-25 00:39:36 +01:00
final int rest = List<int>.from(
2024-01-25 00:47:20 +01:00
data.sublist(5, data.length).map((e) => e.values.first.toInt())
2024-01-25 00:39:36 +01:00
).reduce((a, b) => a + b);
values.add({
"label": AppLocalizations.of(context)!.others,
"value": rest.toDouble()
});
}
return values;
}
2024-01-28 19:04:01 +01:00
final List<Map<String, dynamic>> lineChartData = lineData();
final mapData = lineChartData.map((e) => e["value"]);
final List<dynamic> total = mapData.isNotEmpty
? mapData.reduce((a, b) => a + b)
: [];
2023-11-01 20:47:37 +01:00
return SizedBox(
child: Column(
children: [
2024-01-28 19:04:01 +01:00
if (data.isEmpty) _NoData(label: label),
2024-01-25 00:47:20 +01:00
if (data.isNotEmpty && width > 700) Padding(
padding: EdgeInsets.only(bottom: withChart == false ? 16 : 0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
if (withChart == true) Expanded(
flex: 1,
child: ConstrainedBox(
constraints: const BoxConstraints(
maxHeight: 250
2023-11-01 20:47:37 +01:00
),
child: Padding(
padding: const EdgeInsets.all(16),
child: CustomPieChart(
2024-01-25 00:39:36 +01:00
data: ringData(),
colors: colors
)
),
)
2023-11-01 20:47:37 +01:00
),
Expanded(
flex: 2,
child: Column(
children: [
Padding(
padding: const EdgeInsets.only(
top: 8,
bottom: 16
),
child: Text(
2024-01-25 00:47:20 +01:00
label,
style: const TextStyle(
2023-11-01 20:47:37 +01:00
fontSize: 18,
fontWeight: FontWeight.w500
2023-11-01 20:47:37 +01:00
),
),
),
_ItemsList(
colors: colors,
2024-01-25 00:47:20 +01:00
data: data,
clients: type == HomeTopItems.recurrentClients,
type: type,
showChart: withChart,
buildValue: buildValue,
menuOptions: menuOptions,
onTapEntry: onTapEntry,
),
if (withChart == true) OthersRowItem(
2024-01-25 00:47:20 +01:00
items: data,
showColor: true,
)
]
2023-11-01 20:47:37 +01:00
),
)
2023-11-01 20:47:37 +01:00
],
),
),
2024-01-25 00:47:20 +01:00
if (data.isNotEmpty && width <= 700) ...[
2024-01-25 00:39:36 +01:00
Text(
2024-01-25 00:47:20 +01:00
label,
2024-01-25 00:39:36 +01:00
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.w500,
color: Theme.of(context).colorScheme.onSurface
),
),
const SizedBox(height: 8),
if (withChart == true) Padding(
padding: const EdgeInsets.all(16),
child: ClipRRect(
borderRadius: BorderRadius.circular(20),
child: SizedBox(
height: 20,
child: LayoutBuilder(
builder: (context, constraints) => Row(
2024-01-25 00:47:20 +01:00
children: lineChartData.asMap().entries.map((e) => Tooltip(
2024-01-25 00:39:36 +01:00
message:'${e.value["label"]} (${doubleFormat((e.value["value"]/total)*100, Platform.localeName)}%)',
child: Container(
width: constraints.maxWidth*(e.value["value"]/total),
decoration: BoxDecoration(
color: colors[e.key]
),
),
2024-01-25 00:39:36 +01:00
)).toList()
)
)
),
),
),
Padding(
padding: const EdgeInsets.only(top: 8),
child: _ItemsList(
colors: colors,
2024-01-25 00:47:20 +01:00
data: data,
clients: type == HomeTopItems.recurrentClients,
type: type,
showChart: withChart,
buildValue: buildValue,
menuOptions: menuOptions,
onTapEntry: onTapEntry,
2024-01-25 00:39:36 +01:00
),
),
OthersRowItem(
2024-01-25 00:47:20 +01:00
items: data,
showColor: withChart,
2024-01-25 00:39:36 +01:00
),
],
2023-11-01 20:47:37 +01:00
2024-01-25 00:47:20 +01:00
if (data.length > 5) ...[
2023-11-01 20:47:37 +01:00
Padding(
padding: const EdgeInsets.only(right: 20),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
TextButton(
onPressed: () => {
2024-01-27 22:25:46 +01:00
if (width > 700) {
showDialog(
context: context,
builder: (context) => TopItemsScreen(
type: type,
title: label,
isClient: type == HomeTopItems.recurrentClients,
data: data,
withProgressBar: withProgressBar,
buildValue: buildValue,
options: menuOptions,
onTapEntry: onTapEntry,
isFullscreen: !(width > 700 || !(Platform.isAndroid | Platform.isIOS)),
),
2023-11-01 20:47:37 +01:00
)
2024-01-27 22:25:46 +01:00
}
else {
Navigator.push(context, MaterialPageRoute(
builder: (context) => TopItemsScreen(
type: type,
title: label,
isClient: type == HomeTopItems.recurrentClients,
data: data,
withProgressBar: withProgressBar,
buildValue: buildValue,
options: menuOptions,
onTapEntry: onTapEntry,
isFullscreen: !(width > 700 || !(Platform.isAndroid | Platform.isIOS)),
),
))
}
2023-11-01 20:47:37 +01:00
},
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Text(AppLocalizations.of(context)!.viewMore),
const SizedBox(width: 10),
const Icon(
Icons.arrow_forward,
size: 20,
)
],
)
),
],
),
),
const SizedBox(height: 10),
]
],
),
);
}
}
class _ItemsList extends StatelessWidget {
2023-11-01 20:47:37 +01:00
final List<Color> colors;
final List<Map<String, dynamic>> data;
final bool? clients;
final HomeTopItems type;
2023-11-01 20:47:37 +01:00
final bool showChart;
final String Function(dynamic) buildValue;
final List<MenuOption> Function(dynamic) menuOptions;
final void Function(dynamic)? onTapEntry;
2023-11-01 20:47:37 +01:00
const _ItemsList({
2023-11-01 20:47:37 +01:00
required this.colors,
required this.data,
required this.clients,
required this.type,
required this.showChart,
required this.buildValue,
required this.menuOptions,
this.onTapEntry,
});
2023-11-01 20:47:37 +01:00
@override
Widget build(BuildContext context) {
return Column(
children: data.sublist(
0, data.length > 5 ? 5 : data.length
).asMap().entries.map((e) => RowItem(
clients: clients ?? false,
domain: e.value.keys.toList()[0],
number: buildValue(e.value.values.toList()[0]),
2023-11-01 20:47:37 +01:00
type: type,
chartColor: colors[e.key],
showColor: showChart,
options: menuOptions,
onTapEntry: onTapEntry,
2023-11-01 20:47:37 +01:00
)).toList()
);
}
2024-01-28 19:04:01 +01:00
}
class _NoData extends StatelessWidget {
final String label;
const _NoData({
required this.label
});
@override
Widget build(BuildContext context) {
return Column(
children: [
Text(
label,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.w500,
color: Theme.of(context).colorScheme.onSurface
),
),
const SizedBox(height: 24),
Text(
AppLocalizations.of(context)!.noDataThisSection,
style: TextStyle(
fontSize: 16,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
const SizedBox(height: 16),
],
);
}
2023-11-01 20:47:37 +01:00
}