Files
umbrix/lib/features/settings/widgets/settings_input_dialog.dart

260 lines
7.4 KiB
Dart
Raw Normal View History

2023-07-06 17:18:41 +03:30
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
2023-12-01 12:56:24 +03:30
import 'package:hiddify/core/localization/translations.dart';
2023-07-06 17:18:41 +03:30
import 'package:hiddify/utils/utils.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
2023-09-01 15:00:41 +03:30
class SettingsInputDialog<T> extends HookConsumerWidget with PresLogger {
const SettingsInputDialog({
2023-07-06 17:18:41 +03:30
super.key,
required this.title,
2023-09-01 15:00:41 +03:30
required this.initialValue,
this.mapTo,
this.validator,
2024-03-02 22:53:14 +03:30
this.valueFormatter,
this.onReset,
2023-09-28 14:03:45 +03:30
this.optionalAction,
2023-07-06 17:18:41 +03:30
this.icon,
2023-09-01 15:00:41 +03:30
this.digitsOnly = false,
2023-07-06 17:18:41 +03:30
});
final String title;
2023-09-01 15:00:41 +03:30
final T initialValue;
final T? Function(String value)? mapTo;
final bool Function(String value)? validator;
2024-03-02 22:53:14 +03:30
final String Function(T value)? valueFormatter;
final VoidCallback? onReset;
2023-09-28 14:03:45 +03:30
final (String text, VoidCallback)? optionalAction;
2023-07-06 17:18:41 +03:30
final IconData? icon;
2023-09-01 15:00:41 +03:30
final bool digitsOnly;
2023-07-06 17:18:41 +03:30
2023-09-01 15:00:41 +03:30
Future<T?> show(BuildContext context) async {
2023-07-06 17:18:41 +03:30
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(
2024-03-02 22:53:14 +03:30
text: valueFormatter?.call(initialValue) ?? initialValue.toString(),
2023-07-06 17:18:41 +03:30
);
2023-12-11 19:06:05 +03:30
return FocusTraversalGroup(
policy: OrderedTraversalPolicy(),
child: AlertDialog(
title: Text(title),
icon: icon != null ? Icon(icon) : null,
content: FocusTraversalOrder(
order: const NumericFocusOrder(1),
child: CustomTextFormField(
controller: textController,
inputFormatters: [
FilteringTextInputFormatter.singleLineFormatter,
if (digitsOnly) FilteringTextInputFormatter.digitsOnly,
],
autoCorrect: true,
hint: title,
2023-09-28 14:03:45 +03:30
),
2023-07-06 17:18:41 +03:30
),
2023-12-11 19:06:05 +03:30
actions: [
if (optionalAction != null)
FocusTraversalOrder(
order: const NumericFocusOrder(5),
child: TextButton(
onPressed: () async {
optionalAction!.$2();
await Navigator.of(context)
.maybePop(T == String ? textController.value.text : null);
},
child: Text(optionalAction!.$1.toUpperCase()),
),
),
2024-03-02 22:53:14 +03:30
if (onReset != null)
2023-12-11 19:06:05 +03:30
FocusTraversalOrder(
order: const NumericFocusOrder(4),
child: TextButton(
onPressed: () async {
2024-03-02 22:53:14 +03:30
onReset!();
await Navigator.of(context).maybePop(null);
2023-12-11 19:06:05 +03:30
},
child: Text(t.general.reset.toUpperCase()),
),
),
FocusTraversalOrder(
order: const NumericFocusOrder(3),
child: TextButton(
onPressed: () async {
await Navigator.of(context).maybePop();
},
child: Text(localizations.cancelButtonLabel.toUpperCase()),
),
),
FocusTraversalOrder(
order: const NumericFocusOrder(2),
child: 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()),
),
),
],
),
2023-07-06 17:18:41 +03:30
);
}
}
2023-08-24 14:50:24 +03:30
2023-09-01 15:00:41 +03:30
class SettingsPickerDialog<T> extends HookConsumerWidget with PresLogger {
const SettingsPickerDialog({
2023-08-24 14:50:24 +03:30
super.key,
required this.title,
2023-09-01 15:00:41 +03:30
required this.selected,
required this.options,
required this.getTitle,
2024-03-02 22:53:14 +03:30
this.onReset,
2023-08-24 14:50:24 +03:30
});
final String title;
2023-09-01 15:00:41 +03:30
final T selected;
final List<T> options;
final String Function(T e) getTitle;
2024-03-02 22:53:14 +03:30
final VoidCallback? onReset;
2023-08-24 14:50:24 +03:30
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),
2023-09-01 15:00:41 +03:30
content: Column(
children: options
.map(
(e) => RadioListTile(
title: Text(getTitle(e)),
value: e,
groupValue: selected,
onChanged: (value) async {
await Navigator.of(context).maybePop(e);
},
2023-09-01 15:00:41 +03:30
),
)
.toList(),
2023-08-24 14:50:24 +03:30
),
actions: [
2024-03-02 22:53:14 +03:30
if (onReset != null)
2023-08-24 14:50:24 +03:30
TextButton(
onPressed: () async {
2024-03-02 22:53:14 +03:30
onReset!();
await Navigator.of(context).maybePop(null);
2023-08-24 14:50:24 +03:30
},
child: Text(t.general.reset.toUpperCase()),
),
TextButton(
onPressed: () async {
await Navigator.of(context).maybePop();
},
child: Text(localizations.cancelButtonLabel.toUpperCase()),
),
],
2023-09-01 15:00:41 +03:30
scrollable: true,
2023-08-24 14:50:24 +03:30
);
}
}
2023-09-05 19:07:13 +03:30
class SettingsSliderDialog extends HookConsumerWidget with PresLogger {
const SettingsSliderDialog({
super.key,
required this.title,
required this.initialValue,
2024-03-02 22:53:14 +03:30
this.onReset,
2023-09-05 19:07:13 +03:30
this.min = 0,
this.max = 1,
this.divisions,
this.labelGen,
});
final String title;
final double initialValue;
2024-03-02 22:53:14 +03:30
final VoidCallback? onReset;
2023-09-05 19:07:13 +03:30
final double min;
final double max;
final int? divisions;
final String Function(double value)? labelGen;
Future<double?> 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 sliderValue = useState(initialValue);
return AlertDialog(
title: Text(title),
content: IntrinsicHeight(
child: Slider(
value: sliderValue.value,
min: min,
max: max,
divisions: divisions,
onChanged: (value) => sliderValue.value = value,
label: labelGen?.call(sliderValue.value),
),
),
actions: [
2024-03-02 22:53:14 +03:30
if (onReset != null)
2023-09-05 19:07:13 +03:30
TextButton(
onPressed: () async {
2024-03-02 22:53:14 +03:30
onReset!();
await Navigator.of(context).maybePop(null);
2023-09-05 19:07:13 +03:30
},
child: Text(t.general.reset.toUpperCase()),
),
TextButton(
onPressed: () async {
await Navigator.of(context).maybePop();
},
child: Text(localizations.cancelButtonLabel.toUpperCase()),
),
TextButton(
onPressed: () async {
await Navigator.of(context).maybePop(sliderValue.value);
},
child: Text(localizations.okButtonLabel.toUpperCase()),
),
],
);
}
}