当前位置:网站首页>flutter 实现摇一摇功能
flutter 实现摇一摇功能
2022-06-28 07:19:00 【氤氲息】
sensors_plus: ^1.3.2
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:sensors_plus/sensors_plus.dart';
import 'package:social_im/common/colors.dart';
//摇一摇小游戏
class ShakeGame extends StatefulWidget {
const ShakeGame({
Key? key}) : super(key: key);
@override
State<StatefulWidget> createState() => ShakeGameState();
}
class ShakeGameState extends State<ShakeGame> {
//是否已经显示摇一摇弹窗
bool isShow = false;
final _streamSubscriptions = <StreamSubscription<dynamic>>[];
@override
void initState() {
//监听手机摇动
shakeListen();
super.initState();
}
@override
void dispose() {
super.dispose();
//取消监听
for (final subscription in _streamSubscriptions) {
subscription.cancel();
}
}
shakeListen() {
_streamSubscriptions
.add(userAccelerometerEvents.listen((UserAccelerometerEvent event) {
//不受重力的影响
print("event的值${event}");
int value = 4;
if (event.x >= value ||
event.x <= -value ||
event.y >= value ||
event.y <= -value ||
event.z >= value ||
event.z <= -value) {
print("已摇了");
if (isShow == false) {
setState(() {
isShow = true;
});
//延时两秒匹配到用户
Future.delayed(const Duration(seconds: 2), () {
print("跳出弹窗");
});
}
}
}));
}
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
appBar: AppBar(
title: const Text('摇一摇'),
backgroundColor: CommonColors.getThemeColor(),
),
body: Center(
child: Padding(
padding: const EdgeInsets.only(left: 5),
child: Image(
image: isShow
? const AssetImage('assets/images/icon_lan_match.png')
: const AssetImage(
'assets/images/conversion/icon_conversion_album.png'),
width: 6,
height: 11))),
);
}
}
边栏推荐
猜你喜欢

ABAP 技能树

Design and practice of vivo sensitive word matching system

Techo day Tencent technology open day, June 28 online waiting for you!

hack the box:RouterSpace题解

SQL statement optimization steps (1)

Leetcode+ 66 - 70 high precision, two sub topics
![[C language] detailed explanation of C language to obtain array length](/img/cf/75c314bb622b8a1745f43cc07cb02e.png)
[C language] detailed explanation of C language to obtain array length

LeetCode+ 66 - 70 高精度、二分专题

云原生(待更新)

C language tutorial
随机推荐
Huawei cloud computing physical node cna installation tutorial
炒股开户在手机上安全吗?
MySQL installation steps - installing MySQL on Linux (3)
Wechat applets - basics takes you to understand the life cycle of applets (I)
Comprehensive analysis of real enterprise software testing process
An important term in MySQL -- CRUD
Can okcc call centers work without computers?
Is it safe to open an account on Dongfang fortune
在idea中,get和set方法爆红可能是没有安装Lombok插件
最后的二十九天
【C语言】详解 C 语言获取数组长度
华为云计算之物理节点CNA安装教程
Techo Day 腾讯技术开放日,6月28日线上等你!
Comment la passerelle BACnet / IP recueille - t - elle les données du système central de contrôle des bâtiments?
同花顺网上开户安全吗
代码提交规范
MySQL installation steps - Linux configuration file JDK installation (II)
Pytorch RNN learning notes
Practice and exploration of vivo live broadcast application technology
Solving the longest palindrome substring by dynamic programming