nc-photos/app/lib/widget/fancy_option_picker.dart

92 lines
2.2 KiB
Dart
Raw Normal View History

2021-07-11 03:25:23 +08:00
import 'package:flutter/material.dart';
2023-06-06 21:39:58 +08:00
import 'package:nc_photos/object_extension.dart';
2021-07-11 03:25:23 +08:00
class FancyOptionPickerItem {
2023-06-06 21:39:58 +08:00
const FancyOptionPickerItem({
2021-07-24 04:05:57 +08:00
required this.label,
2021-08-29 04:58:10 +08:00
this.description,
2021-07-11 03:25:23 +08:00
this.isSelected = false,
this.onSelect,
2022-01-29 21:10:48 +08:00
this.onUnselect,
this.dense = false,
2021-07-11 03:25:23 +08:00
});
2023-06-06 21:39:58 +08:00
final String label;
final String? description;
final bool isSelected;
final VoidCallback? onSelect;
final VoidCallback? onUnselect;
final bool dense;
2021-07-11 03:25:23 +08:00
}
/// A fancy looking dialog to pick an option
class FancyOptionPicker extends StatelessWidget {
2021-09-15 14:58:06 +08:00
const FancyOptionPicker({
2021-07-24 04:05:57 +08:00
Key? key,
2021-07-11 03:25:23 +08:00
this.title,
2021-07-24 04:05:57 +08:00
required this.items,
2021-07-11 03:25:23 +08:00
}) : super(key: key);
@override
2023-06-06 21:39:58 +08:00
Widget build(BuildContext context) {
2021-07-11 03:25:23 +08:00
return SimpleDialog(
2021-07-24 04:05:57 +08:00
title: title != null ? Text(title!) : null,
2021-07-11 03:25:23 +08:00
children: items
.map((e) => SimpleDialogOption(
2023-06-06 21:39:58 +08:00
child: FancyOptionPickerItemView(
label: e.label,
description: e.description,
isSelected: e.isSelected,
onSelect: e.onSelect,
onUnselect: e.onUnselect,
dense: e.dense,
2021-07-11 03:25:23 +08:00
),
))
.toList(),
);
}
2021-07-24 04:05:57 +08:00
final String? title;
2021-07-11 03:25:23 +08:00
final List<FancyOptionPickerItem> items;
}
2023-06-06 21:39:58 +08:00
class FancyOptionPickerItemView extends StatelessWidget {
const FancyOptionPickerItemView({
super.key,
required this.label,
this.description,
required this.isSelected,
this.onSelect,
this.onUnselect,
required this.dense,
});
@override
Widget build(BuildContext context) {
return ListTile(
leading: Icon(
isSelected ? Icons.check : null,
color: Theme.of(context).colorScheme.primary,
),
title: Text(
label,
style: isSelected
? TextStyle(
color: Theme.of(context).colorScheme.primary,
)
: null,
),
subtitle: description?.run(Text.new),
onTap: isSelected ? onUnselect : onSelect,
dense: dense,
);
}
final String label;
final String? description;
final bool isSelected;
final VoidCallback? onSelect;
final VoidCallback? onUnselect;
final bool dense;
}