If you are interested, you can follow the official account BigLead to get the latest learning materials.

  • The Flutter from Introduction to Mastery series is here
  • Of course also must be to have the source code here
  • Github is a little slow to take a look at the code cloud source code
  • The tutorial series is here

1 introduction

Sensor is a sensor detection device, has been widely used in smart phones, including Android, iOS two platforms.

1.1 gyroscope

Gyroscopes can be used to detect the holding mode of the equipment. The principle is to detect the angular velocity of rotation of the equipment on the X, Y and Z axes

1.2Motion Sensor /Accelerometer Sensor

Accelerometers are used to detect the acceleration of the device along the X, Y, and Z axes (in which direction the force is acting), and can be used to detect the shaking of the device. Classic applications include shaking


2 Adding a Dependency

In the FlutterUse the sensors_plus pluginTo get the acceleration sensor data

Add the following to the Flutter project

  sensors_plus: ^1.02.
Copy the code

3 USES sensors_plus

Guide package

import 'package:sensors_plus/sensors_plus.dart';
Copy the code

Listen to the mobile phone shake and achieve a shake display frame effect

/// code listing
class DemoSensorsPlusPage extends StatefulWidget {
  @override
  _DemoSensorsPlusPageState createState(a) => _DemoSensorsPlusPageState();
}

class _DemoSensorsPlusPageState extends State<DemoSensorsPlusPage> {
  bool _isShow = false;

  @override
  void initState(a) {
    super.initState();

    // Acceleration is affected by gravity
    accelerometerEvents.listen((AccelerometerEvent event) async {
      //[AccelerometerEvent (x: 0.0992431640625, y: 0.11407470703125, z: 9.776962280273438)]
      // print(event);
      int value = 10;
      if (event.x.abs() > value ||
          event.y.abs() > value ||
          event.z.abs() > value) {
        if(! _isShow) { _isShow =true;
          dynamic result = await showDialog<bool>(
              builder: (BuildContext context) {
                return CupertinoAlertDialog(
                  title: Text("Shake it."),
                  content: Image.asset(
                    "assets/images/banner1.png",
                    width: 200,),); }, context: context, barrierDismissible:true);
          _isShow = false; }}});// Acceleration is not affected by gravity
    userAccelerometerEvents.listen((UserAccelerometerEvent event) {});

    // Gyroscope sensor
    gyroscopeEvents.listen((GyroscopeEvent event) {
      GyroscopeEvent (x: 0.00042724609375, Y: 0.0005340576171875, Z: -0.0003204345703125)]
      // print(event);
    });
  }

  @override
  Widget build(BuildContext context) {
    /// Use the Scaffold component to build the base page of the application
    /// The architecture of the page
    return Scaffold(
      appBar: AppBar(title: Text("sensors_plus")),
      body: Center(
        child: Text("Shake it."),),); }}Copy the code

And finally, a summary diagram