Recently, there was a feature called gravity sensing. The requirement is that the picture moves in the corresponding direction according to the tilt of the screen.
So you need the gravity sensor or the gyroscope to determine the direction of the tilt, the change in the wheelbase. The original version of Flutter is well developed, but it is still difficult to find relevant information in Flutter.
Do you think bloggers can write their own? Then you are naive, predecessors plant trees, descendants enjoy the shade, the blogger found a pub can be used, a check online, can not find the use of records ah, so share it out to everyone, reduce the loss of their own development plugin.
Pub is called: Sensors
To show you the code:
void initState() {
super.initState();
/// acceleration sensor
_streamSubscriptions
.add(accelerometerEvents.listen((AccelerometerEvent event) {
setState(() {
_accelerometerValues = <double>[event.x, event.y, event.z];
});
}));
/// gyro sensor
_streamSubscriptions.add(gyroscopeEvents.listen((GyroscopeEvent event) {
setState(() {
_gyroscopeValues = <double>[event.x, event.y, event.z];
});
}));
/// The accelerator is not affected by gravity
_streamSubscriptions
.add(userAccelerometerEvents.listen((UserAccelerometerEvent event) {
setState(() {
_userAccelerometerValues = <double>[event.x, event.y, event.z];
});
}));
}
Copy the code
Can be seen from the above code here is contains the acceleration sensor, gyroscope sensor and is not affected by gravity acceleration of three types of listening to return to the x, y, z axis of information, according to these information can do a lot of functions, such as the position of the picture of the first mentioned the function, the function of xx is more treasure, will use the annual bill.
Remember to cancel the listening task after using:
void dispose() {
super.dispose();
for (StreamSubscription<dynamic> subscription in_streamSubscriptions) { subscription.cancel(); }}Copy the code
The official provided a demo, but I know you are lazy, haha, the blogger refined it and uploaded it to you. Click on the download