A few examples of implementing TweenAnimationBuilder in Flutter.
Table of Contents
MakingaA Pulsing Star
A pulsing circle star in the limitless purple universe.Advertisements
App Preview
The complete code
// main.dart
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
// Hide the debug banner
debugShowCheckedModeBanner: false,
title: 'Kindacode.com',
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
double _targetSize = 300;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
alignment: Alignment.center,
color: Colors.purple[900],
child: TweenAnimationBuilder(
tween: Tween<double>(
begin: 50,
end: _targetSize,
),
duration: Duration(seconds: 2),
onEnd: () {
setState(() {
if (_targetSize == 50) {
_targetSize = 300;
} else {
_targetSize = 50;
}
});
},
curve: Curves.linear,
builder: (BuildContext _, double size, Widget? __) {
return Container(
width: size,
height: size,
decoration: BoxDecoration(
gradient: RadialGradient(
center: Alignment.center,
colors: [Colors.yellow, Colors.red]),
shape: BoxShape.circle),
);
},
),
),
);
}
}
Creating a Elastic Box
This example app contains a floating button and 2 boxes. When the user presses the button, the width of the orange box switches between 100 and 300.Advertisements
App Preview
The full code
// main.dart
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
// Hide the debug banner
debugShowCheckedModeBanner: false,
title: 'Kindacode.com',
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
double _targetValue = 100;
void _changeSize() {
setState(() {
if (_targetValue == 100) {
_targetValue = 300;
} else {
_targetValue = 100;
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Kindacode.com'),
),
body: Center(
child: TweenAnimationBuilder(
tween: Tween<double>(
begin: 100,
end: _targetValue,
),
duration: Duration(seconds: 1),
curve: Curves.linear,
builder: (BuildContext _, double value, Widget? __) {
return Container(
width: value,
height: 300,
color: Colors.amber,
alignment: Alignment.center,
);
},
child: Container(
width: 50,
height: 50,
color: Colors.purple,
),
),
),
floatingActionButton: ElevatedButton.icon(
onPressed: _changeSize,
icon: Icon(Icons.refresh),
label: Text('Swich Size')),
);
}
}
Wrapping Up
WeÂ’ve gone through a couple of examples of making cool animations by using TweenAnimationBuilder. If youÂ’d like to explore more new and awesome features of Flutter, take a look at the following articles:
- Flutter Transform examples – Making fancy effects
- Flutter: ColorTween Example
- Flutter AnimatedList – Tutorial and Examples
- Working with Cupertino Date Picker in Flutter
- Using GetX (Get) for Navigation and Routing in Flutter
You can also take a tour around our Flutter topic page and Dart topic page to see the latest tutorials and examples.