Flutter Labsession - 8
Flutter Labsession - 8
@override
void initState() { super.initState();
_controller = AnimationController( duration: Duration(seconds: 1), vsync:
this,
);
_animation = Tween<double>(begin: 0, end: 300).animate(_controller)
..addListener(() {
setState(() {}); // Trigger rebuild when animation value changes
});
}
@override
Widget build(BuildContext context) { return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
width: _animation.value, height: _animation.value, color: Colors.blue,
child: FlutterLogo(size: 100),
),
SizedBox(height: 20), ElevatedButton( onPressed: () {
if (_controller.status == AnimationStatus.completed) {
_controller.reverse();
} else {
_controller.forward();
}
},
child: Text(
_controller.status == AnimationStatus.completed
? 'Reverse Animation'
: 'Start Animation',
),
),
],
),
);
}
@override
void dispose() {
_controller.dispose(); super.dispose();
}
}
Output:
We define an Animation object with Tween to define the range of values for the animation.
I
nside the initState() method, we initialize the animation controller and define the
animation. We use addListener() to trigger a rebuild when the animation value
changes.
In the build method, we use the animated value _animation.value to control the size of the
Container, which contains the FlutterLogo.
The ElevatedButton toggles the animation between forward and reverse based on the status of
the animation controller.
You can customize the animation further by adjusting the duration, adding curves, or chaining
multiple animations together.
Fade Animation:
import 'package:flutter/material.dart';
@override
void initState() { super.initState();
_controller = AnimationController( vsync: this,
duration: Duration(seconds: 2),
);
_animation = Tween<double>( begin: 0.0,
end: 1.0,
).animate(_controller);
_controller.forward();
}
@override
Widget build(BuildContext context) { return Center(
child: FadeTransition( opacity: _animation, child: Container( width: 200,
height: 200,
color: Colors.blue,
),
),
);
}
@override
void dispose() {
_controller.dispose(); super.dispose();
}
}
Output:
Slide Animation:
import 'package:flutter/material.dart';
@override
Widget build(BuildContext context) { return Center(
child: SlideTransition( position: _animation, child: Container( width:
200,
height: 200,
color: Colors.blue,
),
),
);
}
@override
void dispose() {
_controller.dispose(); super.dispose();
}
}
Output:
Scale Animation:
import 'package:flutter/material.dart';
}
}
@override
void initState() { super.initState();
_controller = AnimationController( vsync: this,
duration: Duration(seconds: 2),
);
_animation = Tween<double>( begin: 0.0,
end: 1.0,
).animate(_controller);
_controller.forward();
}
@override
Widget build(BuildContext context) { return Center(
child: ScaleTransition( scale: _animation, child: Container( width: 200,
height: 200,
color: Colors.blue,
),
),
);
}
@override
void dispose() {
_controller.dispose(); super.dispose();
}
}
Output: