Flutter - Spin Bottle Animation
Last Updated :
28 Apr, 2025
In Flutter, Spin Bottle Animation is a fun and attractive animation, here we can achieve this by using the Flutter RotationTransition and AnimatedBuilder widget, In this animation, a bottle is rotated when we press a button. In this article, we are going to implement the Spin Bottle Animation using RotationTransition and AnimatedBuilder widget. A sample video is given below to get an idea about what we are going to do in this article.
Step By Step Implementation
Step 1: Create a New Project in Android Studio
To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.
Step 2: Import the Package
First of all import material.dart file.
import 'package:flutter/material.dart';
Step 3: Execute the main Method
Here the execution of our app starts.
Dart
void main() {
runApp(
MyApp(),
);
}
Step 4: Create MyApp Class
In this class we are going to implement the MaterialApp, here we are also set the Theme of our App.
Dart
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.green, // Set the app's primary theme color
),
debugShowCheckedModeBanner: false,
home: SpinTheBottleAnimation(),
);
}
}
Step 5: Create SpinTheBottleAnimation Class
In this class a RotationTransition widget is used to create the bottle animation. It uses the _animation to control the bottle's rotation. The bottle image is loaded from an asset and given a fixed width of 200.If the _animation is completed (i.e., the bottle has stopped spinning), it shows the text "Spin Completed." Otherwise, it displays an empty container while spinning and the dispose method, is used to release resources when the widget is no longer in use. Comments are added for better understanding.
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
// Display the bottle image with a rotation animation.
RotationTransition(
turns: _animation,
child: Image.asset('assets/bottle.png', width: 200),
),
SizedBox(height: 20),
// Button to trigger the bottle spin animation.
ElevatedButton(
onPressed: () {
_controller.reset(); // Reset the animation.
_controller.forward(); // Start the animation.
},
child: Text('Spin the Bottle'),
),
SizedBox(height: 20),
// Display the result when the bottle animation is completed.
AnimatedBuilder(
animation: _animation,
builder: (context, child) {
if (_animation.status == AnimationStatus.completed) {
return Text('Spin Completed');
} else {
return Container(); // Display an empty container while spinning.
}
},
),
],
),
Dart
class SpinTheBottleAnimation extends StatefulWidget {
@override
_SpinTheBottleAnimationState createState() => _SpinTheBottleAnimationState();
}
class _SpinTheBottleAnimationState extends State<SpinTheBottleAnimation>
with SingleTickerProviderStateMixin {
late AnimationController _controller;
late Animation<double> _animation;
@override
void initState() {
super.initState();
// Create an AnimationController for
// managing the bottle spin animation.
_controller = AnimationController(
vsync: this, // Use 'this' as the TickerProvider.
duration: Duration(seconds: 2),
);
// Create a curved animation
// for smoother bottle rotation.
_animation = CurvedAnimation(
parent: _controller,
curve: Curves.easeOut,
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Spin the Bottle Game'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
// Display the bottle image
// with a rotation animation
RotationTransition(
turns: _animation,
child: Image.asset('assets/bottle.png', width: 200),
),
SizedBox(height: 20),
// Button to trigger the bottle spin animation.
ElevatedButton(
onPressed: () {
_controller.reset(); // Reset the animation.
_controller.forward(); // Start the animation.
},
child: Text('Spin the Bottle'),
),
SizedBox(height: 20),
// Display the result when the
// bottle animation is completed.
AnimatedBuilder(
animation: _animation,
builder: (context, child) {
if (_animation.status == AnimationStatus.completed) {
return Text('Spin Completed');
} else {
return Container(); // Display an empty container while spinning.
}
},
),
],
),
),
);
}
@override
void dispose() {
_controller.dispose(); // Dispose of the animation controller.
super.dispose();
}
}
Here is the full Code of main.dart file
Dart
import 'package:flutter/material.dart';
void main() {
runApp(
MyApp(),
);
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.green, // Set the app's primary theme color
),
debugShowCheckedModeBanner: false,
home: SpinTheBottleAnimation(),
);
}
}
class SpinTheBottleAnimation extends StatefulWidget {
@override
_SpinTheBottleAnimationState createState() => _SpinTheBottleAnimationState();
}
class _SpinTheBottleAnimationState extends State<SpinTheBottleAnimation>
with SingleTickerProviderStateMixin {
late AnimationController _controller;
late Animation<double> _animation;
@override
void initState() {
super.initState();
// Create an AnimationController for
// managing the bottle spin animation.
_controller = AnimationController(
vsync: this, // Use 'this' as the TickerProvider.
duration: Duration(seconds: 2),
);
// Create a curved animation
// for smoother bottle rotation.
_animation = CurvedAnimation(
parent: _controller,
curve: Curves.easeOut,
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Spin the Bottle Game'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
// Display the bottle image with a rotation animation.
RotationTransition(
turns: _animation,
child: Image.asset('assets/bottle.png', width: 200),
),
SizedBox(height: 20),
// Button to trigger the bottle spin animation.
ElevatedButton(
onPressed: () {
_controller.reset(); // Reset the animation.
_controller.forward(); // Start the animation.
},
child: Text('Spin the Bottle'),
),
SizedBox(height: 20),
// Display the result when the bottle animation is completed.
AnimatedBuilder(
animation: _animation,
builder: (context, child) {
if (_animation.status == AnimationStatus.completed) {
return Text('Spin Completed');
} else {
return Container(); // Display an empty container while spinning.
}
},
),
],
),
),
);
}
@override
void dispose() {
_controller.dispose(); // Dispose of the animation controller.
super.dispose();
}
}
Output:
Similar Reads
Flutter - Hinge Animation Animations are a big part of the Flutter application. It makes an app sweet to the eyes and equally user-friendly. In this article, we will discuss in detail the Hinge animations. In Flutter there are two ways to work with animations namely:A pub packageAnimated Builder WidgetIn this article, we wil
4 min read
Rive animations in Flutter Rive is a very useful animation tool that can create beautiful animations and we can add these in our Application. In flutter, we can add animations by writing so many lines of code but this is not a good practice for a developer. Instead of writing lines of code to create animation, we can create o
2 min read
Flutter - Lottie Animation Visualization is an integral part of any application. Animations can highly glorify the UI of an app, but animations can be hectic to implement for an application. This is where the Lottie animation comes in. Lottie is a JSON-based animation file. It can be used both as a network asset and a static
3 min read
Flutter - Wave Animation In this article, we are going to make a Flutter application that demonstrates the creation of a dynamic wave animation. This animation simulates the motion of water waves, creating an engaging visual effect that can be used in various Flutter applications to add styling effects. A sample video is gi
5 min read
Flutter - Movie Tween Animation Basically in Movie Tween Animation, we define different scenes, each with its own set of tweens and then Animate them. In Flutter we can achieve this type of animation easily by using the simple_animations package. In this article, we are going to implement the movie tween animation with the help of
6 min read