From b8168807c6c0ea5365333dbb1c06bec360390025 Mon Sep 17 00:00:00 2001 From: Bent Hillerkus <29630575+benthillerkus@users.noreply.github.com> Date: Thu, 31 Mar 2022 22:48:26 +0200 Subject: [PATCH] feat(example): allow subclassing `ElementSelectorData` to add additional fields --- example/edit_icon/lib/main.dart | 26 ++++++++++++------- .../lib/view/element_selector/delegate.dart | 14 +++++----- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/example/edit_icon/lib/main.dart b/example/edit_icon/lib/main.dart index 7da7a8f..dde5744 100644 --- a/example/edit_icon/lib/main.dart +++ b/example/edit_icon/lib/main.dart @@ -39,6 +39,13 @@ class HomeScreen extends StatefulWidget { State 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 { late final TrayIcon _icon = TrayIcon(const TrayIconData()); @@ -48,13 +55,11 @@ class _HomeScreenState extends State { 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) { @@ -62,7 +67,9 @@ class _HomeScreenState extends State { 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(); }, @@ -73,8 +80,7 @@ class _HomeScreenState extends State { 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)))); }, )); } diff --git a/example/edit_icon/lib/view/element_selector/delegate.dart b/example/edit_icon/lib/view/element_selector/delegate.dart index 972e75c..15e0f4f 100644 --- a/example/edit_icon/lib/view/element_selector/delegate.dart +++ b/example/edit_icon/lib/view/element_selector/delegate.dart @@ -1,16 +1,16 @@ import 'data.dart'; import 'subscription.dart'; -class ElementSelectorDelegate { - ElementSelectorDelegate({Iterable? initialItems}) { - _items = initialItems?.toList() ?? []; +class ElementSelectorDelegate { + ElementSelectorDelegate({Iterable? initialItems}) { + _items = initialItems?.toList() ?? []; } - late final List _items; + late final List _items; get numItems => _items.length; final List _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!(); @@ -36,5 +36,5 @@ class ElementSelectorDelegate { _subscriptions.remove(sub); } - ElementSelectorData elementAt(int index) => _items.elementAt(index); -} \ No newline at end of file + DataType elementAt(int index) => _items.elementAt(index); +}