Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Animation in Flutter: How to resize a Stack?

What’s the right way to animate a change in size of a Stack widget?

The first idea was to wrap the Stack with a SizeTransition:

Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: [
        SizeTransition(
          sizeFactor: _height, // Just a Tween<double>(begin: 200, end: 400)
          axis: Axis.vertical,
          axisAlignment: -1,
          child: Stack(
            children: [
              Positioned(
                bottom: 10,
                left: 10,
                child: Container(width: 50, height: 50, color: Colors.blue),
              ),
              Positioned(
                top: 10,
                right: 10,
                child: Container(width: 50, height: 50, color: Colors.green),
              ),
            ],
          ),
        ),
      ],
    );
  }

But running the snippet causes the following error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

'package:flutter/src/rendering/stack.dart': Failed assertion: line 588 pos 12: 'size.isFinite': is not true.

So, apparently, that’s not the right way to do it. Questions now is: How do you animate resizing of a Stack?

Wrapping the Stack with a SizedBox would fix the error message, but then, how do you get the size from SizedTransition to set the proper size of a SizedBox?

>Solution :

You can use AnimatedContainer as parent of Stack widget by providing height or width one you like to change over time.

AnimatedContainer(
  color: Colors.purple,
  duration: const Duration(milliseconds: 200),
  height: height,
  child: Stack(
    children: [
      Positioned(
        bottom: 10,
        left: 10,
        child: Container(width: 50, height: 50, color: Colors.blue),
      ),
      Positioned(
        top: 10,
        right: 10,
        child: Container(width: 50, height: 50, color: Colors.green),
      ),
    ],
  ),
),
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading