简体   繁体   中英

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

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

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;
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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