Skip to main content

Command Palette

Search for a command to run...

AnimatedBuilder widget and Attributes

Published
2 min read
AnimatedBuilder widget and Attributes
V

"Hello World, I'm Vinit Mepani, a coding virtuoso driven by passion, fueled by curiosity, and always poised to conquer challenges. Picture me as a digital explorer, navigating through the vast realms of code, forever in pursuit of innovation.

In the enchanting kingdom of algorithms and syntax, I wield my keyboard as a magical wand, casting spells of logic and crafting solutions to digital enigmas. With each line of code, I embark on an odyssey of learning, embracing the ever-evolving landscape of technology.

Eager to decode the secrets of the programming universe, I see challenges not as obstacles but as thrilling quests, opportunities to push boundaries and uncover new dimensions in the realm of possibilities.

In this symphony of zeros and ones, I am Vinit Mepani, a coder by passion, an adventurer in the digital wilderness, and a seeker of knowledge in the enchanting world of code. Join me on this quest, and let's create digital wonders together!"

The AnimatedBuilder widget in Flutter is used to animate a portion of the UI in response to changes in an animation. It's particularly useful when you want to create custom animations or when you need more control over how a widget is animated.

Attributes:

  1. animation (Animation):

    • The animation to listen to for changes.
  2. builder (Widget Function(BuildContext, Widget?)):

    • A builder function that returns a widget tree based on the current value of the animation. It takes two arguments: the BuildContext and the child widget.

Example:

import 'package:flutter/material.dart';

class AnimatedBuilderExample extends StatefulWidget {
  @override
  _AnimatedBuilderExampleState createState() => _AnimatedBuilderExampleState();
}

class _AnimatedBuilderExampleState extends State<AnimatedBuilderExample> with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _animation;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      vsync: this,
      duration: Duration(seconds: 2),
    );
    _animation = Tween<double>(begin: 0, end: 1).animate(_controller);
    _controller.repeat(reverse: true);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('AnimatedBuilder Widget Example'),
      ),
      body: Center(
        child: AnimatedBuilder(
          animation: _animation,
          builder: (context, child) {
            return Transform.scale(
              scale: _animation.value,
              child: Container(
                width: 100,
                height: 100,
                color: Colors.blue,
                child: Center(
                  child: Text(
                    'Animated Builder',
                    style: TextStyle(color: Colors.white),
                  ),
                ),
              ),
            );
          },
        ),
      ),
    );
  }
}

void main() {
  runApp(MaterialApp(
    home: AnimatedBuilderExample(),
  ));
}

Explanation:

  • In this example, an AnimationController is created to control the animation with a duration of 2 seconds.

  • A linear animation is created using Tween<double>(begin: 0, end: 1).

  • The _controller is set to repeat the animation indefinitely in a reverse manner.

  • Inside the build method, the AnimatedBuilder widget listens to changes in the animation.

  • The builder function is called whenever the animation value changes, allowing the transformation of a container's scale based on the animation value.

  • As the animation progresses, the container scales up and down smoothly, creating an animated effect.

More from this blog

Vinit Mepani (Flutter Developer)

270 posts

"Vinit Mepani, passionate coder! Dive into my Dart and Flutter journey on the blog. Let's master these tech wonders together. Happy coding! 🚀"