Open In App

Flutter – Spin Bottle Animation

Last Updated : 19 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads