需要帮助合成触摸事件

时间:2019-06-15 18:33:27

标签: flutter

我能够专注于小部件,但是当按遥控器上的中间(OK / ENTER)按钮时,以编程方式创建“敲击”事件时遇到了问题。 指针向下事件似乎可以正常工作,但指针向上事件似乎不起作用。

复制步骤

  1. 实施DefaultFocusNodeFocusTraversalPolicy
  2. 获取焦点节点。
  3. 尝试创建指针向下和向上事件。

相关代码

此处提供了整个项目:https://github.com/ApolloTVofficial/kamino/tree/atv

  bool _handleKeyEvent(FocusNode node, RawKeyEvent event){
    const KEY_ENTER = 1108101562391;

    if(event.logicalKey != null && event.logicalKey.keyId == KEY_ENTER){
      final renderObject = context.findRenderObject();
      if(renderObject is RenderBox){
        // Get the currently focused node.
        FocusNode focusedNode = node.enclosingScope.children.first.children.where((node) => node.hasFocus).first;

        // Get a list of elements at the focused node's coordinates
        BoxHitTestResult result = BoxHitTestResult();
        renderObject.hitTest(result, position: focusedNode.rect.center);

        // Generate the appropriate pointer event.
        PointerEvent pointerEvent;
        if(event is RawKeyDownEvent) pointerEvent = PointerDownEvent(
            device: 0,
            kind: PointerDeviceKind.touch,
            pointer: 1,
            buttons: kPrimaryButton,
            pressure: 1
        );

        if(event is RawKeyUpEvent) pointerEvent = PointerUpEvent(
            device: 0,
            kind: PointerDeviceKind.touch,
            pointer: 1,
            buttons: kPrimaryButton,
            pressure: 1
        );

        // Call handleEvent on that pointer event.
        result.path.forEach((entry) => entry.target.handleEvent(pointerEvent, entry));

      }
      return false;
    }

    if(event is RawKeyDownEvent){
      if(event.logicalKey == LogicalKeyboardKey.arrowUp){
        node.focusInDirection(TraversalDirection.up);
        return true;
      }

      if(event.logicalKey == LogicalKeyboardKey.arrowDown){
        node.focusInDirection(TraversalDirection.down);
        return true;
      }

      if(event.logicalKey == LogicalKeyboardKey.arrowLeft){
        node.focusInDirection(TraversalDirection.left);
        return true;
      }

      if(event.logicalKey == LogicalKeyboardKey.arrowRight){
        node.focusInDirection(TraversalDirection.right);
        return true;
      }
    }

    return false;
  }

Flutter Doctor输出

flutter doctor -v的输出如下:

[✓] Flutter (Channel dev, v1.6.6, on Mac OS X 10.14.2 18C54, locale en-GB)
    • Flutter version 1.6.6
    • Framework revision e1a784ae3f (2 days ago), 2019-05-28 21:53:03 -0700
    • Engine revision 8dc3a4cde2
    • Dart version 2.3.2 (build 2.3.2-dev.0.0 e3edfd36b2)


[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    • Android SDK
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-28, build-tools 28.0.3
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1136-b06)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 10.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 10.1, Build version 10B61
    • CocoaPods version 1.5.3

[✓] iOS tools - develop for iOS devices
    • ios-deploy 1.9.4

[✓] Android Studio (version 3.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 29.0.2
    • Dart plugin version 181.5540.11
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1136-b06)

[✓] IntelliJ IDEA Ultimate Edition (version 2019.1.1)
    • IntelliJ at /Applications/IntelliJ IDEA.app
    • Flutter plugin version 35.2.3
    • Dart plugin version 191.7019

[✓] Connected device (1 available)
    • BRAVIA 4K GB • 192.168.1.104:5555 • android-arm • Android 7.0 (API 24)

• No issues found!

0 个答案:

没有答案