Skip to content

Commit

Permalink
chore: Remove unused anchorKeyId property in AnchoredHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
g-apparence committed Jun 5, 2024
1 parent d9b381b commit 758c1b1
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 41 deletions.
3 changes: 1 addition & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ class _MyHomePageState extends State<MyHomePage> {
}

AnchoredHelper get helper => AnchoredHelper(
anchorKeyId: 'btn',
title: const Text(
'Push to increment',
textAlign: TextAlign.center,
Expand Down Expand Up @@ -137,6 +136,6 @@ class _MyHomePageState extends State<MyHomePage> {
// onRightTap: () => HelperOrchestrator.of(context).hideHelper(),
// onLeftBtnTap: () => HelperOrchestrator.of(context).hideHelper(),
onTapAnchor: () => HelperOrchestrator.of(context).hideHelper(),
widgetFactory: AnchoredRectHoleHelper.anchorFactory,
widgetFactory: AnchoredCircleHoleHelper.anchorFactory,
);
}
28 changes: 14 additions & 14 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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"
9 changes: 5 additions & 4 deletions lib/anchored/anchored_helper_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -74,7 +72,6 @@ class AnchoredHelper extends StatefulWidget {
final AnchorWidgetFactory widgetFactory;

const AnchoredHelper({
required this.anchorKeyId,
this.onLeftBtnTap,
this.onRightTap,
this.rightBtnText,
Expand Down Expand Up @@ -157,6 +154,7 @@ class _AnchoredHelperState extends State<AnchoredHelper>

@override
Widget build(BuildContext context) {
// print("anchor: ${anchor.offset} ${anchor.size}: ${anchor.rect}");
return GestureDetector(
onTap: () async {
if(widget.onTapBackground != null) {
Expand Down Expand Up @@ -191,7 +189,10 @@ class _AnchoredHelperState extends State<AnchoredHelper>
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,
Expand Down
46 changes: 30 additions & 16 deletions lib/helper_orchestrator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -99,7 +102,7 @@ class HelperOrchestratorState extends State<HelperOrchestrator> {
/// 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;
Expand All @@ -120,26 +123,31 @@ class HelperOrchestratorState extends State<HelperOrchestrator> {
/// ```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<String>;
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<Anchor?> findAnchor(String anchorKeyId) async {
Future<Anchor?> findAnchor(String anchorKeyId, {HelperAlignment? align,}) async {
final element = _elementFinder! //
.searchChildElementByKey(getAnchorKey(anchorKeyId));
if (element == null || element.bounds == null) {
Expand All @@ -148,11 +156,17 @@ class HelperOrchestratorState extends State<HelperOrchestrator> {
}
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),
);
}

Expand Down
24 changes: 24 additions & 0 deletions lib/services/element_finder.dart
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion lib/services/overlay_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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!);
}
Expand Down
1 change: 0 additions & 1 deletion test/anchored/widgets/anchored_circle_no_buttons.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class _MyHomePage2State extends State<MyHomePage2> {
);

AnchoredHelper get helper => AnchoredHelper(
anchorKeyId: 'text1',
title: const Text(
'Title lorem pitume',
textAlign: TextAlign.center,
Expand Down
1 change: 0 additions & 1 deletion test/anchored/widgets/anchored_circle_page_.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class _MyHomePageState extends State<MyHomePage> {
);

AnchoredHelper get helper => AnchoredHelper(
anchorKeyId: 'text1',
title: const Text(
'Title lorem pitume',
textAlign: TextAlign.center,
Expand Down
1 change: 0 additions & 1 deletion test/anchored/widgets/anchored_rect_no_buttons.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class _MyHomePage2State extends State<MyHomePage2> {
);

AnchoredHelper get helper => AnchoredHelper(
anchorKeyId: 'text1',
title: const Text(
'Title lorem pitume',
textAlign: TextAlign.center,
Expand Down
1 change: 0 additions & 1 deletion test/anchored/widgets/anchored_rect_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class _MyHomePageState extends State<MyHomePage> {
);

AnchoredHelper get helper => AnchoredHelper(
anchorKeyId: 'text1',
title: const Text(
'Title lorem pitume',
textAlign: TextAlign.center,
Expand Down

0 comments on commit 758c1b1

Please sign in to comment.