Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could not find a Portal above this PortalTarget when using OpenContainer animation #112

Closed
jamesncl opened this issue Mar 19, 2024 · 4 comments
Labels
bug Something isn't working wontfix This will not be worked on

Comments

@jamesncl
Copy link

jamesncl commented Mar 19, 2024

I'm using flutter_portal 1.1.4 with the animations package from flutter.dev.

If I use a PortalFollower in combination with an OpenContainer (aka 'container transform', which provides a nice transition animation when pushing a route), when the container opens, I get:

The following PortalNotFoundError was thrown building _PortalTargetVisibilityBuilder(visible: false, closeDuration: null, builder: Closure: (BuildContext, bool) => Widget, dirty, state: _PortalTargetVisibilityBuilderState#7635b):
Error: Could not find a Portal above this PortalTarget(debugName: null, portalCandidateLabels=[[String <'test'>]]).

The relevant error-causing widget was:
PortalTarget PortalTarget:file:///Users/james/GithubRepos/portal_bug/lib/main.dart:48:18
When the exception was thrown, this was the stack:
0 _PortalTargetState.build. (package:flutter_portal/src/portal_target.dart:265:11)
1 _PortalTargetVisibilityBuilderState.build (package:flutter_portal/src/portal_target.dart:449:26)

I think that during the transition animation, PortalTarget cannot find a PortalLinkScope

        // From portal_target.dart line 263:
        final scope = PortalLinkScope.of(context, widget.portalCandidateLabels);
        if (scope == null) {
          throw PortalNotFoundError._(widget);
        }

I think because the animation detatches the whole widget from the original context to do the animation to the new route. This error occurs even if I set visibility to false on PortalTarget before triggering the animation.

I guess this is more a problem with the animations package but it would be nice if it could not throw an error if PortalLinkScope is not found but visibility is false, for example:

        final scope = PortalLinkScope.of(context, widget.portalCandidateLabels);
        if (scope == null) {
          // Add this check:
          if(widget.visible) {
            throw PortalNotFoundError._(widget);
          }
          else {
            return Container();
          }
        }

Here's a minimal reproducible example, just tap the button to trigger the error:

import 'package:animations/animations.dart';
import 'package:flutter/material.dart';
import 'package:flutter_portal/flutter_portal.dart';

const portalLabel = PortalLabel('test');

void main() {
  runApp(const MaterialApp(
    home: Portal(
      labels: [portalLabel],
      child: Example(),
    ),
  ));
}

class Example extends StatefulWidget {

  const Example();

  @override
  State<Example> createState() => _ExampleState();
}

class _ExampleState extends State<Example> {

  bool _visible = true;

  @override
  Widget build(BuildContext context) {
    return OpenContainer(
      closedBuilder: (BuildContext context, VoidCallback openContainer) {
        return Center(
          child: PortalTarget(
            portalCandidateLabels: const [portalLabel],
            visible: _visible,
            portalFollower: const Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                Icon(Icons.arrow_circle_up),
                Text('Tap here'),
              ],
            ),
            anchor: const Aligned(
              target: Alignment.bottomRight,
              follower: Alignment.topCenter,
            ),
            child: ElevatedButton(
              onPressed: () {
                setState(() {
                  _visible = !_visible;
                });
                openContainer();
              },
              child: const Text('Open container'),
            ),
          ),
        );
      },
      openBuilder: (BuildContext context, VoidCallback openContainer) {
        return const Scaffold(body: Center(child: BackButton()));
      },
    );
  }
}
@jamesncl jamesncl added the bug Something isn't working label Mar 19, 2024
@fzyzcjy
Copy link
Owner

fzyzcjy commented Mar 19, 2024

not throw an error if PortalLinkScope is not found but visibility is false

IIRC flutter_portal has a feature that, if visible becomes false, it may show some animation. (e.g. see

class _PortalTargetVisibilityBuilder extends StatefulWidget {
).

However, for your scenario, what about something like: visible ? PortalTarget(..., child: YourButton()) : YourButton(). In other words, when not visible, just directly remove the PortalTarget from the tree.

@jamesncl
Copy link
Author

Thanks, that seems to work. I'm still not entirely sure why it's happening as I'm not using a closeDuration on PortalTarget (so I don't think it animates?) and OpenContainer is quite complex. But I think this workaround may be the best solution

@fzyzcjy
Copy link
Owner

fzyzcjy commented Mar 20, 2024

You are welcome!

Copy link

stale bot commented May 19, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix This will not be worked on label May 19, 2024
@stale stale bot closed this as completed May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants