Skip to content

Make sure that an AnimatedSwitcher doesn't crash in 0x0 environment#180190

Merged
auto-submit[bot] merged 4 commits into
flutter:masterfrom
ahmedsameha1:handle#6537-AnimatedSwitcher
Jan 21, 2026
Merged

Make sure that an AnimatedSwitcher doesn't crash in 0x0 environment#180190
auto-submit[bot] merged 4 commits into
flutter:masterfrom
ahmedsameha1:handle#6537-AnimatedSwitcher

Conversation

@ahmedsameha1

Copy link
Copy Markdown
Contributor

This is my attempt to handle #6537 for the AnimatedSwitcher widget.

@github-actions github-actions Bot added the framework flutter/packages/flutter repository. See also f: labels. label Dec 22, 2025

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds a regression test for an issue where AnimatedSwitcher could crash when placed in a zero-sized environment. While the added test is a good start, it only covers the initial build and not the case where the child of the AnimatedSwitcher is changed, which was a key part of the original bug report. I've suggested an improvement to the test to make it more comprehensive and ensure the fix is fully covered.

Comment on lines +420 to +430
testWidgets('AnimatedSwitcher does not crash at zero area', (WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: Center(
child: SizedBox.shrink(child: AnimatedSwitcher(duration: Duration(microseconds: 500))),
),
),
);
expect(tester.getSize(find.byType(AnimatedSwitcher)), Size.zero);
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The added test case is a good start for verifying the fix, but it only covers the initial creation of the AnimatedSwitcher with a null child in a zero-sized area. The original issue (#6537) mentions that the crash occurs when the child is changed. To ensure the fix is robust, the test should also cover the scenario of switching children.

  testWidgets('AnimatedSwitcher does not crash at zero area', (WidgetTester tester) async {
    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
        child: Center(
          child: SizedBox.shrink(child: AnimatedSwitcher(duration: Duration(microseconds: 500))),
        ),
      ),
    );
    expect(tester.getSize(find.byType(AnimatedSwitcher)), Size.zero);

    // Switch to a child.
    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
        child: Center(
          child: SizedBox.shrink(
            child: AnimatedSwitcher(
              duration: Duration(microseconds: 500),
              child: Text('1', key: Key('1')),
            ),
          ),
        ),
      ),
    );
    expect(tester.getSize(find.byType(AnimatedSwitcher)), Size.zero);

    // Switch to another child.
    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
        child: Center(
          child: SizedBox.shrink(
            child: AnimatedSwitcher(
              duration: Duration(microseconds: 500),
              child: Text('2', key: Key('2')),
            ),
          ),
        ),
      ),
    );
    expect(tester.getSize(find.byType(AnimatedSwitcher)), Size.zero);
  });

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this doable?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check now.

@victorsanni victorsanni left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about a 0x0 test for AnimatedSwitcher. I don't think it has its own renderbox vs. just handling animations for its children.

@victorsanni

Copy link
Copy Markdown
Contributor

Not sure about a 0x0 test for AnimatedSwitcher. I don't think it has its own renderbox vs. just handling animations for its children.

And wrapping the animating children with an AnimatedSwitcher.layoutBuilder which is just a widget builder. So if the widget returned here already has a 0x0 test I don't know that an additional one is needed.

@dkwingsmt

Copy link
Copy Markdown
Contributor

I think it might be ok to verify that it doesn't add an additional render object on top of the built children.

@victorsanni victorsanni requested a review from dkwingsmt January 9, 2026 23:55

@dkwingsmt dkwingsmt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dkwingsmt dkwingsmt requested a review from victorsanni January 12, 2026 05:27
@flutter-dashboard

Copy link
Copy Markdown

Golden file changes have been found for this pull request. Click here to view and triage (e.g. because this is an intentional change).

If you are still iterating on this change and are not ready to resolve the images on the Flutter Gold dashboard, consider marking this PR as a draft pull request above. You will still be able to view image results on the dashboard, commenting will be silenced, and the check will not try to resolve itself until marked ready for review.

For more guidance, visit Writing a golden file test for package:flutter.

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

Changes reported for pull request #180190 at sha 4e6ebfc

@flutter-dashboard flutter-dashboard Bot added the will affect goldens Changes to golden files label Jan 12, 2026
@victorsanni victorsanni added the autosubmit Merge PR when tree becomes green via auto submit App label Jan 20, 2026
@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Jan 20, 2026
@auto-submit

auto-submit Bot commented Jan 20, 2026

Copy link
Copy Markdown
Contributor

autosubmit label was removed for flutter/flutter/180190, because The base commit of the PR is older than 7 days and can not be merged. Please merge the latest changes from the main into this branch and resubmit the PR.

@justinmc justinmc added the autosubmit Merge PR when tree becomes green via auto submit App label Jan 20, 2026
@auto-submit auto-submit Bot added this pull request to the merge queue Jan 20, 2026
Merged via the queue into flutter:master with commit 93ef5e4 Jan 21, 2026
71 checks passed
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Jan 21, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jan 21, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jan 21, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jan 21, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jan 22, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jan 22, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jan 22, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jan 23, 2026
flutter-zl pushed a commit to flutter-zl/flutter that referenced this pull request Feb 10, 2026
…lutter#180190)

This is my attempt to handle
flutter#6537 for the AnimatedSwitcher
widget.

---------

Co-authored-by: Tong Mu <dkwingsmt@users.noreply.github.com>
Co-authored-by: Victor Sanni <victorsanniay@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

framework flutter/packages/flutter repository. See also f: labels. will affect goldens Changes to golden files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants