onDeviceOrientationChanged method

  1. @override
Stream<DeviceOrientationChangedEvent> onDeviceOrientationChanged()

The ui orientation changed.

Implementations for this:

  • Should support all 4 orientations.

Implementation

@override
Stream<DeviceOrientationChangedEvent> onDeviceOrientationChanged() {
  final html.ScreenOrientation? orientation = window?.screen?.orientation;

  if (orientation != null) {
    // Create an initial orientation event that emits the device orientation
    // as soon as subscribed to this stream.
    final html.Event initialOrientationEvent = html.Event('change');

    return orientation.onChange.startWith(initialOrientationEvent).map(
      (html.Event _) {
        final DeviceOrientation deviceOrientation = _cameraService
            .mapOrientationTypeToDeviceOrientation(orientation.type!);
        return DeviceOrientationChangedEvent(deviceOrientation);
      },
    );
  } else {
    return const Stream<DeviceOrientationChangedEvent>.empty();
  }
}