Giter VIP home page Giter VIP logo

Comments (6)

StanleyCocos avatar StanleyCocos commented on July 24, 2024
RPReplay_Final1717406215.MP4

from flutter.

davidhicks980 avatar davidhicks980 commented on July 24, 2024

I think things are running correctly, but the speed of the page transition makes it appear as if the square has moved prior to animating in.

To wait for the page animation to finish before starting your animation, you can use ModalRoute.of(context).animation:

import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';

class BugApp extends StatelessWidget {
  const BugApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(onGenerateRoute: (settings) {
      return MaterialPageRoute(builder: (context) => const Bug());
    });
  }
}

class Bug extends StatefulWidget {
  const Bug({super.key});

  @override
  State<StatefulWidget> createState() => _BugState();
}

class _BugState extends State<Bug> with SingleTickerProviderStateMixin {
  static final tween = Tween(begin: Offset.zero, end: const Offset(1, 0));
  late final AnimationController _controller;
  late final Animation<Offset> _animation;
  Animation<double>? _routeAnimation;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(seconds: 2),
      vsync: this,
    );
    _animation = tween.animate(_controller);
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    if (_routeAnimation == null) {
      _routeAnimation =
          ModalRoute.of(context)?.animation ?? kAlwaysCompleteAnimation;
      _routeAnimation!.addStatusListener(_handleAnimationStatusChange);
    }
  }

  @override
  void dispose() {
    _routeAnimation?.removeStatusListener(_handleAnimationStatusChange);
    _routeAnimation = null;
    _controller.dispose();
    super.dispose();
  }

  void _handleAnimationStatusChange(AnimationStatus status) {
    if (status == AnimationStatus.completed) {
      _controller.forward();
    }
  }

  @override
  Widget build(BuildContext context) {
    timeDilation = 10; // This allows you to view the animation in slow motion.
    final size = MediaQuery.sizeOf(context);
    return Scaffold(
      appBar: AppBar(
        title: const Text("animation"),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SlideTransition(
            position: _animation,
            child: Container(
              width: size.width / 2,
              height: size.width / 2,
              color: Colors.grey,
            ),
          ),
          TextButton(
            onPressed: () {
              Navigator.of(context).push(
                MaterialPageRoute(builder: (context) => const Bug()),
              );
            },
            child: const Text("Push new page"),
          )
        ],
      ),
    );
  }
}

from flutter.

darshankawar avatar darshankawar commented on July 24, 2024

@StanleyCocos Check above and see if it helps in your case or not.

from flutter.

StanleyCocos avatar StanleyCocos commented on July 24, 2024

Thank you very much, this solution is perfect

from flutter.

darshankawar avatar darshankawar commented on July 24, 2024

Closing as resolved.

from flutter.

github-actions avatar github-actions commented on July 24, 2024

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

from flutter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.