Bird
Raised Fist0
Fluttermobile~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does AnimatedOpacity do in Flutter?
easy
A. It smoothly changes the transparency of a widget over time.
B. It animates the size of a widget instantly without transition.
C. It changes the color of a widget without animation.
D. It moves a widget from one position to another without animation.

Solution

  1. Step 1: Understand the purpose of AnimatedOpacity

    AnimatedOpacity is designed to animate changes in a widget's opacity, making it fade in or out smoothly.
  2. Step 2: Compare with other options

    Options A, B, and C describe animations or changes that AnimatedOpacity does not perform.
  3. Final Answer:

    It smoothly changes the transparency of a widget over time. -> Option A
  4. Quick Check:

    AnimatedOpacity = smooth transparency change [OK]
Hint: Opacity means transparency; AnimatedOpacity animates it smoothly [OK]
Common Mistakes:
  • Confusing opacity with size or color animation
  • Thinking it moves widgets instead of fading them
  • Assuming it changes instantly without animation
2. Which of the following is the correct way to create an AnimatedContainer with a duration of 500 milliseconds?
easy
A. AnimatedContainer(duration: Duration(seconds: 0.5), color: Colors.red)
B. AnimatedContainer(duration: Duration(milliseconds: 500), color: Colors.red)
C. AnimatedContainer(duration: 500, color: Colors.red)
D. AnimatedContainer(duration: Duration(microseconds: 500), color: Colors.red)

Solution

  1. Step 1: Check the Duration syntax in Flutter

    Duration expects named parameters like milliseconds or seconds, not raw numbers.
  2. Step 2: Identify the correct Duration usage

    Duration(milliseconds: 500) correctly sets 500 milliseconds. AnimatedContainer(duration: Duration(seconds: 0.5), color: Colors.red) uses seconds with a decimal, which is invalid. AnimatedContainer(duration: 500, color: Colors.red) passes an int directly, which is wrong. AnimatedContainer(duration: Duration(microseconds: 500), color: Colors.red) uses microseconds, which is too small.
  3. Final Answer:

    AnimatedContainer(duration: Duration(milliseconds: 500), color: Colors.red) -> Option B
  4. Quick Check:

    Duration(milliseconds: 500) is correct [OK]
Hint: Use Duration(milliseconds: value) for millisecond timing [OK]
Common Mistakes:
  • Passing raw int instead of Duration object
  • Using seconds with decimal (not allowed)
  • Confusing microseconds with milliseconds
3. What will happen when the following Flutter code runs and the button is pressed?
class MyWidget extends StatefulWidget {
  @override
  State<MyWidget> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  bool _visible = true;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        AnimatedOpacity(
          opacity: _visible ? 1.0 : 0.0,
          duration: Duration(seconds: 1),
          child: Container(width: 100, height: 100, color: Colors.blue),
        ),
        ElevatedButton(
          onPressed: () {
            setState(() {
              _visible = !_visible;
            });
          },
          child: Text('Toggle'),
        ),
      ],
    );
  }
}
medium
A. The blue box will fade in and out smoothly when the button is pressed.
B. The blue box will instantly appear and disappear without animation.
C. The button press will cause a runtime error due to missing opacity value.
D. The blue box will change size instead of opacity.

Solution

  1. Step 1: Understand AnimatedOpacity behavior

    AnimatedOpacity animates changes in opacity smoothly over the given duration.
  2. Step 2: Analyze the button press effect

    Pressing the button toggles _visible between true and false, changing opacity from 1.0 to 0.0 or vice versa, triggering the fade animation.
  3. Final Answer:

    The blue box will fade in and out smoothly when the button is pressed. -> Option A
  4. Quick Check:

    AnimatedOpacity toggles opacity smoothly [OK]
Hint: Toggle opacity between 1 and 0 for fade effect [OK]
Common Mistakes:
  • Thinking opacity changes instantly without animation
  • Confusing opacity with size or color changes
  • Assuming missing opacity causes error
4. Identify the error in this Flutter code snippet using AnimatedContainer and how to fix it:
AnimatedContainer(
  width: 100,
  height: 100,
  color: Colors.green,
  duration: 500,
)
medium
A. AnimatedContainer requires a child widget; add a child property.
B. The color property should be inside a BoxDecoration, not directly set.
C. Width and height must be inside constraints, not directly set.
D. The duration should be a Duration object, not an int; fix with Duration(milliseconds: 500).

Solution

  1. Step 1: Check the duration property type

    AnimatedContainer's duration expects a Duration object, not a raw integer.
  2. Step 2: Correct the duration syntax

    Replace duration: 500 with duration: Duration(milliseconds: 500) to fix the error.
  3. Final Answer:

    The duration should be a Duration object, not an int; fix with Duration(milliseconds: 500). -> Option D
  4. Quick Check:

    Duration must be Duration object [OK]
Hint: Always wrap time in Duration() for animation durations [OK]
Common Mistakes:
  • Passing int directly to duration
  • Misplacing color property (valid here though)
  • Thinking child is mandatory for AnimatedContainer
5. You want to create a widget that smoothly changes its size and fades out at the same time when a button is pressed. Which Flutter widget combination correctly achieves this behavior?
hard
A. Use only AnimatedContainer with opacity property set to animate transparency.
B. Use AnimatedOpacity with width and height properties to animate size and opacity together.
C. Wrap an AnimatedContainer inside an AnimatedOpacity, both controlled by the same state variable.
D. Use a Container with manual setState calls to animate size and opacity.

Solution

  1. Step 1: Understand capabilities of AnimatedContainer and AnimatedOpacity

    AnimatedContainer animates size, color, and other box properties but not opacity. AnimatedOpacity animates transparency only.
  2. Step 2: Combine widgets for simultaneous animations

    Wrapping AnimatedContainer inside AnimatedOpacity allows simultaneous size and opacity animations controlled by the same state.
  3. Step 3: Evaluate other options

    Use only AnimatedContainer with opacity property set to animate transparency. is incorrect because AnimatedContainer does not animate opacity. Use AnimatedOpacity with width and height properties to animate size and opacity together. is invalid because AnimatedOpacity does not animate size. Use a Container with manual setState calls to animate size and opacity. lacks implicit animation and requires manual animation code.
  4. Final Answer:

    Wrap an AnimatedContainer inside an AnimatedOpacity, both controlled by the same state variable. -> Option C
  5. Quick Check:

    Combine AnimatedContainer + AnimatedOpacity for size + fade [OK]
Hint: Use AnimatedOpacity outside AnimatedContainer for fade + size [OK]
Common Mistakes:
  • Expecting AnimatedContainer to animate opacity
  • Trying to animate size with AnimatedOpacity
  • Using Container without animation widgets
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard