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

322 lines
11 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';
import 'package:flutter_typeahead/flutter_typeahead.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({super.key, required this.title, required this.initialValue, this.mapTo, this.validator, this.valueFormatter, this.onReset, this.optionalAction, this.icon, this.digitsOnly = false, this.possibleValues});
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 List<T>? possibleValues;
2024-03-02 22:53:14 +03:30
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: Column(
mainAxisSize: MainAxisSize.min,
children: [
if (possibleValues != null)
2024-08-20 13:18:26 -04:00
// AutocompleteField(initialValue: initialValue.toString(), options: possibleValues!.map((e) => e.toString()).toList())
TypeAheadField<String>(
controller: textController,
builder: (context, controller, focusNode) {
return TextField(
controller: controller,
focusNode: focusNode,
textDirection: TextDirection.ltr,
autofocus: true,
// decoration: InputDecoration(
// // border: OutlineInputBorder(),
// // labelText: 'City',
// )
);
},
// Callback to fetch suggestions based on user input
suggestionsCallback: (pattern) async {
final items = possibleValues!.map((p) => p.toString());
var res = items.where((suggestion) => suggestion.toLowerCase().contains(pattern.toLowerCase())).toList();
if (res.length <= 1) res = [pattern, ...items.where((s) => s != pattern)];
return res;
},
// Widget to build each suggestion in the list
itemBuilder: (context, suggestion) {
return ListTile(
contentPadding: const EdgeInsets.symmetric(vertical: 3, horizontal: 10), // Minimize ListTile padding
minTileHeight: 0,
title: Text(
suggestion,
textDirection: TextDirection.ltr,
style: Theme.of(context).textTheme.bodySmall,
),
);
},
// Callback when a suggestion is selected
onSelected: (suggestion) {
// Handle the selected suggestion
print('Selected: $suggestion');
textController.text = suggestion.toString();
},
)
else
CustomTextFormField(
controller: textController,
inputFormatters: [
FilteringTextInputFormatter.singleLineFormatter,
if (digitsOnly) FilteringTextInputFormatter.digitsOnly,
],
autoCorrect: true,
hint: title,
),
2023-12-11 19:06:05 +03:30
],
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);
2023-12-11 19:06:05 +03:30
},
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));
2023-12-11 19:06:05 +03:30
} else {
await Navigator.of(context).maybePop(T == String ? textController.value.text : null);
2023-12-11 19:06:05 +03:30
}
},
child: Text(localizations.okButtonLabel.toUpperCase()),
),
),
],
),
2023-07-06 17:18:41 +03:30
);
}
}
2023-08-24 14:50:24 +03:30
class AutocompleteField extends StatelessWidget {
const AutocompleteField({super.key, required this.initialValue, required this.options});
final List<String> options;
final String initialValue;
@override
Widget build(BuildContext context) {
return Autocomplete<String>(
initialValue: TextEditingValue(
text: this.initialValue, selection: TextSelection(baseOffset: 0, extentOffset: this.initialValue.length), // Selects the entire text
),
optionsBuilder: (TextEditingValue textEditingValue) {
// if (textEditingValue.text == '') {
// return const Iterable<String>.empty();
// }
return options.where((String option) {
return option.contains(textEditingValue.text.toLowerCase());
});
},
onSelected: (String selection) {
//debugPrint('You just selected $selection');
},
);
}
}
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()),
),
],
);
}
}