2022-10-15 20:52:31 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
|
|
|
|
|
|
|
import 'package:adguard_home_manager/models/rewrite_rules.dart';
|
|
|
|
|
|
|
|
class AddDnsRewriteModal extends StatefulWidget {
|
|
|
|
final void Function(RewriteRulesData) onConfirm;
|
|
|
|
|
|
|
|
const AddDnsRewriteModal({
|
|
|
|
Key? key,
|
|
|
|
required this.onConfirm
|
|
|
|
}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<AddDnsRewriteModal> createState() => _AddDnsRewriteModalState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _AddDnsRewriteModalState extends State<AddDnsRewriteModal> {
|
|
|
|
final TextEditingController domainController = TextEditingController();
|
|
|
|
String? domainError;
|
|
|
|
final TextEditingController answerController = TextEditingController();
|
|
|
|
|
|
|
|
bool validData = false;
|
|
|
|
|
|
|
|
void validateDomain(String value) {
|
|
|
|
final domainRegex = RegExp(r'^([a-z0-9|-]+\.)*[a-z0-9|-]+\.[a-z]+$');
|
|
|
|
if (domainRegex.hasMatch(value)) {
|
|
|
|
setState(() => domainError = null);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
setState(() => domainError = AppLocalizations.of(context)!.domainNotValid);
|
|
|
|
}
|
|
|
|
checkValidValues();
|
|
|
|
}
|
|
|
|
|
|
|
|
void checkValidValues() {
|
|
|
|
if (
|
|
|
|
domainController.text != '' &&
|
|
|
|
domainError == null &&
|
|
|
|
answerController.text != ''
|
|
|
|
) {
|
|
|
|
setState(() => validData = true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
setState(() => validData = false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Padding(
|
|
|
|
padding: MediaQuery.of(context).viewInsets,
|
|
|
|
child: Container(
|
2022-11-05 01:09:09 +01:00
|
|
|
height: 400,
|
2022-10-15 20:52:31 +02:00
|
|
|
decoration: BoxDecoration(
|
|
|
|
borderRadius: const BorderRadius.only(
|
|
|
|
topLeft: Radius.circular(28),
|
|
|
|
topRight: Radius.circular(28)
|
|
|
|
),
|
|
|
|
color: Theme.of(context).dialogBackgroundColor,
|
|
|
|
),
|
|
|
|
child: Column(
|
|
|
|
children: [
|
2022-10-21 11:26:14 +02:00
|
|
|
Expanded(
|
|
|
|
child: ListView(
|
|
|
|
physics: 410 < MediaQuery.of(context).size.height
|
|
|
|
? const NeverScrollableScrollPhysics()
|
|
|
|
: null,
|
|
|
|
children: [
|
2022-11-05 01:09:09 +01:00
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.only(top: 24),
|
2022-10-21 11:26:14 +02:00
|
|
|
child: Icon(
|
|
|
|
Icons.add,
|
2022-11-05 01:09:09 +01:00
|
|
|
size: 24,
|
2022-11-05 02:35:35 +01:00
|
|
|
color: Theme.of(context).listTileTheme.iconColor
|
2022-10-21 11:26:14 +02:00
|
|
|
),
|
|
|
|
),
|
2022-11-05 01:09:09 +01:00
|
|
|
const SizedBox(height: 16),
|
2022-10-21 11:26:14 +02:00
|
|
|
Text(
|
|
|
|
AppLocalizations.of(context)!.addDnsRewrite,
|
|
|
|
textAlign: TextAlign.center,
|
2022-11-05 01:09:09 +01:00
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 24,
|
|
|
|
color: Theme.of(context).colorScheme.onSurface
|
2022-10-21 11:26:14 +02:00
|
|
|
),
|
|
|
|
),
|
2022-11-05 01:09:09 +01:00
|
|
|
const SizedBox(height: 16),
|
2022-10-21 11:26:14 +02:00
|
|
|
Padding(
|
2022-11-05 01:09:09 +01:00
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 24),
|
2022-10-21 11:26:14 +02:00
|
|
|
child: TextFormField(
|
|
|
|
controller: domainController,
|
|
|
|
onChanged: validateDomain,
|
|
|
|
decoration: InputDecoration(
|
|
|
|
prefixIcon: const Icon(Icons.link_rounded),
|
|
|
|
border: const OutlineInputBorder(
|
|
|
|
borderRadius: BorderRadius.all(
|
|
|
|
Radius.circular(10)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
errorText: domainError,
|
|
|
|
labelText: AppLocalizations.of(context)!.domain,
|
|
|
|
),
|
|
|
|
),
|
2022-10-15 20:52:31 +02:00
|
|
|
),
|
2022-10-21 11:26:14 +02:00
|
|
|
const SizedBox(height: 30),
|
|
|
|
Padding(
|
2022-11-05 01:09:09 +01:00
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 24),
|
2022-10-21 11:26:14 +02:00
|
|
|
child: TextFormField(
|
|
|
|
controller: answerController,
|
|
|
|
onChanged: (_) => checkValidValues(),
|
|
|
|
decoration: InputDecoration(
|
|
|
|
prefixIcon: const Icon(Icons.system_update_alt_rounded),
|
|
|
|
border: const OutlineInputBorder(
|
|
|
|
borderRadius: BorderRadius.all(
|
|
|
|
Radius.circular(10)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
labelText: AppLocalizations.of(context)!.answer,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
2022-10-15 20:52:31 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
Padding(
|
2022-11-05 01:09:09 +01:00
|
|
|
padding: const EdgeInsets.all(24),
|
2022-10-21 11:26:14 +02:00
|
|
|
child: Row(
|
2022-10-15 20:52:31 +02:00
|
|
|
mainAxisAlignment: MainAxisAlignment.end,
|
|
|
|
children: [
|
2022-10-21 11:26:14 +02:00
|
|
|
TextButton(
|
|
|
|
onPressed: () => Navigator.pop(context),
|
|
|
|
child: Text(AppLocalizations.of(context)!.cancel),
|
|
|
|
),
|
|
|
|
const SizedBox(width: 20),
|
|
|
|
TextButton(
|
|
|
|
onPressed: validData == true
|
|
|
|
? () {
|
|
|
|
Navigator.pop(context);
|
|
|
|
widget.onConfirm(
|
|
|
|
RewriteRulesData(
|
|
|
|
domain: domainController.text,
|
|
|
|
answer: answerController.text
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
: null,
|
|
|
|
child: Text(
|
|
|
|
AppLocalizations.of(context)!.confirm,
|
|
|
|
style: TextStyle(
|
|
|
|
color: validData == true
|
2023-01-25 20:51:23 +01:00
|
|
|
? Theme.of(context).colorScheme.primary
|
2022-11-05 01:09:09 +01:00
|
|
|
: Theme.of(context).colorScheme.onSurface.withOpacity(0.38)
|
2022-10-21 11:26:14 +02:00
|
|
|
),
|
2022-10-15 20:52:31 +02:00
|
|
|
),
|
2022-10-21 11:26:14 +02:00
|
|
|
),
|
2022-10-15 20:52:31 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|