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.
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.
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.
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.
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.
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.
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.
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.
Screenshot:
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)),
),
],
),
);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With