简体   繁体   English

Flutter GestureDetector 在按钮上不起作用

[英]Flutter GestureDetector is not working on button

I want to keep track of the time when the screen is touched last time.我想跟踪上次触摸屏幕的时间。 Touch can be slightly dragged, or tapped or any mode of touching the screen.触摸可以轻轻拖动,或轻敲或触摸屏幕的任何模式。 I could not find any solution but I am using onTap of GestureDetector that partially fulfills my requirements.我找不到任何解决方案,但我正在使用 GestureDetector 的 onTap 部分满足我的要求。 Still there is a problem, when I tap on screen it is working but it is not working on any button on the screen, not working means when the screen is tapped I am getting the time when the screen is touched but when a button is pressed, it is not getting the touching time.仍然有一个问题,当我点击屏幕时它正在工作,但它不能在屏幕上的任何按钮上工作,不工作意味着当点击屏幕时我得到了触摸屏幕但按下按钮的时间,它没有得到动人的时间。 Like in the following code has three buttons the tap on button is not working.就像在下面的代码中有三个按钮,点击按钮不起作用。 (I want to achieve any mode of touching the screen not only onTap). (我想实现任何触屏模式,不只是onTap)。 I am using this code in every page, is there any way to include it once like in main.dart that will work for all pages or screens.我在每个页面中都使用此代码,有没有办法像 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],
                         ),
                      
                      ),
                    ),

You can use RawGestureDetector and override rejectGesture.您可以使用 RawGestureDetector 并覆盖rejectGesture。 Here is working sample code. 是工作示例代码。

Your problem is that gestureDetector wont work on other button, Instead of that try making the function you are using in gestureDetector separately, and call that function on every button in your scaffold.您的问题是,gestureDetector 不能在其他按钮上工作,而不是尝试制作 function 您在gestureDetector 中单独使用,并在脚手架上的每个按钮上调用 function。

Solution is mentioned below:解决方法如下:

  @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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM