Flutter current screen out, new in Animation

Viewed 24

I need to animate current screen A out, then screen B in. PageRouteBuilder, transition:

  @override
  Widget buildTransitions(
    BuildContext context,
    Animation<double> animation,
    Animation<double> secondaryAnimation,
    Widget child,
  ) {
    var animCurrentWidget = Tween<Offset>(begin: const Offset(0, 0), end: const Offset(0, 1))
        .chain(CurveTween(curve: const Interval(0, 0.5, curve: Curves.linear)))
        .animate(animation);
    var animNewWidget = Tween<Offset>(begin: const Offset(0, 1), end: const Offset(0, 0))
        .chain(CurveTween(curve: const Interval(0.5, 1, curve: Curves.linear)))
        .animate(animation);
    return Stack(
      children: <Widget>[
        SlideTransition(position: animCurrentWidget, child: parent, transformHitTests: false),
        SlideTransition(position: animNewWidget, child: child),
      ],
    );
  }

Pretty simple, current screen leaves with up->down, new one goes in with sliding down->up after first one left.

But the tricky part is that you need to have access to current Widget on screen to do that, which removes use of

onGenerateRoute: MyCustomRouting.generateRoute

And forces me to work with routes in local scope of every possible screen app can have.

  1. What are the best practices for tasks like that?
  2. Is there a way to get current widget inside MyCustomRouting.generateRoute without going full static?
  3. Is there a way to get current 'on top' widget inside other widget in the tree?
0 Answers
Related