簡體   English   中英

Flutter GestureDetector 在按鈕上不起作用

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM