AnimatedPadding widget and Attributes

AnimatedPadding widget and Attributes

ยท

2 min read

The AnimatedPadding widget in Flutter is used to animate changes to the padding of its child widget. It's useful for creating smooth transitions when the padding of a widget needs to change dynamically. This widget works similarly to Padding, but it animates changes to the padding over a specified duration.

Attributes:

  1. child (Widget):

    • The child widget that is wrapped by the AnimatedPadding widget.
  2. padding (EdgeInsetsGeometry):

    • The padding to apply to the child widget. This can be either EdgeInsets or EdgeInsetsDirectional.
  3. curve (Curve):

    • The animation curve to use for animating the padding change.
  4. duration (Duration):

    • The duration over which the padding change should be animated.

Example:

import 'package:flutter/material.dart';

class AnimatedPaddingExample extends StatefulWidget {
  @override
  _AnimatedPaddingExampleState createState() => _AnimatedPaddingExampleState();
}

class _AnimatedPaddingExampleState extends State<AnimatedPaddingExample> {
  double _paddingValue = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('AnimatedPadding Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            AnimatedPadding(
              padding: EdgeInsets.all(_paddingValue),
              duration: Duration(seconds: 1),
              curve: Curves.easeInOut,
              child: Container(
                width: 200,
                height: 200,
                color: Colors.blue,
                child: Center(
                  child: Text(
                    'AnimatedPadding',
                    style: TextStyle(color: Colors.white),
                  ),
                ),
              ),
            ),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: () {
                setState(() {
                  _paddingValue = _paddingValue == 0 ? 50 : 0;
                });
              },
              child: Text('Toggle Padding'),
            ),
          ],
        ),
      ),
    );
  }
}

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

In this example:

  • An AnimatedPadding widget is used to animate changes to the padding of a Container widget.

  • The padding attribute is set to _paddingValue, which is initially 0.

  • The duration attribute is set to Duration(seconds: 1), specifying the duration of the animation.

  • The curve attribute is set to Curves.easeInOut, defining the animation curve for the padding change.

  • When the user presses the "Toggle Padding" button, the _paddingValue is toggled between 0 and 50, triggering the animation of the padding change.

Did you find this article valuable?

Support Vinit Mepani (Flutter Developer) by becoming a sponsor. Any amount is appreciated!

ย