0
0
Fluttermobile~20 mins

Implicit animations (AnimatedContainer, AnimatedOpacity) in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Implicit Animation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What happens when you tap the AnimatedContainer below?

Consider this Flutter widget that uses AnimatedContainer. What will you see after tapping the container?

Flutter
class MyWidget extends StatefulWidget {
  @override
  State<MyWidget> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  bool selected = false;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => setState(() => selected = !selected),
      child: AnimatedContainer(
        width: selected ? 200 : 100,
        height: selected ? 100 : 200,
        color: selected ? Colors.blue : Colors.red,
        duration: Duration(seconds: 1),
        curve: Curves.easeInOut,
      ),
    );
  }
}
AThe container instantly changes size and color without animation.
BThe container smoothly changes size and color between red and blue over 1 second.
CThe container changes color but size stays the same.
DNothing happens because AnimatedContainer requires an explicit animation controller.
Attempts:
2 left
💡 Hint

AnimatedContainer automatically animates changes to its properties over the given duration.

ui_behavior
intermediate
2:00remaining
What is the visual effect of this AnimatedOpacity widget when toggled?

Look at this Flutter widget using AnimatedOpacity. What will the user see when the button is pressed?

Flutter
class FadeWidget extends StatefulWidget {
  @override
  State<FadeWidget> createState() => _FadeWidgetState();
}

class _FadeWidgetState extends State<FadeWidget> {
  bool visible = true;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        AnimatedOpacity(
          opacity: visible ? 1.0 : 0.0,
          duration: Duration(milliseconds: 500),
          child: Container(
            width: 100,
            height: 100,
            color: Colors.green,
          ),
        ),
        ElevatedButton(
          onPressed: () => setState(() => visible = !visible),
          child: Text('Toggle'),
        ),
      ],
    );
  }
}
AThe green box changes size but stays fully visible.
BThe green box slides left and right when toggled.
CThe green box fades in and out smoothly when the button is pressed.
DThe green box instantly appears and disappears without animation.
Attempts:
2 left
💡 Hint

AnimatedOpacity animates the transparency of its child widget.

lifecycle
advanced
2:00remaining
Why does AnimatedContainer rebuild its animation when setState is called?

In Flutter, when you call setState to change properties of an AnimatedContainer, why does it animate the change instead of instantly updating?

ABecause AnimatedContainer requires an explicit AnimationController to animate changes.
BBecause setState triggers a rebuild that resets the animation controller to zero.
CBecause Flutter caches the widget and does not rebuild it on setState.
DBecause AnimatedContainer listens to property changes and automatically animates between old and new values.
Attempts:
2 left
💡 Hint

Think about how implicit animations work in Flutter.

📝 Syntax
advanced
2:00remaining
Which option correctly uses AnimatedOpacity to fade out a widget over 2 seconds?

Choose the correct Flutter code snippet that fades out a widget smoothly over 2 seconds using AnimatedOpacity.

AAnimatedOpacity(opacity: 0.0, duration: Duration(seconds: 2), child: Text('Fade'))
BAnimatedOpacity(opacity: 0, duration: 2, child: Text('Fade'))
CAnimatedOpacity(opacity: 0.0, duration: Duration(milliseconds: 2000), child: Text('Fade'))
DAnimatedOpacity(opacity: 0.0, duration: 2.seconds, child: Text('Fade'))
Attempts:
2 left
💡 Hint

Duration must be created with Duration() and seconds as a named parameter.

🔧 Debug
expert
3:00remaining
Why does this AnimatedContainer not animate on property change?

Given this Flutter code, why does the container jump instantly instead of animating?

class MyWidget extends StatefulWidget {
  @override
  State createState() => _MyWidgetState();
}

class _MyWidgetState extends State {
  bool selected = false;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => selected = !selected,
      child: AnimatedContainer(
        width: selected ? 200 : 100,
        height: selected ? 100 : 200,
        color: selected ? Colors.blue : Colors.red,
        duration: Duration(seconds: 1),
      ),
    );
  }
}
ABecause setState() is not called after changing the selected variable, so widget does not rebuild.
BBecause AnimatedContainer requires an explicit AnimationController to animate.
CBecause the duration is too short to see the animation.
DBecause GestureDetector does not detect taps on AnimatedContainer.
Attempts:
2 left
💡 Hint

Remember what triggers a widget rebuild in Flutter.