当用户摇动或旋转 phone 时,如何配置传感器(加速度计或陀螺仪)来执行某些操作?

How can I configure the sensors (Accelerometer or gyroscope) to do something when the user shakes or rotate the phone?

当用户摇动或旋转 phone 时,如何配置传感器(加速度计或陀螺仪)来执行某些操作?

我想在用户每次摇动 phone 时显示一个新字符串。

我还需要一个按钮来启动和完成传感器“动作”。谢谢!!!!!!

使用flutter_shake_plugin,你可以尝试这样的操作:

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter_shake_plugin/flutter_shake_plugin.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  FlutterShakePlugin _shakePlugin;

  @override
  void initState() {
    super.initState();
    _shakePlugin = FlutterShakePlugin(
      onPhoneShaken: (){
        //do stuff on phone shake
        print('phone shakes');
      },
    )..startListening();
  }

  @override
  void dispose() {
    super.dispose();
    _shakePlugin.stopListening();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Shake Plugin example app'),
        ),
      ),
    );
  }
}

如果你想看活生生的例子。看看 this link 我已经在我的一个应用程序中实现了。