4

I want to use SizeTransition widget for just an image not for page. It will be my loading page and while loading the application app symbol will be displayed with a size transition.

https://docs.flutter.io/flutter/widgets/SizeTransition-class.html

I want my logo with this effect in the link. However there is not enough source for me to learn about that widget. Can you please give me an example? I tried something like this but it;s not working.

class _AnimTestState extends State<AnimTest>
    with SingleTickerProviderStateMixin {
  AnimationController controller;
  @override
  void initState() {
    super.initState();
    controller = AnimationController(vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SizeTransition(
        child: Image.asset('assets/images/appLogo.png'),
        sizeFactor: CurvedAnimation(
          curve: Curves.fastOutSlowIn,
          parent: controller,
        ),
      ),
    );
  }
}

3 Answers 3

4
class _AnimTestState extends State<HomePage> with SingleTickerProviderStateMixin {
  AnimationController controller;

  @override
  void initState() {
    super.initState();
    controller = AnimationController(
      vsync: this,
      duration: Duration(milliseconds: 400),
    )..repeat(reverse: true); // automatically animation will be started
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SizeTransition(
        child: Image.asset('assets/images/appLogo.png'),
        sizeFactor: CurvedAnimation(
          curve: Curves.fastOutSlowIn,
          parent: controller,
        ),
      ),
    );
  }
}

You can also use _controller.forward() or _controller.reverse() in a button press if you don't want it to run automatically like I did.

3

First of all give the controller a duration, either in the constructor like this:

controller = AnimationController(vsync: this,duration: Duration(seconds: 1));

or anywhere before calling controller.forward() like this:

controller.duration = Duration(seconds: 1);

And last and most important thing, you need to start the animation by calling

controller.forward();
1
  • With controller.forward(from: 0); this solution works.
    – niwufujol
    Commented Apr 5, 2019 at 23:34
0

In addition to Ryosuke's answer (adding controller.forward(), be careful not to do this in build() other than for testing purposes), in order to achieve the effect displayed on the page you linked, you might want to consider

  1. centering your SizeTransition widget by wrapping it in a Center() widget
  2. specifying the transition axis by adding axisAlignment: -0.5 to your SizeTransition widget (see https://docs.flutter.io/flutter/widgets/SizeTransition/axisAlignment.html for details on why).
@override
Widget build(BuildContext context) {
  controller.forward();
  return Scaffold(
    body: Center(
      child: SizeTransition(
        child: Image.asset('assets/images/appLogo.png'),
        axisAlignment: -0.5,
        sizeFactor: CurvedAnimation(
          curve: Curves.fastOutSlowIn,
          parent: controller,
        ),
      ),
    ),
  );
}
3
  • controller.forward() shouldn't be inside build.
    – Ryosuke
    Commented Apr 5, 2019 at 23:11
  • You're absolutely right. What's a better place for it in a demo snippet? Commented Apr 5, 2019 at 23:17
  • Yeah for sake of demo anything works. I was just saying that its not a goof practice.
    – Ryosuke
    Commented Apr 5, 2019 at 23:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.