简体   繁体   中英

How to recognize the Google glass swipe up gesture with Flutter?

I'm a beginner with Flutter and I'm trying to get the gestures from a Google Glass Enterprise Edition 2. I have found that the GestureDetector does not detect the swipe/tap movements of the Glass and that they are detected as keys. So I use the KeyboardListener and the keys I found are "shift left" for the swipe left , "tab" for the swipe right , "select" for the tap and "go back" for the swipe down (a bit strange but never mind).

However, I can't get the swipe up key to work, it exits the application directly. I tried a lot of things ( AbsorbPointer , Listener on touch event, MouseRegion ,WillPopScope ...) but no event seems to be triggered when I swipe up.

However, I have an app that was developed using Android Studio and has a feature that opens the bottom screen when I swipe up and does not exit the application. So, if this was possible with Android Studio, I think it is possible with Flutter to get the swipe up gesture and perform action, other than exiting the application.

Here is the code I use to read the key pressed:

class HomePage extends StatefulWidget {
  const HomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  double _startX = 0.0;
  double _startY = 0.0;
  double _endX = 0.0;
  double _endY = 0.0;

  final FocusNode _focusNode = FocusNode();

  // The message to display.
  String? _message;

  // Focus nodes need to be disposed.
  @override
  void dispose() {
    _focusNode.dispose();
    super.dispose();
  }

  // Called by onPanStart, works only on smartphone
  void _startSwipe(DragStartDetails details) {
    setState(() {
      _startX = details.globalPosition.dx;
      _startY = details.globalPosition.dy;
    });
  }

  // Called by onPanUpdate, works only on smartphone
  void _updateSwipe(DragUpdateDetails details) {
    setState(() {
      _endX = details.globalPosition.dx;
      _endY = details.globalPosition.dy;
    });
  }

  // Called by onPanEnd, works only on smartphone
  void _endSwipe(DragEndDetails details) {
    // Get screen height and width
    double height = MediaQuery.of(context).size.height;
    double width = MediaQuery.of(context).size.width;

    double xRatio = (_endX - _startX).abs() / width;
    double yRatio = (_endY - _startY).abs() / height;

    setState(() {
      // if horizontal move is greater than vertical move,
      // it's an horizontal swipe
      if (xRatio > yRatio) {
        if (_endX < _startX) {
          _message = 'Swipe left';
        } else {
          _message = 'Swipe right';
        }
      } else {
        // vertical move
        _message = 'Swipe bottom';
      }
    });
  }

  // Called by onKey, works only on Google glass
  void _tap() {
    setState(() {
      _message = 'Tap';
    });
  }

  // Called by onPanUpdate, works only on smartphone
  void _handleKeyEvent(RawKeyEvent event) {
    if (event is RawKeyUpEvent && !event.isShiftPressed) {
      setState(() {
        if (event.logicalKey == LogicalKeyboardKey.goBack) {
          _message = 'Swipe down';
          // } else if (event.logicalKey == ???) {
          //   _message = 'Swipe up';
        } else if (event.logicalKey == LogicalKeyboardKey.shiftLeft) {
          _message = 'Swipe left';
        } else if (event.logicalKey == LogicalKeyboardKey.tab) {
          _message = 'Swipe right';
        } else if (event.logicalKey == LogicalKeyboardKey.select) {
          _message = 'Tap';
        } else {
          if (kReleaseMode) {
            _message =
                'Pressed : 0x${event.logicalKey.keyId.toRadixString(16)}';
          } else {
            _message = 'Pressed : ${event.logicalKey.debugName}';
          }
        }
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      // Smartphone events
      onPanStart: _startSwipe,
      onPanUpdate: _updateSwipe,
      onPanEnd: _endSwipe,
      onTap: _tap,
      child: Scaffold(
        backgroundColor: Colors.black,
        body: Center(
          child: RawKeyboardListener(
            focusNode: _focusNode,
            // Google glass event
            onKey: _handleKeyEvent,
            child: Text(
              _message ?? 'Swipe',
              style: const TextStyle(
                fontSize: 20,
                color: Colors.white,
              ),
            ),
          ),
        ),
      ),
    );
  }
}

I have a Flutter documentation about Taps, drags, and other gestures but it works only on my smartphone. I also have a Google Glass documentation about gestures but it's adapted for Android Studio, not Flutter.

There is no real documentation on developing applications for Google Glass with Flutter, I'm running out of solutions. Is there anyone who has managed to get the swipe gesture of a Google Glass with Flutter and can help me?

With Google Glass Enterprise Edition 2, my Flutter Google Glass app didn't receive touch pad events until I added the following to the application section of my AndroidManifest.xml for the app:

<meta-data
        android:name="com.google.android.glass.TouchEnabledApplication"
        android:value="true" />

at which point the GestureDetector worked correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM