dynamic category groups
This commit is contained in:
parent
6ed01cf61b
commit
6726e544bd
5 changed files with 335 additions and 92 deletions
65
lib/widgets/settings/dialog_list_tile.dart
Normal file
65
lib/widgets/settings/dialog_list_tile.dart
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
import "package:flutter/material.dart";
|
||||
import "package:flutter_riverpod/flutter_riverpod.dart";
|
||||
import "package:nexus/widgets/settings/radio_dialog.dart";
|
||||
|
||||
class DialogListTile<T> extends ConsumerWidget {
|
||||
final T? initialValue;
|
||||
final String title;
|
||||
final String? description;
|
||||
final List<T> options;
|
||||
final bool required;
|
||||
final Icon icon;
|
||||
final void Function(T value)? onChanged;
|
||||
final String Function(T option) getName;
|
||||
const DialogListTile({
|
||||
super.key,
|
||||
required this.icon,
|
||||
required this.title,
|
||||
required this.initialValue,
|
||||
required this.options,
|
||||
required this.onChanged,
|
||||
required this.getName,
|
||||
this.description,
|
||||
this.required = true,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) => FormField(
|
||||
validator: (value) =>
|
||||
value == null && required == true ? "This field is required." : null,
|
||||
initialValue: initialValue,
|
||||
builder: (field) => InputDecorator(
|
||||
decoration: InputDecoration(
|
||||
errorText: field.errorText,
|
||||
contentPadding: EdgeInsets.zero,
|
||||
enabledBorder: InputBorder.none,
|
||||
),
|
||||
child: ListTile(
|
||||
onTap: () => showDialog(
|
||||
context: context,
|
||||
builder: (context) => RadioDialog<T>(
|
||||
title: title,
|
||||
getName: getName,
|
||||
onChanged: onChanged == null
|
||||
? null
|
||||
: (value) {
|
||||
field.didChange(value);
|
||||
onChanged!.call(value);
|
||||
},
|
||||
value: field.value,
|
||||
options: options,
|
||||
),
|
||||
),
|
||||
title: Text(title),
|
||||
subtitle: description == null ? null : Text(description!),
|
||||
leading: icon,
|
||||
trailing: Chip(
|
||||
label: Text(
|
||||
field.value == null ? "None" : getName(field.value as T),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
55
lib/widgets/settings/radio_dialog.dart
Normal file
55
lib/widgets/settings/radio_dialog.dart
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
import "package:flutter/material.dart";
|
||||
import "package:flutter_hooks/flutter_hooks.dart";
|
||||
|
||||
class RadioDialog<T> extends HookWidget {
|
||||
final T? value;
|
||||
final String title;
|
||||
final List<T> options;
|
||||
final void Function(T value)? onChanged;
|
||||
final String Function(T option) getName;
|
||||
const RadioDialog({
|
||||
super.key,
|
||||
required this.title,
|
||||
required this.value,
|
||||
required this.options,
|
||||
required this.onChanged,
|
||||
required this.getName,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final mutValue = useState<T?>(null);
|
||||
return AlertDialog(
|
||||
title: Text(title),
|
||||
content: RadioGroup<T>(
|
||||
groupValue: mutValue.value ?? value,
|
||||
onChanged: (value) => mutValue.value = value ?? mutValue.value,
|
||||
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: options
|
||||
.map(
|
||||
(option) => RadioListTile<T>(
|
||||
enabled: onChanged != null,
|
||||
value: option,
|
||||
title: Text(getName(option)),
|
||||
dense: true,
|
||||
),
|
||||
)
|
||||
.toList(),
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(onPressed: Navigator.of(context).pop, child: Text("Cancel")),
|
||||
if (onChanged != null)
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
if (mutValue.value != null) onChanged!(mutValue.value as T);
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
child: Text("OK"),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue