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

311 lines
8.8 KiB
Dart
Raw Normal View History

2023-11-01 20:47:37 +01:00
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:adguard_home_manager/widgets/domain_options.dart';
import 'package:adguard_home_manager/models/applied_filters.dart';
import 'package:adguard_home_manager/providers/app_config_provider.dart';
import 'package:adguard_home_manager/providers/logs_provider.dart';
import 'package:adguard_home_manager/providers/status_provider.dart';
2023-11-01 21:14:45 +01:00
class RowItem extends StatefulWidget {
2023-11-01 20:47:37 +01:00
final String type;
final Color chartColor;
final String domain;
final String number;
final bool clients;
final bool showColor;
const RowItem({
Key? key,
required this.type,
required this.chartColor,
required this.domain,
required this.number,
required this.clients,
required this.showColor,
}) : super(key: key);
2023-11-01 21:14:45 +01:00
@override
State<RowItem> createState() => _RowItemState();
}
class _RowItemState extends State<RowItem> with TickerProviderStateMixin {
late AnimationController expandController;
late Animation<double> animation;
@override
void initState() {
super.initState();
prepareAnimations();
_runExpandCheck();
}
void prepareAnimations() {
expandController = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 250)
);
animation = CurvedAnimation(
parent: expandController,
curve: Curves.ease,
);
}
void _runExpandCheck() {
if (widget.showColor) {
expandController.forward();
}
else {
expandController.reverse();
}
}
@override
void didUpdateWidget(oldWidget) {
super.didUpdateWidget(oldWidget);
_runExpandCheck();
}
@override
void dispose() {
expandController.dispose();
super.dispose();
}
2023-11-01 20:47:37 +01:00
@override
Widget build(BuildContext context) {
final statusProvider = Provider.of<StatusProvider>(context);
final appConfigProvider = Provider.of<AppConfigProvider>(context);
final logsProvider = Provider.of<LogsProvider>(context);
String? name;
2023-11-01 21:14:45 +01:00
if (widget.clients == true) {
2023-11-01 20:47:37 +01:00
try {
2023-11-01 21:14:45 +01:00
name = statusProvider.serverStatus!.clients.firstWhere((c) => c.ids.contains(widget.domain)).name;
2023-11-01 20:47:37 +01:00
} catch (e) {
// ---- //
}
}
return Material(
color: Colors.transparent,
child: DomainOptions(
2023-11-01 21:14:45 +01:00
item: widget.domain,
isClient: widget.type == 'topClients',
isBlocked: widget.type == 'topBlockedDomains',
2023-11-01 20:47:37 +01:00
onTap: () {
2023-11-01 21:14:45 +01:00
if (widget.type == 'topQueriedDomains' || widget.type == 'topBlockedDomains') {
logsProvider.setSearchText(widget.domain);
2023-11-01 20:47:37 +01:00
logsProvider.setSelectedClients(null);
logsProvider.setAppliedFilters(
AppliedFiters(
selectedResultStatus: 'all',
2023-11-01 21:14:45 +01:00
searchText: widget.domain,
2023-11-01 20:47:37 +01:00
clients: null
)
);
appConfigProvider.setSelectedScreen(2);
}
2023-11-01 21:14:45 +01:00
else if (widget.type == 'topClients') {
2023-11-01 20:47:37 +01:00
logsProvider.setSearchText(null);
2023-11-01 21:14:45 +01:00
logsProvider.setSelectedClients([widget.domain]);
2023-11-01 20:47:37 +01:00
logsProvider.setAppliedFilters(
AppliedFiters(
selectedResultStatus: 'all',
searchText: null,
2023-11-01 21:14:45 +01:00
clients: [widget.domain]
2023-11-01 20:47:37 +01:00
)
);
2023-11-01 20:49:38 +01:00
appConfigProvider.setSelectedScreen(2);
2023-11-01 20:47:37 +01:00
}
},
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 20,
vertical: 8
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Flexible(
child: Row(
children: [
2023-11-01 21:14:45 +01:00
SizeTransition(
axisAlignment: 1.0,
sizeFactor: animation,
axis: Axis.horizontal,
child: Container(
margin: const EdgeInsets.only(right: 16),
width: 12,
height: 12,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30),
color: widget.chartColor
),
2023-11-01 20:47:37 +01:00
),
),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
2023-11-01 21:14:45 +01:00
widget.domain,
2023-11-01 20:47:37 +01:00
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 16,
color: Theme.of(context).colorScheme.onSurface
),
),
if (name != null) ...[
const SizedBox(height: 5),
Text(
name,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 14,
color: Theme.of(context).colorScheme.onSurfaceVariant
),
),
]
],
),
),
],
),
),
const SizedBox(width: 16),
Text(
2023-11-01 21:14:45 +01:00
widget.number,
2023-11-01 20:47:37 +01:00
style: TextStyle(
color: Theme.of(context).colorScheme.onSurface
),
)
],
),
),
),
);
}
}
class OthersRowItem extends StatefulWidget {
final List<Map<String, dynamic>> items;
final bool showColor;
const OthersRowItem({
Key? key,
required this.items,
required this.showColor,
}) : super(key: key);
@override
State<OthersRowItem> createState() => _OthersRowItemState();
}
class _OthersRowItemState extends State<OthersRowItem> with SingleTickerProviderStateMixin {
late AnimationController expandController;
late Animation<double> animation;
@override
void initState() {
super.initState();
prepareAnimations();
_runExpandCheck();
}
void prepareAnimations() {
expandController = AnimationController(
vsync: this,
2023-11-01 21:14:45 +01:00
duration: const Duration(milliseconds: 250)
2023-11-01 20:47:37 +01:00
);
animation = CurvedAnimation(
parent: expandController,
curve: Curves.ease,
);
}
void _runExpandCheck() {
if (widget.showColor) {
expandController.forward();
}
else {
expandController.reverse();
}
}
@override
void didUpdateWidget(oldWidget) {
super.didUpdateWidget(oldWidget);
_runExpandCheck();
}
@override
void dispose() {
expandController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
if (widget.items.length <= 5) {
return const SizedBox();
}
return SizeTransition(
axisAlignment: 1.0,
sizeFactor: animation,
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 20,
vertical: 8
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Flexible(
child: Row(
children: [
Container(
margin: const EdgeInsets.only(right: 16),
width: 12,
height: 12,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30),
color: Colors.grey
),
),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
AppLocalizations.of(context)!.others,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 16,
color: Theme.of(context).colorScheme.onSurface
),
),
],
),
),
],
),
),
const SizedBox(width: 16),
Text(
List<int>.from(
widget.items.sublist(5, widget.items.length).map((e) => e.values.first.toInt())
).reduce((a, b) => a + b).toString(),
style: TextStyle(
color: Theme.of(context).colorScheme.onSurface
),
)
],
),
),
);
}
}