detectShakeAndOpenListCurlScreen static method

void detectShakeAndOpenListCurlScreen({
  1. required BuildContext? buildContext,
  2. required GlobalKey<NavigatorState>? navigatorKey,
  3. double shakeThresholdGravity = 1,
})

Listens for shake gestures and opens the ListCurlScreen if a shake is detected.

buildContext is the current build context, used to navigate to the ListCurlScreen. navigatorKey is an optional key for the navigator, used to obtain the context. shakeThresholdGravity is the threshold for detecting a shake, with a default value of 1.

Implementation

static void detectShakeAndOpenListCurlScreen({
  required BuildContext? buildContext,
  required GlobalKey<NavigatorState>? navigatorKey,
  double shakeThresholdGravity = 1,
}) {
  int lastShakeTimestamp = 0;
  int shakeSlopTimeMs = 500;

  // Listen to the user accelerometer events to detect shakes.
  userAccelerometerEventStream().listen(
    (UserAccelerometerEvent event) {
      double gX = event.x / 9.80665;
      double gY = event.y / 9.80665;
      double gZ = event.z / 9.80665;

      // Calculate the gForce to determine if a shake has occurred.
      double gForce = math.sqrt(gX * gX + gY * gY + gZ * gZ);
      if (gForce > shakeThresholdGravity) {
        final now = DateTime.now().millisecondsSinceEpoch;
        if (lastShakeTimestamp + shakeSlopTimeMs > now) {
          return;
        }
        lastShakeTimestamp = now;
        final context =
            navigatorKey?.currentState?.overlay?.context ?? buildContext;
        if (context != null &&
            context.mounted &&
            !CurlLogs.instance.isAlreadyOpen) {
          CurlLogs.instance.isAlreadyOpen = true;
          Navigator.of(context).push(
            MaterialPageRoute(builder: (context) => const ListCurlScreen()),
          );
        }
      }
    },
    onError: (e) {
      debugPrint(
        'It seems that your device doesn\'t support User Accelerometer Sensor',
      );
    },
    cancelOnError: true,
  );
}