【问题标题】:Flutter GestureDetector is not working on buttonFlutter GestureDetector 在按钮上不起作用
【发布时间】:2021-04-03 01:55:08
【问题描述】:

我想记录上次触摸屏幕的时间。触摸可以轻轻拖动,或轻敲或触摸屏幕的任何模式。我找不到任何解决方案,但我正在使用 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],
                         ),
                      
                      ),
                    ),

【问题讨论】:

标签: flutter gesturedetector


【解决方案1】:

您可以使用 RawGestureDetector 并覆盖rejectGesture。 Here 正在运行示例代码。

【讨论】:

    【解决方案2】:

    您的问题是,gestureDetector 不能在其他按钮上工作,而不是尝试单独在gestureDetector 中使用您正在使用的函数,并在脚手架中的每个按钮上调用该函数。

    解决方法如下:

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

    【讨论】:

      猜你喜欢
      • 2023-01-25
      • 1970-01-01
      • 2020-08-17
      • 2020-04-02
      • 1970-01-01
      • 2020-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多