Reputation: 1759
I was trying to learn Flutter SizeTransition. I used SizeTransition and provided sizeFactor as animation and provided tween from 0 to 1. I made a function execute in build that gets exectued after some seconds, What I expected was that the size of logo will increase and decrease when animation is forward and reverse respectively. But what I noticed that logo first moves down and then goes back up.(like a Slide Transition)
widget to test for SizeTransition
import 'dart:async';
import 'package:flutter/material.dart';
class LogoApp extends StatefulWidget {
_LogoAppState createState() => _LogoAppState();
}
class _LogoAppState extends State<LogoApp> with TickerProviderStateMixin {
AnimationController _animationController;
Animation<double> _animation;
@override
void initState() {
super.initState();
_animationController =
AnimationController(vsync: this, duration: Duration(seconds: 4));
_animation = _animationController.drive(Tween(begin: 0, end: 1));
}
int ctr = 0;
@override
Widget build(BuildContext context) {
ctr += 1;
print("build$ctr");
execute(); //function that executes forward()/reverse() methods of animationController
return SizeTransition(
sizeFactor: _animation,
child: Center(
child: FlutterLogo(),
),
);
}
void execute() async {
Future.delayed(const Duration(seconds: 2), () {
_animationController.forward();
});
Future.delayed(const Duration(seconds: 4), () {
_animationController.reverse();
});
}
}
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: LogoApp(),
);
}
}
I have tried a lot but no success. What else can I do?
Upvotes: 1
Views: 1675
Reputation: 509
I think what you actually meant to implement was a ScaleTransition()
instead of a SizeTransition()
.
It's a pretty straightforward fix:
int ctr = 0;
@override
Widget build(BuildContext context) {
ctr += 1;
print("build$ctr");
execute(); //function that executes forward()/reverse() methods of animationController
return Center(
child: ScaleTransition(
scale: _animation,
child: FlutterLogo(),
),
);
}
You also need to move the Center()
widget one level up (as shown in the code) to ensure that the entire animation is anchored to the center of the display - as you originally intended it to be.
Upvotes: 2