weta-vn / advance_image_picker

Flutter plugin for selecting multiple images from the Android and iOS image library, taking new pictures with the camera, and edit them before using such as rotating, cropping, adding sticker/filters.
BSD 3-Clause "New" or "Revised" License
110 stars 49 forks source link

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

Open VuillaumeGautier opened 2 years ago

VuillaumeGautier commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

@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.