Skip to content

[camera_android_camerax] Restore torch state when camera changes - #13086

Draft
camsim99 wants to merge 5 commits into
flutter:mainfrom
camsim99:pos_torch_2
Draft

camsim99 wants to merge 5 commits into
flutter:mainfrom
camsim99:pos_torch_2

Conversation

@camsim99

@camsim99 camsim99 commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Continuation of #12302..

Restores the state of a camera when that camera was previously initialized. For example, if you initialize camera A and start the torch, then initialize camera B, then switch back to A, the torch will turn off when camera B is started but then turn back on when switching back to camera A.

Fixes flutter/flutter#160956.


Normal pull request information above the line.

This pull request is an attempt to "oneshot" fixing flutter/flutter#160956. Using antigravity to execute a plan that is collaboratively worked on. The "rules" are to not allow human authored code to camera_android_camerax and to not rely on human code review feedback for code iteration.

We can add documentation, skills, mcp servers, presubmit test etc. Basically any "support infrastructure" for development is allowed but the package changes must be generated as a single pass.
When we discover that the plan is not good enough we will blow the package changes away and either modify the plan or add more support infrastructure and try again.

The implementation plan used to create this PR can be found at commit: 2e222ee. It was created using Claude Opus 5.5 with high effort in the Antigravity IDE.

For more information see the project proposal doc go/flutter-project-one-shot or the working doc for this specific attempt go/flutter-project-one-shot-torch.

Pre-Review Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

Footnotes

  1. Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. ↩ ↩2


## Proposed solution

Keep `torchEnabled` as the **requested** torch mode, which is how the app sees it: `CameraValue.flashMode` stays `torch` across `setDescription`. Then **apply that request again every time the plugin gets a new CameraX `Camera`**, skipping cameras that have no flash unit. Reset the request on `dispose`, because a new `CameraController` starts from default settings.

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.

Sometimes there is more than a front/back camera. Should we consider saving a mapping of the camera to the torch state to account for this versus a single bool?

## Open questions

> [!IMPORTANT]
> **1. Expected behavior.** The report says "after step 4, the torch does not turn back **off**". I've assumed you meant "**on**", because you expect it to come back on and keep its state from step 2. Is that right?

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.

Yes, typo!

@reidbaker reidbaker left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Pr description should be updated to say what model/effort and harness you used.

…ching cameras

CameraX turns the torch off whenever a camera becomes inactive, but the
plugin cached torch mode as enabled and never re-applied it to the newly
bound camera, so the torch stayed off after switching cameras and
further requests to enable it were no-ops.

Re-enable the torch whenever the bound camera changes if torch mode was
requested and the camera has a flash unit (exposed via
CameraInfo.hasFlashUnit), and reset flash state on dispose.
# Conflicts:
#	packages/camera/camera_android_camerax/CHANGELOG.md
#	packages/camera/camera_android_camerax/pubspec.yaml

@camsim99 camsim99 left a comment

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.

Two major pieces of feedback to help improve our agent: (1) if there is no value to testing something on hardware, write a unit test for it rather than an integration test. (2) If adding convenience methods/wrappers, aim to improve the entire suite rather than only considering the newly added tests.

(2) might be a hot take so open to pushback 🤷‍♀️ maybe it can be part of the implementation plan to be approved/rejected before implementation.

await newController.dispose();
});

testWidgets('Torch mode is retained after switching cameras', (WidgetTester tester) async {

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.

I'm not convinced this should be an integration test. We aren't using adb or anything to check the flash is on/off, so it looks equivalent to a unit test.

/// CameraX returns a new [Camera] each time use cases are bound, so each
/// camera switch in these tests is modeled by a separate [MockCamera]. As
/// in CameraX, enabling the torch on a camera without a flash unit fails.
MockCamera createMockCamera({

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.

We now have a lot of different methods to customize mock objects and I'm worried about this file bloating more than it already has. If agents add methods like this, I think they should try to do it effectively by, for example, designing these methods so that as many tests as possible can be refactored to use it to hopefully strengthen the entire group of tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[camera_android_camerax]The torch doesn't turn on back after switching to rear camera even after a manual test set

2 participants