Skip to content

Commit

Permalink
feat(example): allow subclassing ElementSelectorData to add additio…
Browse files Browse the repository at this point in the history
…nal fields
  • Loading branch information
benthillerkus committed Mar 31, 2022
1 parent 3f51b06 commit b816880
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
26 changes: 16 additions & 10 deletions example/edit_icon/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ class HomeScreen extends StatefulWidget {
State<HomeScreen> createState() => _HomeScreenState();
}

class MyData extends ElementSelectorData {
MyData({Widget? Function(BuildContext)? builder, Key? key, this.name})
: super(builder: builder, key: key);

final String? name;
}

class _HomeScreenState extends State<HomeScreen> {
late final TrayIcon _icon = TrayIcon(const TrayIconData());

Expand All @@ -48,21 +55,21 @@ class _HomeScreenState extends State<HomeScreen> {
super.dispose();
}

final _items = [
ElementSelectorData(builder: (_) => const FlutterLogo()),
ElementSelectorData(builder: (_) => const FlutterLogo()),
ElementSelectorData(builder: (_) => const FlutterLogo()),
];

late final _delegate = ElementSelectorDelegate(initialItems: _items);
final _delegate = ElementSelectorDelegate(initialItems: [
MyData(name: "A", builder: (_) => const FlutterLogo()),
MyData(name: "B", builder: (_) => const FlutterLogo()),
MyData(builder: (_) => const FlutterLogo()),
]);

@override
Widget build(BuildContext context) {
return Scaffold(
body: ElementSelector(
axis: Axis.vertical,
onSelectionChanged: (index) {
_icon.setTooltip(_delegate.elementAt(index).key.toString());
final element = _delegate.elementAt(index);

_icon.setTooltip(element.name ?? element.key.toString());
_icon.setIcon();
_icon.show();
},
Expand All @@ -73,8 +80,7 @@ class _HomeScreenState extends State<HomeScreen> {
type: FileType.custom, allowedExtensions: const ["ico", "png"]);
if (result == null) return;
final path = result.files.first.path!;
_delegate
.add(ElementSelectorData(builder: (_) => Image.file(File(path))));
_delegate.add(MyData(builder: (_) => Image.file(File(path))));
},
));
}
Expand Down
14 changes: 7 additions & 7 deletions example/edit_icon/lib/view/element_selector/delegate.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import 'data.dart';
import 'subscription.dart';

class ElementSelectorDelegate {
ElementSelectorDelegate({Iterable<ElementSelectorData>? initialItems}) {
_items = initialItems?.toList() ?? <ElementSelectorData>[];
class ElementSelectorDelegate<DataType extends ElementSelectorData> {
ElementSelectorDelegate({Iterable<DataType>? initialItems}) {
_items = initialItems?.toList() ?? <DataType>[];
}

late final List<ElementSelectorData> _items;
late final List<DataType> _items;
get numItems => _items.length;
final List<ElementSelectorDelegateSubscription> _subscriptions = [];

void add(ElementSelectorData item) async {
void add(DataType item) async {
_items.add(item);
for (var sub in _subscriptions) {
if (sub.onAdd != null) await sub.onAdd!();
Expand All @@ -36,5 +36,5 @@ class ElementSelectorDelegate {
_subscriptions.remove(sub);
}

ElementSelectorData elementAt(int index) => _items.elementAt(index);
}
DataType elementAt(int index) => _items.elementAt(index);
}

0 comments on commit b816880

Please sign in to comment.