【问题标题】:How can i check that device has notch without using SafeArea in flutter?如何在不使用 SafeArea 的情况下检查设备是否有缺口?
【发布时间】:2021-10-20 21:17:11
【问题描述】:

我正在设计一个视频通话屏幕,我必须在孔屏幕之间管理 selfViewContainer。

在这个屏幕上,我已经管理了大部分用于水平对齐的部分,但是对于 vertical,它在有凹槽的设备中无法正常工作。

我想在拖动时从顶部最小填充 50

这是可拖动的橙色容器的代码

Widget _selfView() {

    return Positioned(
      top: _selfViewTop,
      left: _selfViewLeft,
      child: Draggable(
        feedback: _draggableView(),
        childWhenDragging: Container(),
        child: _draggableView(),
        onDragEnd: (dragDetail) {
          var screenWidth = AspectSize.getScreenWidth(context: context);
          var screenHeight = AspectSize.getScreenHeight(context: context);

          _selfViewLeft = dragDetail.offset.dx;
          _selfViewTop = dragDetail.offset.dy;

          if (_selfViewLeft < (screenWidth / 2)) {
            _selfViewLeft = 16.0;
          } else if (_selfViewLeft > (screenWidth / 2)) {
            _selfViewLeft = (screenWidth) - (_selfViewWidth + 16);
          }

          if (_selfViewLeft < 1.0) {
            _selfViewLeft = 16.0;
          } else if ((_selfViewLeft + _selfViewWidth) > screenWidth) {
            _selfViewLeft = (screenWidth) - (_selfViewWidth + 16);
          }
          if (_selfViewTop < 1.0) {
            _selfViewTop = 50;
          } else if ((_selfViewTop + _selfViewHeight) > screenHeight) {
            _selfViewTop = (screenHeight) - (_selfViewWidth + 50);
          }

          setState(() {});
        },
      ),
    );
  }

对于这个 UI,我使用 Stack 小部件来管理容器,橙色容器用于显示呼叫者屏幕,蓝色容器将显示另一个人的照片。

请给我一些解决方案。

谢谢。

【问题讨论】:

    标签: android ios flutter draggable


    【解决方案1】:

    你可以使用 MediaQuery.of(context).viewPadding 并检查填充是否大于零,然后你需要一个安全区域,因为有一个缺口 .

    处理缺口行为的最佳方法是使用flutter_device_type 包。

    上述包中的以下代码可以确定缺口:

    if( Device.get().hasNotch ){
    //Do some notch business
     }
    

    谢谢。

    【讨论】:

      猜你喜欢
      • 2020-10-28
      • 1970-01-01
      • 1970-01-01
      • 2011-12-27
      • 2020-03-09
      • 1970-01-01
      • 1970-01-01
      • 2015-03-03
      • 2013-04-12
      相关资源
      最近更新 更多