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

210 lines
7.4 KiB
Dart
Raw Normal View History

2022-09-27 22:49:58 +02:00
// ignore_for_file: use_build_context_synchronously
2022-09-27 18:42:23 +02:00
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
2022-10-03 22:41:19 +02:00
import 'package:adguard_home_manager/providers/app_config_provider.dart';
2022-09-27 18:42:23 +02:00
import 'package:adguard_home_manager/providers/servers_provider.dart';
class ManagementModal extends StatelessWidget {
2022-09-27 18:42:23 +02:00
const ManagementModal({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final serversProvider = Provider.of<ServersProvider>(context);
2022-10-03 22:41:19 +02:00
final appConfigProvider = Provider.of<AppConfigProvider>(context);
2022-09-27 18:42:23 +02:00
2022-09-27 22:49:58 +02:00
void updateBlocking(bool value, String filter) async {
final result = await serversProvider.updateBlocking(
serversProvider.selectedServer!,
filter,
value
);
2022-10-03 23:05:25 +02:00
if (result != null) {
2022-10-03 22:41:19 +02:00
if (result != false) {
appConfigProvider.addLog(result['log']);
}
2022-09-27 22:49:58 +02:00
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(AppLocalizations.of(context)!.invalidUsernamePassword),
backgroundColor: Colors.red,
)
);
}
}
2022-09-27 18:42:23 +02:00
Widget mainSwitch() {
return Padding(
2022-11-04 17:04:25 +01:00
padding: const EdgeInsets.symmetric(horizontal: 24),
2022-09-27 18:42:23 +02:00
child: Material(
color: Theme.of(context).primaryColor.withOpacity(0.1),
borderRadius: BorderRadius.circular(28),
child: InkWell(
onTap: serversProvider.protectionsManagementProcess.contains('general') == false
2022-09-27 22:49:58 +02:00
? () => updateBlocking(!serversProvider.serverStatus.data!.generalEnabled, 'general')
: null,
2022-09-27 18:42:23 +02:00
borderRadius: BorderRadius.circular(28),
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 20,
vertical: 12
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
AppLocalizations.of(context)!.allProtections,
style: const TextStyle(
fontSize: 18,
),
),
Switch(
value: serversProvider.serverStatus.data!.generalEnabled,
onChanged: serversProvider.protectionsManagementProcess.contains('general') == false
2022-09-27 22:49:58 +02:00
? (value) => updateBlocking(value, 'general')
: null,
2022-09-27 18:42:23 +02:00
activeColor: Theme.of(context).primaryColor,
)
],
),
),
),
),
);
}
2022-09-27 22:49:58 +02:00
Widget smallSwitch(String label, IconData icon, bool value, Function(bool) onChange, bool disabled) {
2022-09-27 18:42:23 +02:00
return Material(
color: Colors.transparent,
child: InkWell(
2022-09-27 22:49:58 +02:00
onTap: disabled == false
? () => onChange(!value)
: null,
2022-09-27 18:42:23 +02:00
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 35,
2022-11-04 17:04:25 +01:00
vertical: 8
2022-09-27 18:42:23 +02:00
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
2022-09-27 22:49:58 +02:00
Row(
children: [
Icon(
icon,
size: 24,
2022-11-05 02:35:35 +01:00
color: Theme.of(context).listTileTheme.iconColor,
2022-09-27 22:49:58 +02:00
),
2022-11-04 17:04:25 +01:00
const SizedBox(width: 16),
2022-09-27 22:49:58 +02:00
Text(
label,
2022-11-04 17:04:25 +01:00
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w400,
color: Theme.of(context).colorScheme.onSurface,
2022-09-27 22:49:58 +02:00
),
),
],
2022-09-27 18:42:23 +02:00
),
Switch(
value: value,
2022-09-27 22:49:58 +02:00
onChanged: disabled == false
? onChange
: null,
2022-09-27 18:42:23 +02:00
activeColor: Theme.of(context).primaryColor,
)
],
),
),
),
);
}
return Container(
width: double.maxFinite,
height: 540,
decoration: BoxDecoration(
color: Theme.of(context).dialogBackgroundColor,
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(28),
topRight: Radius.circular(28)
)
),
child: Column(
children: [
Expanded(
child: ListView(
physics: 540 < MediaQuery.of(context).size.height
? const NeverScrollableScrollPhysics()
: null,
children: [
2022-11-04 17:04:25 +01:00
Padding(
padding: const EdgeInsets.only(top: 24),
child: Icon(
Icons.shield_rounded,
size: 24,
2022-11-05 02:35:35 +01:00
color: Theme.of(context).listTileTheme.iconColor
2022-11-04 17:04:25 +01:00
),
2022-09-27 18:42:23 +02:00
),
2022-11-04 17:04:25 +01:00
Padding(
padding: const EdgeInsets.symmetric(vertical: 16),
child: Text(
AppLocalizations.of(context)!.manageServer,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 24,
color: Theme.of(context).colorScheme.onSurface,
),
2022-09-27 18:42:23 +02:00
),
),
2022-11-04 17:04:25 +01:00
mainSwitch(),
const SizedBox(height: 10),
smallSwitch(
AppLocalizations.of(context)!.ruleFiltering,
Icons.filter_list_rounded,
serversProvider.serverStatus.data!.filteringEnabled,
(value) => updateBlocking(value, 'filtering'),
serversProvider.protectionsManagementProcess.contains('filtering')
),
smallSwitch(
AppLocalizations.of(context)!.safeBrowsing,
Icons.vpn_lock_rounded,
serversProvider.serverStatus.data!.safeBrowsingEnabled,
(value) => updateBlocking(value, 'safeBrowsing'),
serversProvider.protectionsManagementProcess.contains('safeBrowsing')
),
smallSwitch(
AppLocalizations.of(context)!.parentalFiltering,
Icons.block,
serversProvider.serverStatus.data!.parentalControlEnabled,
(value) => updateBlocking(value, 'parentalControl'),
serversProvider.protectionsManagementProcess.contains('parentalControl')
),
smallSwitch(
AppLocalizations.of(context)!.safeSearch,
Icons.search_rounded,
serversProvider.serverStatus.data!.safeSearchEnabled,
(value) => updateBlocking(value, 'safeSearch'),
serversProvider.protectionsManagementProcess.contains('safeSearch')
),
],
),
2022-09-27 18:42:23 +02:00
),
Padding(
padding: const EdgeInsets.all(24),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
TextButton(
onPressed: () => Navigator.pop(context),
child: Text(AppLocalizations.of(context)!.close),
),
],
),
)
],
),
);
}
}