Skip to content

Commit

Permalink
Add modal mode
Browse files Browse the repository at this point in the history
  • Loading branch information
g-apparence committed Oct 30, 2024
1 parent 88aa72b commit a9aba9b
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 29 deletions.
2 changes: 1 addition & 1 deletion lib/anchored/painters/anchor_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:flutter/material.dart';
abstract class AnchorWidgetFactory {
const AnchorWidgetFactory();

@factory
// @factory
Widget create({
final Offset? currentPos,
final Size? anchorSize,
Expand Down
12 changes: 8 additions & 4 deletions lib/helper_orchestrator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,12 @@ class HelperOrchestratorState extends State<HelperOrchestrator> {
String anchorKeyId,
AnchoredHelper helper, {
HelperAlignment? align,
bool isInModal = false,
}) async {
try {
//final key = getAnchorKey(anchorKeyId) as ValueKey<String>;
final anchor = await findAnchor(anchorKeyId, align: align);
final anchor =
await findAnchor(anchorKeyId, align: align, isInModal: isInModal);
if (anchor == null) {
debugPrint("anchor cannot be found. show anchored failed");
return;
Expand All @@ -142,8 +144,8 @@ class HelperOrchestratorState extends State<HelperOrchestrator> {
child: helper,
),
);
} catch (e) {
debugPrint("show anchored helper failed: $e");
} catch (e, s) {
debugPrint("show anchored helper failed: $e $s");
}
}

Expand All @@ -154,9 +156,11 @@ class HelperOrchestratorState extends State<HelperOrchestrator> {
Future<Anchor?> findAnchor(
String anchorKeyId, {
HelperAlignment? align,
bool isInModal = false,
}) async {
final element = _elementFinder! //
.searchChildElementByKey(getAnchorKey(anchorKeyId));
.searchChildElementByKey(getAnchorKey(anchorKeyId),
isInModal: isInModal);
if (element == null || element.bounds == null) {
debugPrint("anchor not found");
return null;
Expand Down
86 changes: 64 additions & 22 deletions lib/services/element_finder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class ElementFinder {
return navigatorKey!.currentContext!;
}

Size get screenSize => MediaQuery.of(context).size;

// this method scan all child recursively to get all widget bounds we could select for an helper
Map<String, ElementModel> scan({Key? omitChildsOf, bool debugMode = false}) {
Map<String, ElementModel> results = <String, ElementModel>{};
Expand Down Expand Up @@ -49,10 +51,17 @@ class ElementFinder {
return result;
}

ElementModel? searchChildElementByKey(Key key) {
ElementModel? searchChildElementByKey(
Key key, {
required bool isInModal,
}) {
ElementModel? result;
context.visitChildElements((element) => result =
_searchChildElementByKey(context.findRenderObject(), element, key));
context.visitChildElements((element) => result = _searchChildElementByKey(
context.findRenderObject(),
element,
key,
isInModal: isInModal,
));
if (result == null) {
return ElementModel.empty();
}
Expand All @@ -62,11 +71,10 @@ class ElementFinder {
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 translation = element.renderObject!.getTransformTo(parentObject).getTranslation();
var objectX = elementModel.offset!.dx;
var objectEndX = objectX + element.size!.width;
var objectY = translation.y;
var objectY = elementModel.offset!.dy;
var objectEndY = objectY + element.size!.height;
var layerRect = parentObject.paintBounds;
switch (align) {
Expand Down Expand Up @@ -133,7 +141,7 @@ class ElementFinder {
element.widget.key.toString().contains(key!)) {
try {
// if render element has bounds lets take it
var res = _createElementModel(parentObject, element);
var res = _createElementModel(parentObject, element, false);
return res;
} catch (_) {}
}
Expand All @@ -146,18 +154,27 @@ class ElementFinder {
}

ElementModel? _searchChildElementByKey(
RenderObject? parentObject, Element element, Key key,
{int n = 0}) {
RenderObject? parentObject,
Element element,
Key key, {
int n = 0,
required bool isInModal,
}) {
if (element.widget.key != null && element.widget.key == key) {
try {
// if render element has bounds lets take it
var res = _createElementModel(parentObject, element);
return res;
return _createElementModel(parentObject, element, isInModal);
} catch (_) {}
}
ElementModel? result;
element.visitChildElements((visitor) {
var res = _searchChildElementByKey(parentObject, visitor, key, n: n + 1);
var res = _searchChildElementByKey(
parentObject,
visitor,
key,
n: n + 1,
isInModal: isInModal,
);
if (res != null) result = res;
});
return result;
Expand Down Expand Up @@ -189,7 +206,7 @@ class ElementFinder {
debugPrint(" added ${element.widget.key.toString()} : $n");
}
try {
var model = _createElementModel(parentObject, element);
var model = _createElementModel(parentObject, element, false);
if (results.values.firstWhereOrNull((element) =>
element.bounds == model.bounds &&
element.offset == model.offset) ==
Expand Down Expand Up @@ -217,15 +234,40 @@ class ElementFinder {
}

ElementModel _createElementModel(
RenderObject? parentObject, Element element) {
var renderObject = element.findRenderObject()!;
var bounds = renderObject.paintBounds;
var translation =
renderObject.getTransformTo(parentObject).getTranslation();
var offset = Offset(translation.x, translation.y);
return ElementModel(element.widget.key.toString(), bounds, offset,
RenderObject? parentObject,
Element element,
bool isInModal,
) {
try {
final mScreenSize = screenSize;
var renderObject = element.findRenderObject()!;

var bounds = renderObject.paintBounds;
var translation = renderObject //
.getTransformTo(parentObject)
.getTranslation();
// final parentBounds = parentObject!.paintBounds;
if (isInModal) {
final parentRenderObject = renderObject.parent!;
final parentBounds = parentRenderObject.paintBounds;
if (parentBounds.height < mScreenSize.height) {
translation.y +=
mScreenSize.height - parentBounds.height - bounds.height / 2;
}
}
var offset = Offset(translation.x, translation.y);

return ElementModel(
element.widget.key.toString(),
bounds,
offset,
element.widget.runtimeType,
element: element);
element: element,
);
} catch (e, s) {
debugPrint("Error while creating element model: $e $s");
rethrow;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: pal_widgets
description: A set of amazing onboarding widgets for your flutter applications.
version: 0.3.0
version: 0.4.0
repository: https://github.com/Apparence-io/pal-widgets

environment:
Expand Down
3 changes: 2 additions & 1 deletion test/services/element_finder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ void main() {
)));
await tester.pumpWidget(app);
ElementFinder finder = ElementFinder(buildContext: _context);
var result = finder.searchChildElementByKey(containerKey)!;
var result =
finder.searchChildElementByKey(containerKey, isInModal: false)!;
expect(result, isNotNull);
expect(result.bounds!.size, equals(const Size(150, 50)));
});
Expand Down

0 comments on commit a9aba9b

Please sign in to comment.