Flutter 向颤振中的容器添加5次敲击事件
我想在容器中添加5次点击事件。我正在使用手势检测器获取tap事件。但我不知道如何实现5次点击事件在它。我想当用户点击容器5次,然后我想在我的应用程序中执行一些操作 您可以很容易地使用一段状态来实现这一点,在下面的示例中,我使用了一个Flutter 向颤振中的容器添加5次敲击事件,flutter,flutter-layout,Flutter,Flutter Layout,我想在容器中添加5次点击事件。我正在使用手势检测器获取tap事件。但我不知道如何实现5次点击事件在它。我想当用户点击容器5次,然后我想在我的应用程序中执行一些操作 您可以很容易地使用一段状态来实现这一点,在下面的示例中,我使用了一个不透明度小部件来显示一个图标,如果\u counter==5,这可能不是您想要的,但根据您问题的上下文,这是我可以给出的 类MyApp扩展StatefulWidget{ @凌驾 _MyAppState createState()=>\u MyAppState(); }
不透明度
小部件来显示一个图标,如果\u counter==5
,这可能不是您想要的,但根据您问题的上下文,这是我可以给出的
类MyApp扩展StatefulWidget{
@凌驾
_MyAppState createState()=>\u MyAppState();
}
类MyAppState扩展了状态{
int _计数器=0;
@凌驾
小部件构建(构建上下文){
返回手势检测器(
onTap:()=>setState(()=>U计数器++),
子:容器(
子:列(
儿童:[
文本(“点击我”),
不透明(
不透明度:_计数器==5?1:0,
子:图标(
什么
),
)
],
)),
);
}
}
我通过以下步骤实现
GestureDetector(
child: Image.asset(
ImageLogo,
width: MediaQuery.of(context).size.width * 0.30,
fit: BoxFit.fitWidth,
),
onTap: () {
tapCounter = 0;
oldTimestamp = 0;
},
onDoubleTap: () {
int currentTimestamp = DateTime.now().millisecondsSinceEpoch;
print('currentTimestamp $currentTimestamp');
print('oldTimestamp $oldTimestamp');
print('tapCounter $tapCounter');
if (tapCounter == 0) {
oldTimestamp = 0;
}
if (oldTimestamp == 0 ||
currentTimestamp - oldTimestamp < 450) {
tapCounter += 2;
oldTimestamp = currentTimestamp;
if (tapCounter == 6) {
tapCounter = 0;
oldTimestamp = 0;
print('Do some operation');
}
} else {
tapCounter = 0;
}
},
)
手势检测器(
子:Image.asset(
ImageLogo,
宽度:MediaQuery.of(context).size.width*0.30,
适合:BoxFit.fitWidth,
),
onTap:(){
tapCounter=0;
oldTimestamp=0;
},
onDoubleTap:(){
int currentTimestamp=DateTime.now().millissecondssinceepoch;
打印('currentTimestamp$currentTimestamp');
打印('oldTimestamp$oldTimestamp');
打印(“tapCounter$tapCounter”);
如果(tapCounter==0){
oldTimestamp=0;
}
如果(oldTimestamp==0||
currentTimestamp-oldTimestamp<450){
tapCounter+=2;
oldTimestamp=当前时间戳;
如果(tapCounter==6){
tapCounter=0;
oldTimestamp=0;
打印(“做一些操作”);
}
}否则{
tapCounter=0;
}
},
)
我正在存储当前时间并比较旧时间和当前时间,这是因为当用户停止点击容器时,它会将计数器重置为0。创建一个状态,并在每个选项卡上递增它。