adguard-home-manager/lib/screens/settings/access_settings/add_client_modal.dart

208 lines
6.1 KiB
Dart
Raw Normal View History

2022-10-06 00:47:35 +02:00
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
2023-11-19 04:56:51 +01:00
import 'package:adguard_home_manager/providers/clients_provider.dart';
class AddClientModal extends StatelessWidget {
final AccessSettingsList type;
final void Function(String, AccessSettingsList) onConfirm;
2023-04-30 23:36:02 +02:00
final bool dialog;
2022-10-06 00:47:35 +02:00
2022-10-09 01:13:55 +02:00
const AddClientModal({
2023-11-19 04:56:51 +01:00
super.key,
2022-10-09 01:13:55 +02:00
required this.type,
2023-04-30 23:36:02 +02:00
required this.onConfirm,
required this.dialog,
2023-11-19 04:56:51 +01:00
});
2022-10-06 00:47:35 +02:00
@override
2023-11-19 04:56:51 +01:00
Widget build(BuildContext context) {
if (dialog == true) {
return Padding(
padding: MediaQuery.of(context).viewInsets,
child: Dialog(
child: ConstrainedBox(
constraints: const BoxConstraints(
maxWidth: 400
),
child: _Content(
type: type,
onConfirm: onConfirm,
)
),
),
);
}
else {
return Padding(
padding: MediaQuery.of(context).viewInsets,
child: Container(
decoration: BoxDecoration(
color: Theme.of(context).dialogBackgroundColor,
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(28),
topRight: Radius.circular(28)
)
),
2023-12-09 04:04:14 +01:00
child: SafeArea(
child: _Content(
type: type,
onConfirm: onConfirm,
),
2023-11-19 04:56:51 +01:00
)
),
);
}
}
2022-10-06 00:47:35 +02:00
}
2023-11-19 04:56:51 +01:00
class _Content extends StatefulWidget {
final AccessSettingsList type;
final void Function(String, AccessSettingsList) onConfirm;
const _Content({
required this.type,
required this.onConfirm,
});
2022-10-06 00:47:35 +02:00
2023-11-19 04:56:51 +01:00
@override
State<_Content> createState() => _ContentState();
}
class _ContentState extends State<_Content> {
TextEditingController fieldController = TextEditingController();
2022-10-09 01:13:55 +02:00
bool validData = false;
2022-10-06 00:47:35 +02:00
2022-10-09 01:13:55 +02:00
void checkValidValues() {
if (fieldController.text != '') {
setState(() => validData = true);
2022-10-06 00:47:35 +02:00
}
else {
2022-10-09 01:13:55 +02:00
setState(() => validData = false);
2022-10-06 00:47:35 +02:00
}
}
2023-11-19 04:56:51 +01:00
2022-10-06 00:47:35 +02:00
@override
Widget build(BuildContext context) {
2022-10-09 01:13:55 +02:00
IconData icon() {
switch (widget.type) {
2023-11-19 04:56:51 +01:00
case AccessSettingsList.allowed:
2022-10-09 01:13:55 +02:00
return Icons.check;
2023-11-19 04:56:51 +01:00
case AccessSettingsList.disallowed:
2022-10-09 01:13:55 +02:00
return Icons.block;
2023-11-19 04:56:51 +01:00
case AccessSettingsList.domains:
2022-10-09 01:13:55 +02:00
return Icons.link_rounded;
default:
return Icons.check;
}
}
String title() {
switch (widget.type) {
2023-11-19 04:56:51 +01:00
case AccessSettingsList.allowed:
2022-10-09 01:13:55 +02:00
return AppLocalizations.of(context)!.allowClient;
2023-11-19 04:56:51 +01:00
case AccessSettingsList.disallowed:
2022-10-09 01:13:55 +02:00
return AppLocalizations.of(context)!.disallowClient;
2023-11-19 04:56:51 +01:00
case AccessSettingsList.domains:
2022-10-09 01:13:55 +02:00
return AppLocalizations.of(context)!.disallowedDomains;
default:
return "";
}
}
2023-11-19 04:56:51 +01:00
return Padding(
padding: const EdgeInsets.all(24),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Flexible(
child: SingleChildScrollView(
child: Wrap(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
icon(),
size: 24,
color: Theme.of(context).listTileTheme.iconColor
),
],
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 16),
child: Row(
2023-05-01 04:49:40 +02:00
mainAxisAlignment: MainAxisAlignment.center,
children: [
2023-11-19 04:56:51 +01:00
Text(
title(),
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 24,
color: Theme.of(context).colorScheme.onSurface
2023-05-01 04:49:40 +02:00
),
),
2023-11-19 04:56:51 +01:00
],
2023-05-01 04:49:40 +02:00
),
),
2023-11-19 04:56:51 +01:00
TextFormField(
controller: fieldController,
onChanged: (_) => checkValidValues(),
decoration: InputDecoration(
prefixIcon: const Icon(Icons.link_rounded),
border: const OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(10)
)
),
2023-11-19 04:56:51 +01:00
helperText: widget.type == AccessSettingsList.allowed || widget.type == AccessSettingsList.disallowed
? AppLocalizations.of(context)!.addClientFieldDescription : null,
labelText: widget.type == AccessSettingsList.allowed || widget.type == AccessSettingsList.disallowed
? AppLocalizations.of(context)!.clientIdentifier
: AppLocalizations.of(context)!.domain,
),
2022-10-06 00:47:35 +02:00
),
],
),
2023-03-16 23:29:18 +01:00
),
2023-04-30 23:36:02 +02:00
),
2023-11-19 04:56:51 +01:00
Padding(
padding: const EdgeInsets.only(top: 24),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
TextButton(
onPressed: () => Navigator.pop(context),
child: Text(AppLocalizations.of(context)!.cancel)
),
const SizedBox(width: 16),
TextButton(
onPressed: validData == true
? () {
Navigator.pop(context);
widget.onConfirm(fieldController.text, widget.type);
}
: null,
child: Text(
AppLocalizations.of(context)!.confirm,
style: TextStyle(
color: validData == true
? Theme.of(context).colorScheme.primary
: Colors.grey
),
)
),
],
),
2023-04-30 23:36:02 +02:00
),
2023-11-19 04:56:51 +01:00
],
),
);
2022-10-06 00:47:35 +02:00
}
}