2024-03-02 22:53:14 +03:30
|
|
|
import 'package:flutter/material.dart';
|
2026-01-17 13:09:20 +03:00
|
|
|
import 'package:umbrix/core/utils/preferences_utils.dart';
|
|
|
|
|
import 'package:umbrix/features/settings/widgets/widgets.dart';
|
2024-03-02 22:53:14 +03:30
|
|
|
|
|
|
|
|
class ValuePreferenceWidget<T> extends StatelessWidget {
|
|
|
|
|
const ValuePreferenceWidget({
|
|
|
|
|
super.key,
|
|
|
|
|
required this.value,
|
|
|
|
|
required this.preferences,
|
|
|
|
|
this.enabled = true,
|
|
|
|
|
required this.title,
|
|
|
|
|
this.presentValue,
|
|
|
|
|
this.formatInputValue,
|
|
|
|
|
this.validateInput,
|
|
|
|
|
this.inputToValue,
|
|
|
|
|
this.digitsOnly = false,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
final T value;
|
|
|
|
|
final PreferencesNotifier<T, dynamic> preferences;
|
|
|
|
|
final bool enabled;
|
|
|
|
|
final String title;
|
|
|
|
|
final String Function(T value)? presentValue;
|
|
|
|
|
final String Function(T value)? formatInputValue;
|
|
|
|
|
final bool Function(String value)? validateInput;
|
|
|
|
|
final T? Function(String input)? inputToValue;
|
|
|
|
|
final bool digitsOnly;
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
return ListTile(
|
|
|
|
|
title: Text(title),
|
|
|
|
|
subtitle: Text(presentValue?.call(value) ?? value.toString()),
|
|
|
|
|
enabled: enabled,
|
|
|
|
|
onTap: () async {
|
|
|
|
|
final inputValue = await SettingsInputDialog(
|
|
|
|
|
title: title,
|
|
|
|
|
initialValue: value,
|
|
|
|
|
validator: validateInput,
|
|
|
|
|
valueFormatter: formatInputValue,
|
|
|
|
|
onReset: preferences.reset,
|
|
|
|
|
digitsOnly: digitsOnly,
|
|
|
|
|
mapTo: inputToValue,
|
2024-08-20 01:53:26 -04:00
|
|
|
possibleValues: preferences.possibleValues,
|
2024-03-02 22:53:14 +03:30
|
|
|
).show(context);
|
|
|
|
|
if (inputValue == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
await preferences.update(inputValue);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class ChoicePreferenceWidget<T> extends StatelessWidget {
|
|
|
|
|
const ChoicePreferenceWidget({
|
|
|
|
|
super.key,
|
|
|
|
|
required this.selected,
|
|
|
|
|
required this.preferences,
|
|
|
|
|
this.enabled = true,
|
|
|
|
|
required this.choices,
|
|
|
|
|
required this.title,
|
|
|
|
|
required this.presentChoice,
|
|
|
|
|
this.validateInput,
|
2024-07-27 23:51:54 +02:00
|
|
|
this.onChanged,
|
2024-03-02 22:53:14 +03:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
final T selected;
|
|
|
|
|
final PreferencesNotifier<T, dynamic> preferences;
|
|
|
|
|
final bool enabled;
|
|
|
|
|
final List<T> choices;
|
|
|
|
|
final String title;
|
|
|
|
|
final String Function(T value) presentChoice;
|
|
|
|
|
final bool Function(String value)? validateInput;
|
2024-07-27 23:51:54 +02:00
|
|
|
final ValueChanged<T>? onChanged;
|
2024-03-02 22:53:14 +03:30
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
return ListTile(
|
|
|
|
|
title: Text(title),
|
|
|
|
|
subtitle: Text(presentChoice(selected)),
|
|
|
|
|
enabled: enabled,
|
|
|
|
|
onTap: () async {
|
|
|
|
|
final selection = await SettingsPickerDialog(
|
|
|
|
|
title: title,
|
|
|
|
|
selected: selected,
|
|
|
|
|
options: choices,
|
|
|
|
|
getTitle: (e) => presentChoice(e),
|
|
|
|
|
onReset: preferences.reset,
|
|
|
|
|
).show(context);
|
|
|
|
|
if (selection == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-07-27 23:51:54 +02:00
|
|
|
final out = await preferences.update(selection);
|
|
|
|
|
onChanged?.call(selection);
|
|
|
|
|
return out;
|
2024-03-02 22:53:14 +03:30
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|