Files
umbrix/lib/features/settings/widgets/settings_input_dialog.dart
problematicconsumer 2841c4b6ea Add config options
2023-09-01 15:00:41 +03:30

152 lines
4.2 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:go_router/go_router.dart';
import 'package:hiddify/core/core_providers.dart';
import 'package:hiddify/utils/utils.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
class SettingsInputDialog<T> extends HookConsumerWidget with PresLogger {
const SettingsInputDialog({
super.key,
required this.title,
required this.initialValue,
this.mapTo,
this.validator,
this.resetValue,
this.icon,
this.digitsOnly = false,
});
final String title;
final T initialValue;
final T? Function(String value)? mapTo;
final bool Function(String value)? validator;
final T? resetValue;
final IconData? icon;
final bool digitsOnly;
Future<T?> show(BuildContext context) async {
return showDialog(
context: context,
useRootNavigator: true,
builder: (context) => this,
);
}
@override
Widget build(BuildContext context, WidgetRef ref) {
final t = ref.watch(translationsProvider);
final localizations = MaterialLocalizations.of(context);
final textController = useTextEditingController(
text: initialValue?.toString(),
);
return AlertDialog(
title: Text(title),
icon: icon != null ? Icon(icon) : null,
content: TextFormField(
controller: textController,
inputFormatters: [
FilteringTextInputFormatter.singleLineFormatter,
if (digitsOnly) FilteringTextInputFormatter.digitsOnly,
],
autovalidateMode: AutovalidateMode.always,
),
actions: [
if (resetValue != null)
TextButton(
onPressed: () async {
await Navigator.of(context).maybePop(resetValue);
},
child: Text(t.general.reset.toUpperCase()),
),
TextButton(
onPressed: () async {
await Navigator.of(context).maybePop();
},
child: Text(localizations.cancelButtonLabel.toUpperCase()),
),
TextButton(
onPressed: () async {
if (validator?.call(textController.value.text) == false) {
await Navigator.of(context).maybePop(null);
} else if (mapTo != null) {
await Navigator.of(context)
.maybePop(mapTo!.call(textController.value.text));
} else {
await Navigator.of(context)
.maybePop(T == String ? textController.value.text : null);
}
},
child: Text(localizations.okButtonLabel.toUpperCase()),
),
],
);
}
}
class SettingsPickerDialog<T> extends HookConsumerWidget with PresLogger {
const SettingsPickerDialog({
super.key,
required this.title,
required this.selected,
required this.options,
required this.getTitle,
this.resetValue,
});
final String title;
final T selected;
final List<T> options;
final String Function(T e) getTitle;
final T? resetValue;
Future<T?> show(BuildContext context) async {
return showDialog(
context: context,
useRootNavigator: true,
builder: (context) => this,
);
}
@override
Widget build(BuildContext context, WidgetRef ref) {
final t = ref.watch(translationsProvider);
final localizations = MaterialLocalizations.of(context);
return AlertDialog(
title: Text(title),
content: Column(
children: options
.map(
(e) => RadioListTile(
title: Text(getTitle(e)),
value: e,
groupValue: selected,
onChanged: (value) => context.pop(e),
),
)
.toList(),
),
actions: [
if (resetValue != null)
TextButton(
onPressed: () async {
await Navigator.of(context).maybePop(resetValue);
},
child: Text(t.general.reset.toUpperCase()),
),
TextButton(
onPressed: () async {
await Navigator.of(context).maybePop();
},
child: Text(localizations.cancelButtonLabel.toUpperCase()),
),
],
scrollable: true,
);
}
}