[英]Flutter GestureDetector is not working on button
我想跟踪上次触摸屏幕的时间。 触摸可以轻轻拖动,或轻敲或触摸屏幕的任何模式。 我找不到任何解决方案,但我正在使用 GestureDetector 的 onTap 部分满足我的要求。 仍然有一个问题,当我点击屏幕时它正在工作,但它不能在屏幕上的任何按钮上工作,不工作意味着当点击屏幕时我得到了触摸屏幕但按下按钮的时间,它没有得到动人的时间。 就像在下面的代码中有三个按钮,点击按钮不起作用。 (我想实现任何触屏模式,不只是onTap)。 我在每个页面中都使用此代码,有没有办法像 main.dart 一样将它包含一次,它适用于所有页面或屏幕。
int tapStart = Constants.prefsTapStart.getInt("tapStart");
@override
Widget build(BuildContext context) {
return GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
int tapTemp;
int tapEnd = DateTime.now().millisecondsSinceEpoch;
Constants.prefsTapEnd.setInt("tapEnd", tapEnd);
int idleTime = (tapEnd - tapStart) -
300000; // 5 minutes without touch in milliseconds
if (idleTime > 0) {
// print(idleTime);
int idleTime = Constants.prefsIdleTime.getInt("idleTime");
int sumIdleTime = idleTime + (tapEnd - tapStart);
Constants.prefsIdleTime.setInt("idleTime", sumIdleTime);
}
tapTemp = tapStart;
tapStart = tapEnd;
tapEnd = tapTemp;
},
child: Scaffold(
drawer: new DrawerDodeOnly(),
appBar: AppBar(
title: Text('Account'),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(12.0),
child: SingleChildScrollView(
child: Column(
children: [
SizedBox(
height: 15,
),
Container(
child: ElevatedButton(
onPressed: () {
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => new ChangePassword()));
},
child: Text(
"Change Account Password",
style: TextStyle(fontSize: 15, color: Colors.white),
),
style: ElevatedButton.styleFrom(
primary: Colors.deepPurple[800],
),
),
),
Container(
child: ElevatedButton(
onPressed: () {
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) =>
new ChangePayoutpassword()));
},
child: Text(
"Change Payout Password",
style: TextStyle(fontSize: 15, color: Colors.white),
),
style: ElevatedButton.styleFrom(
primary: Colors.deepPurple[800],
),
),
),
Container(
// color: Colors.blueAccent,
child: ElevatedButton(
onPressed: () {
Constants.prefsSaveTime.setInt("usage_time", 0);
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(builder: (context) => Login()),
(Route<dynamic> route) => false);
},
child: Text(
"Sign Out",
style: TextStyle(fontSize: 15, color: Colors.white),
),
style: ElevatedButton.styleFrom(
primary: Colors.deepPurple[800],
),
),
),
您可以使用 RawGestureDetector 并覆盖rejectGesture。 这是工作示例代码。
您的问题是,gestureDetector 不能在其他按钮上工作,而不是尝试制作 function 您在gestureDetector 中单独使用,并在脚手架上的每个按钮上调用 function。
解决方法如下:
@override
Widget build(BuildContext context) {
return Container(
child: Column(
children: <Widget>[
ElevatedButton(
onPressed: () {
checkIsTapped();
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => new ChangePayoutpassword(),
),
);
},
child: Text(
"Change Payout Password",
style: TextStyle(fontSize: 15, color: Colors.white),
),
style: ElevatedButton.styleFrom(
primary: Colors.deepPurple[800],
),
),
],
),
);
}
checkIsTapped() {
int tapTemp;
int tapEnd = DateTime.now().millisecondsSinceEpoch;
Constants.prefsTapEnd.setInt("tapEnd", tapEnd);
int idleTime =
(tapEnd - tapStart) - 300000; // 5 minutes without touch in milliseconds
if (idleTime > 0) {
// print(idleTime);
int idleTime = Constants.prefsIdleTime.getInt("idleTime");
int sumIdleTime = idleTime + (tapEnd - tapStart);
Constants.prefsIdleTime.setInt("idleTime", sumIdleTime);
}
tapTemp = tapStart;
tapStart = tapEnd;
tapEnd = tapTemp;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.