text
stringlengths
1
372
height: _height,
decoration: BoxDecoration(
color: _color,
borderRadius: _borderRadius,
),
// define how long the animation should take.
duration: const duration(seconds: 1),
// provide an optional curve to make the animation feel smoother.
curve: Curves.fastOutSlowIn,
)
<code_end>
<topic_end>
<topic_start>
3. start the animation by rebuilding with new properties
finally, start the animation by rebuilding the
AnimatedContainer with the new properties.
how to trigger a rebuild?
use the setState() method.
add a button to the app. when the user taps the button, update
the properties with a new width, height, background color and border radius
inside a call to setState().
a real app typically transitions between fixed values (for example,
from a grey to a green background). for this app,
generate new values each time the user taps the button.
<code_start>
FloatingActionButton(
// when the user taps the button
onPressed: () {
// use setState to rebuild the widget with new values.
setState(() {
// create a random number generator.
final random = random();
// generate a random width and height.
_width = random.nextInt(300).toDouble();
_height = random.nextInt(300).toDouble();
// generate a random color.
_color = Color.fromRGBO(
random.nextInt(256),
random.nextInt(256),
random.nextInt(256),
1,
);
// generate a random border radius.
_borderRadius =
BorderRadius.circular(random.nextInt(100).toDouble());
});
},
child: const Icon(Icons.play_arrow),
)
<code_end>
<topic_end>
<topic_start>
interactive example
<code_start>
import 'dart:math';
import 'package:flutter/material.dart';
void main() => runApp(const AnimatedContainerApp());
class AnimatedContainerApp extends StatefulWidget {
const AnimatedContainerApp({super.key});
@override
State<AnimatedContainerApp> createState() => _AnimatedContainerAppState();
}
class _AnimatedContainerAppState extends State<AnimatedContainerApp> {
// define the various properties with default values. update these properties
// when the user taps a FloatingActionButton.
double _width = 50;
double _height = 50;
color _color = colors.green;
BorderRadiusGeometry _borderRadius = BorderRadius.circular(8);
@override
widget build(BuildContext context) {
return MaterialApp(
home: scaffold(
appBar: AppBar(
title: const Text('AnimatedContainer demo'),
),
body: center(
child: AnimatedContainer(
// use the properties stored in the state class.
width: _width,
height: _height,
decoration: BoxDecoration(
color: _color,
borderRadius: _borderRadius,
),
// define how long the animation should take.
duration: const duration(seconds: 1),
// provide an optional curve to make the animation feel smoother.
curve: Curves.fastOutSlowIn,
),
),
floatingActionButton: FloatingActionButton(
// when the user taps the button
onPressed: () {
// use setState to rebuild the widget with new values.
setState(() {
// create a random number generator.
final random = random();
// generate a random width and height.
_width = random.nextInt(300).toDouble();