Skip to content

Commit

Permalink
Merge pull request #96 from jotshq/master
Browse files Browse the repository at this point in the history
Allow EnhancedLayerLink to be used on it's own
  • Loading branch information
fzyzcjy authored Dec 14, 2022
2 parents ff68743 + 6f3be7f commit 9c2c811
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 57 deletions.
4 changes: 3 additions & 1 deletion lib/enhanced_composited_transform.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export 'src/enhanced_composited_transform/anchor.dart'
show EnhancedCompositedTransformAnchor, EnhancedCompositedTransformAligned;
export 'src/enhanced_composited_transform/flutter_src/rendering_layer.dart'
show EnhancedLayerLink;
show EnhancedLayerLink, EnhancedLeaderLayer, EnhancedFollowerLayer;
export 'src/enhanced_composited_transform/flutter_src/rendering_proxy_box.dart'
show EnhancedRenderFollowerLayer, EnhancedRenderLeaderLayer;
export 'src/enhanced_composited_transform/flutter_src/widgets_basic.dart'
show EnhancedCompositedTransformTarget, EnhancedCompositedTransformFollower;
2 changes: 2 additions & 0 deletions lib/flutter_portal.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export 'package:flutter_portal/src/anchor.dart'
show Anchor, Aligned, AxisFlag, Filled;
export 'package:flutter_portal/src/portal.dart' show Portal, PortalLabel;
export 'package:flutter_portal/src/portal_link.dart'
show PortalLinkScope, PortalLink;
export 'package:flutter_portal/src/portal_target.dart'
show PortalTarget, PortalNotFoundError;
53 changes: 53 additions & 0 deletions lib/src/portal_link.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:collection/collection.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import '../flutter_portal.dart';
Expand Down Expand Up @@ -61,4 +62,56 @@ class PortalLinkScope extends InheritedWidget {
}

bool linkEquals(PortalLinkScope other) => portalLink == other.portalLink;

static PortalLinkScope? of(
BuildContext context,
List<PortalLabel<dynamic>> portalCandidateLabels,
) {
for (final portalLabel in portalCandidateLabels) {
final scope =
context.dependOnSpecificInheritedWidgetOfExactType<PortalLinkScope>(
(scope) => scope.portalLabels.contains(portalLabel));
if (scope != null) {
return scope;
}
}
return null;
}
}

extension BuildContextPortalLinkScopeExt on BuildContext {
/// https://stackoverflow.com/questions/71200969
Iterable<InheritedElement> getElementsForInheritedWidgetsOfExactType<
T extends InheritedWidget>() sync* {
final element = getElementForInheritedWidgetOfExactType<T>();
if (element != null) {
yield element;

Element? parent;
element.visitAncestorElements((element) {
parent = element;
return false;
});

if (parent != null) {
yield* parent!.getElementsForInheritedWidgetsOfExactType<T>();
}
}
}

InheritedElement? getSpecificElementForInheritedWidgetsOfExactType<
T extends InheritedWidget>(bool Function(T) test) =>
getElementsForInheritedWidgetsOfExactType<T>()
.where((element) => test(element.widget as T))
.firstOrNull;

/// https://stackoverflow.com/questions/71200969
T? dependOnSpecificInheritedWidgetOfExactType<T extends InheritedWidget>(
bool Function(T) test) {
final element = getSpecificElementForInheritedWidgetsOfExactType<T>(test);
if (element == null) {
return null;
}
return dependOnInheritedElement(element) as T;
}
}
58 changes: 2 additions & 56 deletions lib/src/portal_target.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:async';

import 'package:collection/collection.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
Expand Down Expand Up @@ -241,27 +240,12 @@ class PortalTarget extends StatefulWidget {
BuildContext context,
List<PortalLabel<dynamic>> portalCandidateLabels,
) {
final scope = _dependOnScope(context, portalCandidateLabels);
final scope = PortalLinkScope.of(context, portalCandidateLabels);
if (scope == null) {
return null;
}
return '(debugName: ${scope.debugName}, portalLabel: ${scope.portalLabels})';
}

static PortalLinkScope? _dependOnScope(
BuildContext context,
List<PortalLabel<dynamic>> portalCandidateLabels,
) {
for (final portalLabel in portalCandidateLabels) {
final scope =
context.dependOnSpecificInheritedWidgetOfExactType<PortalLinkScope>(
(scope) => scope.portalLabels.contains(portalLabel));
if (scope != null) {
return scope;
}
}
return null;
}
}

class _PortalTargetState extends State<PortalTarget> {
Expand All @@ -273,8 +257,7 @@ class _PortalTargetState extends State<PortalTarget> {
visible: widget.visible,
closeDuration: widget.closeDuration,
builder: (context, currentVisible) {
final scope =
PortalTarget._dependOnScope(context, widget.portalCandidateLabels);
final scope = PortalLinkScope.of(context, widget.portalCandidateLabels);
if (scope == null) {
throw PortalNotFoundError._(widget);
}
Expand Down Expand Up @@ -540,40 +523,3 @@ class SanityCheckNestedPortalInfo {
String _scopeToString(PortalLinkScope scope) =>
'$scope(hash=${shortHash(scope)})';
}

extension on BuildContext {
/// https://stackoverflow.com/questions/71200969
Iterable<InheritedElement> getElementsForInheritedWidgetsOfExactType<
T extends InheritedWidget>() sync* {
final element = getElementForInheritedWidgetOfExactType<T>();
if (element != null) {
yield element;

Element? parent;
element.visitAncestorElements((element) {
parent = element;
return false;
});

if (parent != null) {
yield* parent!.getElementsForInheritedWidgetsOfExactType<T>();
}
}
}

InheritedElement? getSpecificElementForInheritedWidgetsOfExactType<
T extends InheritedWidget>(bool Function(T) test) =>
getElementsForInheritedWidgetsOfExactType<T>()
.where((element) => test(element.widget as T))
.firstOrNull;

/// https://stackoverflow.com/questions/71200969
T? dependOnSpecificInheritedWidgetOfExactType<T extends InheritedWidget>(
bool Function(T) test) {
final element = getSpecificElementForInheritedWidgetsOfExactType<T>(test);
if (element == null) {
return null;
}
return dependOnInheritedElement(element) as T;
}
}

0 comments on commit 9c2c811

Please sign in to comment.