Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue : using the image picker resets the phone orientation allowed #28

Open
VuillaumeGautier opened this issue Sep 27, 2021 · 3 comments

Comments

@VuillaumeGautier
Copy link

VuillaumeGautier commented Sep 27, 2021

Hi ! We've found a small issue while using the picker.
Our app is locked in portrait mode, but after using this library and leaving it, the app allows all 4 modes without any restriction.
Is this a normal behavior (allowing picker phone modes will overwrite the app modes) and should we implement a local workaround ? Thanks

@rydmike
Copy link
Contributor

rydmike commented Sep 27, 2021

Hi again @VuillaumeGautier,

You are indeed right. The Image picker locks its camera UI to vertical mode only, and when done it removes it and restores all normal rotations on the system.

It uses the PortraitStatefulModeMixin mixin below on its screens to only use portrait mode, but when done it restores rotation to all enabled, to not mess it up from "default" allow all mode, that many might just use as default.

/// Forces portrait-only mode application-wide
/// Use this Mixin on the main app widget i.e. app.Dart
/// Flutter's 'App' has to extend Stateless widget.
///
/// Call `super.build(context)` in the main build() method
/// to enable portrait only mode
mixin PortraitModeMixin on StatelessWidget {
  @override
  Widget build(BuildContext context) {
    _portraitModeOnly();
    return const SizedBox();
  }
}

// Forces portrait-only mode on a specific screen
/// Use this Mixin in the specific screen you want to
/// block to portrait only mode.
///
/// Call `super.build(context)` in the State's build() method
/// and `super.dispose();` in the State's dispose() method
mixin PortraitStatefulModeMixin<T extends StatefulWidget> on State<T> {
  @override
  Widget build(BuildContext context) {
    _portraitModeOnly();
    return const SizedBox();
  }

  @override
  void dispose() {
    super.dispose();
    _enableRotation();
  }
}

/// blocks rotation; sets orientation to: portrait
void _portraitModeOnly() {
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
  ]);
}

void _enableRotation() {
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
    DeviceOrientation.landscapeLeft,
    DeviceOrientation.landscapeRight,
  ]);
}

As a simple fix I guess you could call

  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
  ]);

After using the picker plugin, if your app also only allows portrait mode, or use the same mixin on your screens as well.

I wonder if it is possible to read the system chrome rotation mode orientations, store them in the state of the PortraitStatefulModeMixin, then set forced portrait mode and restore what it had when it was called in dispose. That would then never mess it up for whatever the using app had.

Ultimately I do think supporting landscape mode might be useful too.

@VuillaumeGautier
Copy link
Author

It does seems like the orientations in SystemChrome cannot be get. I guess it could be possible to add to the configuration file a list of DeviceOrientation to be called through in the _enableRotation. I'll add the workaround until there's a fix for the lib.

@rydmike
Copy link
Contributor

rydmike commented Sep 27, 2021

@VuillaumeGautier Good idea, that could certainly be added, even without breaking current behavior. I'll put that up as config option to add as well.

I wonder why the camera/picker does not support landscape mode, with option to lock in either mode?

I might take a peak at it. Supporting landscape might be possible with the camera itself, with some different layout of the UI needed of course, but perhaps it is not possible with some of the packages it depends on, but if you switch to those it could rotate to portrait then, at least then you could take landscape pics.

I might investigate this later too, I saw other issues have also requested landscape mode pics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants