简体   繁体   English

如何在 Overlay/PopupRoute 中制作 Hero Widget 动画

[英]How do I make a Hero Widget Animate in an Overlay/PopupRoute

I have a settings page with options that show a popup when they are clicked because it can't fit in a small container.我有一个设置页面,其中包含在单击时显示弹出窗口的选项,因为它不适合小容器。 I used an Overlay to display the popup ( Overlay.of(context).insert(OverlayEntry()); ).我使用 Overlay 来显示弹出窗口( Overlay.of(context).insert(OverlayEntry()); )。 The problem was that it would just pop in place with no animation.问题是它会在没有 animation 的情况下弹出。

I then tried an alternative approach and followed this article https://medium.com/flutter-community/flutter-route-animations-6ea071be5168 .然后我尝试了另一种方法,并按照这篇文章https://medium.com/flutter-community/flutter-route-animations-6ea071be5168 进行操作 The problem is that I don't want to use a transition like Fade, Scale, etc. I have a both wrapped in a Hero widget so I want the setting to expand when clicked and shrink when it is being popped.问题是我不想使用淡入淡出、缩放等过渡。我将两者都包裹在 Hero 小部件中,所以我希望设置在单击时展开,在弹出时缩小。

On the welcome page of the app I have something similar: Welcome Page Screen Recording I achieved this using this code:在应用程序的欢迎页面上,我有类似的内容:欢迎页面屏幕录制我使用以下代码实现了这一点:

onPressed: () => Navigator.of(context).push(
                                PageRouteBuilder(
                                  transitionDuration:
                                      Duration(milliseconds: 600),
                                  pageBuilder: (BuildContext context,
                                      Animation<double> animation,
                                      Animation<double> secondaryAnimation) {
                                    return LogInMock();
                                  },
                                  transitionsBuilder: (BuildContext context,
                                      Animation<double> animation,
                                      Animation<double> secondaryAnimation,
                                      Widget child) {
                                    return Align(
                                      child: FadeTransition(
                                        opacity: animation,
                                        child: child,
                                      ),
                                    );
                                  },
                                ),
                              ),

Both containers have the same hero tag and a very similar structure.两个容器具有相同的英雄标签和非常相似的结构。

I tried doing this using a PopupRoute (from the medium article above) but it would just fade in without the Hero animating: Settings Page Screen Recording .我尝试使用 PopupRoute(来自上面的媒体文章)来执行此操作,但它只会在没有 Hero 动画的情况下淡入:设置页面屏幕录制 I am using the same widget so it has the same structure and Hero tag.我使用的是相同的小部件,因此它具有相同的结构和 Hero 标签。 To make sure it isn't a problem with my widget I made a container wrapped with a Hero and I get the same result.为了确保我的小部件没有问题,我制作了一个用英雄包裹的容器,我得到了相同的结果。

Here is the PopupRoute navigation code:这是 PopupRoute 导航代码:

Navigator.push(
              context,
              CustomPopupRoute(
                pageBuilder: (BuildContext context, Animation<double> animation,
                    Animation<double> secondaryAnimation) {
                  return Test();
                },
                transitionsBuilder: (BuildContext context,
                    Animation<double> animation,
                    Animation<double> secondaryAnimation,
                    Widget child) {
                  return FadeTransition(
                    opacity: animation,
                    child: child,
                  );
                },
              ),
            );

This is a current limitation of Flutter so it is not possible, there is an existing issue on the Flutter GitHub repository.这是 Flutter 的当前限制,因此不可能,Flutter GitHub 存储库中存在一个问题。

https://github.com/flutter/flutter/issues/48467 https://github.com/flutter/flutter/issues/48467

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM