在Flutter中为特定小部件自定义旋转行为

3
在我的 Flutter 应用程序中,我使用 OrientationBuilder 小部件根据当前方向更改布局。但是小部件位置变化时发生的动画看起来不够流畅,UI 元素的缩放暂时变得奇怪。
有没有一种实现方向更改的方法,使元素保持在它们的位置,并自己旋转? 当前行为: Current Behavior 优先行为: Preferred Behavior 我省略了 Flutter 应用程序中的摄像头屏幕,因为这只涉及屏幕控制。摄像视图本身不是问题!

1
你有没有考虑使用缓动和计时器控制器来实现旋转动画? - Golden Lion
1
改变组件的旋转而不是整个屏幕怎么样?您可以将旋转设置为纵向默认,然后在检测到旋转更改时更新小部件。 - Crazzygamerr
我该怎么做呢?一旦我锁定了方向(至少使用SystemChrome.setPreferrerOrientation),就不再注册旋转了。 - Scott Clements
1个回答

1
我一直遇到同样的问题,相机预览旋转的效果很奇怪。最后我使用了这个包native orientation,它提供了以下API:Stream<NativeDeviceOrientation> onOrientationChanged(useSensor: false),即使你使用SystemChrome.setPreferredOrientations()锁定屏幕,也会触发方向变化。
基本上,在组件的初始化中:
_orientation = NativeDeviceOrientationCommunicator()
        .onOrientationChanged(useSensor: true)
        .listen((event) {
      switch (event) {
        case NativeDeviceOrientation.portraitUp:
        case NativeDeviceOrientation.unknown:
          _actualOrientation = DeviceOrientation.portraitUp;
          break;
        case NativeDeviceOrientation.portraitDown:
          _actualOrientation = DeviceOrientation.portraitDown;
          break;
        case NativeDeviceOrientation.landscapeLeft:
          _actualOrientation = DeviceOrientation.landscapeLeft;
          break;
        case NativeDeviceOrientation.landscapeRight:
          _actualOrientation = DeviceOrientation.landscapeRight;
          break;
      }
    });

然后使用实际方向来为图标添加动画或者了解捕获图像的实际方向。
记得在处理完毕后关闭订阅:_orientation.cancel();

谢谢,我几天前一直在找这种解决方案。 - Michał Jabłoński

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接