Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: How to use AnimatedContainer with Expanded in Column?

Let's say we have 3 children of a Column:

Flexible(
   flex:1,
   child: Container(
      color: Colors.red,
   ),
),
Flexible(
   flex:3,
   child: Container(
      color: Colors.yellow,
   ),
),
Flexible(
   flex:1,
   child: Container(
      color: Colors.blue,
   ),
),

I want to programmatically expand middle child with flex=3 to cover the whole Column with smooth animation, while the top and bottom children shrinks accordingly.

Right now my setup is with a Column and Flexible widgets. However if you can think of a solution with other widgets, I am open to those ideas as well.

like image 889
aytunch Avatar asked Oct 10 '19 17:10

aytunch


People also ask

How do you expand a flutter in container?

There are two options given by flutter, the first is FlexFit. tight which sets the child to fill the space available to it and the second is FlexFit. loose which allows the child widget to be as large as the available space.

How do you use AnimatedBuilder in flutter?

AnimatedBuilder is useful for more complex widgets that wish to include an animation as part of a larger build function. To use AnimatedBuilder, simply construct the widget and pass it a builder function. For simple cases without additional state, consider using AnimatedWidget.

How do you use tweens in flutter?

A linear interpolation between a beginning and ending value. Tween is useful if you want to interpolate across a range. To use a Tween object with an animation, call the Tween object's animate method and pass it the Animation object that you want to modify.

What is the difference between container and animatedcontainer in flutter?

In Flutter a container is a simple widget with well-defined properties like height, width, and color, etc. The AnimatedContainer widget is a simple container widget with animations. These types of widgets can be animated by altering the values of their properties which are the same as the Container widget.

Can I use animation with the sizetransition widget in flutter?

An example of a Widget that use animation with the SizeTransition widget: AnimatedContainer also works but Flutter can complain about overflow if the child is not resizable to zero width or zero height.

How to create an animation in flutter?

To start an animation, you need to change one of the property values. For example, you can change the value of color and Flutter will create an animation that changes the color gradually over the given duration. For this tutorial, we are going to use the State class below.

What is container widget in flutter?

Most of you have already known about Container, a very popular widget in Flutter. It's a widget that combines common painting, positioning, and sizing widgets. It has some properties such as color, alignment, padding, width, and height.


1 Answers

Screenshot:

enter image description here


Code:

Duration _duration = Duration(seconds: 1);
int _flex1 = 1, _flex2 = 3, _flex3 = 1;

@override
Widget build(BuildContext context) {

  double height = MediaQuery.of(context).size.height;
  var height1 = (_flex1 * height) / (_flex1 + _flex2 + _flex3);
  var height2 = (_flex2 * height) / (_flex1 + _flex2 + _flex3);
  var height3 = (_flex3 * height) / (_flex1 + _flex2 + _flex3);

  return Scaffold(
    body: Column(
      children: <Widget>[
        AnimatedContainer(
          duration: _duration,
          color: Colors.blue,
          height: height1,
          alignment: Alignment.center,
          child: Text("Flex: ${_flex1}", style: TextStyle(fontSize: 32, fontWeight: FontWeight.bold)),
        ),
        AnimatedContainer(
          duration: _duration,
          color: Colors.red,
          height: height2,
          alignment: Alignment.center,
          child: Text("Flex: ${_flex2}", style: TextStyle(fontSize: 32, fontWeight: FontWeight.bold)),
        ),
        AnimatedContainer(
          duration: _duration,
          color: Colors.teal,
          height: height3,
          alignment: Alignment.center,
          child: Text("Flex: ${_flex3}", style: TextStyle(fontSize: 32, fontWeight: FontWeight.bold)),
        ),
      ],
    ),
  );
}
like image 177
CopsOnRoad Avatar answered Nov 15 '22 05:11

CopsOnRoad