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

256 lines
7.8 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';
import 'package:adguard_home_manager/providers/servers_provider.dart';
2022-09-27 22:49:58 +02:00
class ManagementModal extends StatefulWidget {
2022-09-27 18:42:23 +02:00
const ManagementModal({Key? key}) : super(key: key);
2022-09-27 22:49:58 +02:00
@override
State<ManagementModal> createState() => _ManagementModalState();
}
class _ManagementModalState extends State<ManagementModal> {
bool disableGeneralSwitch = false;
bool disableFiltersSwitch = false;
bool disableSafeBrowsingSwitch = false;
bool disableParentalControlSwitch = false;
bool disableSafeSearchSwitch = false;
2022-09-27 18:42:23 +02:00
@override
Widget build(BuildContext context) {
final serversProvider = Provider.of<ServersProvider>(context);
2022-09-27 22:49:58 +02:00
void updateBlocking(bool value, String filter) async {
switch (filter) {
case 'general':
setState(() => disableGeneralSwitch = true);
break;
case 'filtering':
setState(() => disableFiltersSwitch = true);
break;
case 'safeBrowsing':
setState(() => disableSafeBrowsingSwitch = true);
break;
case 'parentalControl':
setState(() => disableParentalControlSwitch = true);
break;
case 'safeSearch':
setState(() => disableSafeSearchSwitch = true);
break;
default:
break;
}
final result = await serversProvider.updateBlocking(
serversProvider.selectedServer!,
filter,
value
);
if (result == false) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(AppLocalizations.of(context)!.invalidUsernamePassword),
backgroundColor: Colors.red,
)
);
}
switch (filter) {
case 'general':
setState(() => disableGeneralSwitch = false);
break;
case 'filtering':
setState(() => disableFiltersSwitch = false);
break;
case 'safeBrowsing':
setState(() => disableSafeBrowsingSwitch = false);
break;
case 'parentalControl':
setState(() => disableParentalControlSwitch = false);
break;
case 'safeSearch':
setState(() => disableSafeSearchSwitch = false);
break;
default:
break;
}
}
2022-09-27 18:42:23 +02:00
Widget mainSwitch() {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 12),
child: Material(
color: Theme.of(context).primaryColor.withOpacity(0.1),
borderRadius: BorderRadius.circular(28),
child: InkWell(
2022-09-27 22:49:58 +02:00
onTap: disableGeneralSwitch == false
? () => 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,
2022-09-27 22:49:58 +02:00
onChanged: disableGeneralSwitch == false
? (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,
vertical: 5
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
2022-09-27 22:49:58 +02:00
Row(
children: [
Icon(
icon,
size: 24,
),
const SizedBox(width: 20),
Text(
label,
style: const TextStyle(
fontSize: 15,
),
),
],
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: [
Column(
children: [
const Padding(
padding: EdgeInsets.only(top: 24),
child: Icon(
2022-09-27 22:49:58 +02:00
Icons.shield_rounded,
2022-09-27 18:42:23 +02:00
size: 26,
),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 24),
child: Text(
AppLocalizations.of(context)!.manageServer,
style: const TextStyle(
fontSize: 22
),
),
),
mainSwitch(),
const SizedBox(height: 10),
smallSwitch(
AppLocalizations.of(context)!.ruleFiltering,
2022-09-27 22:49:58 +02:00
Icons.filter_list_rounded,
serversProvider.serverStatus.data!.filteringEnabled,
(value) => updateBlocking(value, 'filtering'),
disableFiltersSwitch
2022-09-27 18:42:23 +02:00
),
smallSwitch(
AppLocalizations.of(context)!.safeBrowsing,
2022-09-27 22:49:58 +02:00
Icons.vpn_lock_rounded,
serversProvider.serverStatus.data!.safeBrowsingEnabled,
(value) => updateBlocking(value, 'safeBrowsing'),
disableSafeBrowsingSwitch
2022-09-27 18:42:23 +02:00
),
smallSwitch(
AppLocalizations.of(context)!.parentalFiltering,
2022-09-27 22:49:58 +02:00
Icons.block,
serversProvider.serverStatus.data!.parentalControlEnabled,
(value) => updateBlocking(value, 'parentalControl'),
disableParentalControlSwitch
2022-09-27 18:42:23 +02:00
),
smallSwitch(
AppLocalizations.of(context)!.safeSearch,
2022-09-27 22:49:58 +02:00
Icons.search_rounded,
serversProvider.serverStatus.data!.safeSearchEnabled,
(value) => updateBlocking(value, 'safeSearch'),
disableSafeSearchSwitch
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),
),
],
),
)
],
),
);
}
}