From 758c1b1b2bf6bbd99884d21c0eb2f7b3e28c1fcf Mon Sep 17 00:00:00 2001 From: Gautier Date: Wed, 5 Jun 2024 15:55:08 +0200 Subject: [PATCH] chore: Remove unused anchorKeyId property in AnchoredHelper --- example/lib/main.dart | 3 +- example/pubspec.lock | 28 +++++------ lib/anchored/anchored_helper_widget.dart | 9 ++-- lib/helper_orchestrator.dart | 46 ++++++++++++------- lib/services/element_finder.dart | 24 ++++++++++ lib/services/overlay_helper.dart | 2 +- .../widgets/anchored_circle_no_buttons.dart | 1 - .../widgets/anchored_circle_page_.dart | 1 - .../widgets/anchored_rect_no_buttons.dart | 1 - test/anchored/widgets/anchored_rect_page.dart | 1 - 10 files changed, 75 insertions(+), 41 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 0556b03..230b4e5 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -109,7 +109,6 @@ class _MyHomePageState extends State { } AnchoredHelper get helper => AnchoredHelper( - anchorKeyId: 'btn', title: const Text( 'Push to increment', textAlign: TextAlign.center, @@ -137,6 +136,6 @@ class _MyHomePageState extends State { // onRightTap: () => HelperOrchestrator.of(context).hideHelper(), // onLeftBtnTap: () => HelperOrchestrator.of(context).hideHelper(), onTapAnchor: () => HelperOrchestrator.of(context).hideHelper(), - widgetFactory: AnchoredRectHoleHelper.anchorFactory, + widgetFactory: AnchoredCircleHoleHelper.anchorFactory, ); } diff --git a/example/pubspec.lock b/example/pubspec.lock index 51ce41a..244a185 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -79,26 +79,26 @@ packages: dependency: transitive description: name: leak_tracker - sha256: "78eb209deea09858f5269f5a5b02be4049535f568c07b275096836f01ea323fa" + sha256: "7f0df31977cb2c0b88585095d168e689669a2cc9b97c309665e3386f3e9d341a" url: "https://pub.dev" source: hosted - version: "10.0.0" + version: "10.0.4" leak_tracker_flutter_testing: dependency: transitive description: name: leak_tracker_flutter_testing - sha256: b46c5e37c19120a8a01918cfaf293547f47269f7cb4b0058f21531c2465d6ef0 + sha256: "06e98f569d004c1315b991ded39924b21af84cf14cc94791b8aea337d25b57f8" url: "https://pub.dev" source: hosted - version: "2.0.1" + version: "3.0.3" leak_tracker_testing: dependency: transitive description: name: leak_tracker_testing - sha256: a597f72a664dbd293f3bfc51f9ba69816f84dcd403cdac7066cb3f6003f3ab47 + sha256: "6ba465d5d76e67ddf503e1161d1f4a6bc42306f9d66ca1e8f079a47290fb06d3" url: "https://pub.dev" source: hosted - version: "2.0.1" + version: "3.0.1" lints: dependency: transitive description: @@ -127,10 +127,10 @@ packages: dependency: transitive description: name: meta - sha256: d584fa6707a52763a52446f02cc621b077888fb63b93bbcb1143a7be5a0c0c04 + sha256: "7687075e408b093f36e6bbf6c91878cc0d4cd10f409506f7bc996f68220b9136" url: "https://pub.dev" source: hosted - version: "1.11.0" + version: "1.12.0" pal_widgets: dependency: "direct main" description: @@ -195,10 +195,10 @@ packages: dependency: transitive description: name: test_api - sha256: "5c2f730018264d276c20e4f1503fd1308dfbbae39ec8ee63c5236311ac06954b" + sha256: "9955ae474176f7ac8ee4e989dadfb411a58c30415bcfb648fa04b2b8a03afa7f" url: "https://pub.dev" source: hosted - version: "0.6.1" + version: "0.7.0" vector_math: dependency: transitive description: @@ -211,10 +211,10 @@ packages: dependency: transitive description: name: vm_service - sha256: b3d56ff4341b8f182b96aceb2fa20e3dcb336b9f867bc0eafc0de10f1048e957 + sha256: "3923c89304b715fb1eb6423f017651664a03bf5f4b29983627c4da791f74a4ec" url: "https://pub.dev" source: hosted - version: "13.0.0" + version: "14.2.1" sdks: - dart: ">=3.2.0-0 <4.0.0" - flutter: ">=1.17.0" + dart: ">=3.3.0 <4.0.0" + flutter: ">=3.18.0-18.0.pre.54" diff --git a/lib/anchored/anchored_helper_widget.dart b/lib/anchored/anchored_helper_widget.dart index ee463f7..d8d54b6 100644 --- a/lib/anchored/anchored_helper_widget.dart +++ b/lib/anchored/anchored_helper_widget.dart @@ -32,8 +32,6 @@ class AnchorHelperWrapper extends InheritedWidget { /// You are free to add a positiv button / negativ button or an onTap function /// on the anchor. class AnchoredHelper extends StatefulWidget { - /// The reference to the [Key] created by [HelperOrchestrator] - final String anchorKeyId; /// A [Text] widget to show as title final Text? title; @@ -74,7 +72,6 @@ class AnchoredHelper extends StatefulWidget { final AnchorWidgetFactory widgetFactory; const AnchoredHelper({ - required this.anchorKeyId, this.onLeftBtnTap, this.onRightTap, this.rightBtnText, @@ -157,6 +154,7 @@ class _AnchoredHelperState extends State @override Widget build(BuildContext context) { + // print("anchor: ${anchor.offset} ${anchor.size}: ${anchor.rect}"); return GestureDetector( onTap: () async { if(widget.onTapBackground != null) { @@ -191,7 +189,10 @@ class _AnchoredHelperState extends State rect: anchor.rect, child: LayoutBuilder( builder: (context, constraints) => Padding( - padding: const EdgeInsets.symmetric(vertical: 8.0), + padding: const EdgeInsets.symmetric( + horizontal: 16.0, + vertical: 8.0 + ), child: ConstrainedBox( constraints: BoxConstraints( minHeight: constraints.maxHeight, diff --git a/lib/helper_orchestrator.dart b/lib/helper_orchestrator.dart index c212e65..987c01a 100644 --- a/lib/helper_orchestrator.dart +++ b/lib/helper_orchestrator.dart @@ -5,6 +5,9 @@ import 'anchored/anchor_model.dart'; import 'anchored/anchored_helper_widget.dart'; import 'services/overlay_helper.dart'; +/// used to align the helper to the top, bottom, left or right of the anchor +enum HelperAlignment { top, bottom, left, right } + class _HelperOrchestratorScope extends InheritedWidget { final HelperOrchestratorState _helperOrchestratorState; @@ -99,7 +102,7 @@ class HelperOrchestratorState extends State { /// The key will be used to find the element position when requesting /// [showAnchoredHelper] Key generateKey(String key) { - // final uniqueKey = UniqueKey(); + // final uniqueKey = GlobalKey(debugLabel: key); final uniqueKey = ValueKey(key); keys[key] = uniqueKey; return uniqueKey; @@ -120,26 +123,31 @@ class HelperOrchestratorState extends State { /// ```dart /// HelperOrchestrator.of(context).generateKey('myKeyId') /// ``` - Future showAnchoredHelper(String anchorKeyId, AnchoredHelper helper) async { - final anchor = await findAnchor(anchorKeyId); - if (anchor == null) { - debugPrint("anchor cannot be found. show anchored failed"); - return; + Future showAnchoredHelper(String anchorKeyId, AnchoredHelper helper, {HelperAlignment? align,}) async { + try { + //final key = getAnchorKey(anchorKeyId) as ValueKey; + final anchor = await findAnchor(anchorKeyId, align: align); + if (anchor == null) { + debugPrint("anchor cannot be found. show anchored failed"); + return; + } + _overlayHelper.showHelper( + context, + (context) => AnchorHelperWrapper( + anchor: anchor, + child: helper, + ), + ); + } catch (e) { + debugPrint("show anchored helper failed: $e"); } - _overlayHelper.showHelper( - context, - (context) => AnchorHelperWrapper( - anchor: anchor, - child: helper, - ), - ); } /// Returns an [Anchor] wich contains position, size and rect of the widget /// containing the key. /// /// this requires an [anchorKeyId] to search within our keys - Future findAnchor(String anchorKeyId) async { + Future findAnchor(String anchorKeyId, {HelperAlignment? align,}) async { final element = _elementFinder! // .searchChildElementByKey(getAnchorKey(anchorKeyId)); if (element == null || element.bounds == null) { @@ -148,11 +156,17 @@ class HelperOrchestratorState extends State { } final anchorSize = element.bounds!.size; final currentPos = element.offset!; - final writeArea = _elementFinder!.getLargestAvailableSpace(element); + if(align != null) { + return Anchor( + size: anchorSize, + offset: currentPos, + rect: _elementFinder!.getSpaceFromAlignment(align, element), + ); + } return Anchor( size: anchorSize, offset: currentPos, - rect: writeArea, + rect: _elementFinder!.getLargestAvailableSpace(element), ); } diff --git a/lib/services/element_finder.dart b/lib/services/element_finder.dart index 87cfd1d..4fbb5d2 100644 --- a/lib/services/element_finder.dart +++ b/lib/services/element_finder.dart @@ -1,5 +1,6 @@ import 'package:collection/collection.dart' show IterableExtension; import 'package:flutter/material.dart'; +import 'package:pal_widgets/helper_orchestrator.dart'; const minWritableSpace = 100; @@ -58,6 +59,29 @@ class ElementFinder { return result; } + Rect getSpaceFromAlignment(HelperAlignment align, ElementModel elementModel) { + var parentObject = context.findRenderObject()!; + var element = elementModel.element!; + var translation = + element.renderObject!.getTransformTo(parentObject).getTranslation(); + var objectX = translation.x; + var objectEndX = objectX + element.size!.width; + var objectY = translation.y; + var objectEndY = objectY + element.size!.height; + var layerRect = parentObject.paintBounds; + switch (align) { + case HelperAlignment.top: + return Rect.fromLTWH(0, 0, layerRect.width, objectY); + case HelperAlignment.bottom: + return Rect.fromLTWH(0, objectEndY, layerRect.width, layerRect.height); + case HelperAlignment.left: + return Rect.fromLTWH(0, 0, objectX, layerRect.height); + case HelperAlignment.right: + return Rect.fromLTWH(objectEndX, 0, layerRect.width - objectEndX, + layerRect.height); + } + } + /// This functions search for the maximum rect available space /// We use it for example to find the most available space to write a text in our anchored helper Rect getLargestAvailableSpace(ElementModel elementModel) { diff --git a/lib/services/overlay_helper.dart b/lib/services/overlay_helper.dart index 828060e..1c8bf93 100644 --- a/lib/services/overlay_helper.dart +++ b/lib/services/overlay_helper.dart @@ -11,7 +11,7 @@ class OverlayHelper { opaque: false, builder: widgetBuilder, ); - final overlay = Overlay.of(context); + final overlay = Overlay.maybeOf(context); if (overlay != null) { overlay.insert(overlayEntry!); } diff --git a/test/anchored/widgets/anchored_circle_no_buttons.dart b/test/anchored/widgets/anchored_circle_no_buttons.dart index d92a311..79c3eba 100644 --- a/test/anchored/widgets/anchored_circle_no_buttons.dart +++ b/test/anchored/widgets/anchored_circle_no_buttons.dart @@ -54,7 +54,6 @@ class _MyHomePage2State extends State { ); AnchoredHelper get helper => AnchoredHelper( - anchorKeyId: 'text1', title: const Text( 'Title lorem pitume', textAlign: TextAlign.center, diff --git a/test/anchored/widgets/anchored_circle_page_.dart b/test/anchored/widgets/anchored_circle_page_.dart index 29a96c5..d1f0c1b 100644 --- a/test/anchored/widgets/anchored_circle_page_.dart +++ b/test/anchored/widgets/anchored_circle_page_.dart @@ -54,7 +54,6 @@ class _MyHomePageState extends State { ); AnchoredHelper get helper => AnchoredHelper( - anchorKeyId: 'text1', title: const Text( 'Title lorem pitume', textAlign: TextAlign.center, diff --git a/test/anchored/widgets/anchored_rect_no_buttons.dart b/test/anchored/widgets/anchored_rect_no_buttons.dart index f41f26a..094759b 100644 --- a/test/anchored/widgets/anchored_rect_no_buttons.dart +++ b/test/anchored/widgets/anchored_rect_no_buttons.dart @@ -54,7 +54,6 @@ class _MyHomePage2State extends State { ); AnchoredHelper get helper => AnchoredHelper( - anchorKeyId: 'text1', title: const Text( 'Title lorem pitume', textAlign: TextAlign.center, diff --git a/test/anchored/widgets/anchored_rect_page.dart b/test/anchored/widgets/anchored_rect_page.dart index 3db4ef3..8fa6991 100644 --- a/test/anchored/widgets/anchored_rect_page.dart +++ b/test/anchored/widgets/anchored_rect_page.dart @@ -54,7 +54,6 @@ class _MyHomePageState extends State { ); AnchoredHelper get helper => AnchoredHelper( - anchorKeyId: 'text1', title: const Text( 'Title lorem pitume', textAlign: TextAlign.center,