【问题标题】:How to disable onTap function when a single click has been clicked. Using flutter单击单击时如何禁用 onTap 功能。使用颤振
【发布时间】:2022-01-23 08:47:10
【问题描述】:

单击单击时如何禁用 onTap 功能。使用颤振。

这是我下面的代码,请帮我看看...

class VoteCalonUmumPage extends StatelessWidget {
  const VoteCalonUmumPage({Key? key, required this.title}) : super(key: key);
  
  final String title;

  Widget _buildListItem(BuildContext context, DocumentSnapshot document) {
    return ListTile(
      tileColor: Color(0xff99c2ec),
      title: Row(
        children: [
          Expanded(
            child: Text(document['name'],
                style: TextStyle(
                  color: Colors.black87,
                  fontSize: 20,
                )),
          ),
          Container(
            decoration: const BoxDecoration(
              color: Color(0xffecc399),
            ),
            padding: const EdgeInsets.all(10.0),
            child: Text(
              document['votes'].toString(),
              style: Theme.of(context).textTheme.headline4,
            ),
          ),
        ],
      ),
      onTap: () {
        FirebaseFirestore.instance.runTransaction((transaction) async {
          DocumentSnapshot freshSnap =
              await transaction.get(document.reference);
          await transaction.update(freshSnap.reference, {
            'votes': freshSnap['votes'] + 1,
          });
        });
      },
    );
  }
}

【问题讨论】:

    标签: firebase flutter dart vote-up-buttons


    【解决方案1】:

    您可以设置如下条件:-

    设置一个布尔变量并将其设置为 true,如果您想永久禁用使用偏好,则当用户点击按钮时将其设置为 false

    bool isClicked = true;

    GestureDetector(
    onTap:  (){
          if(isClicked){
            isClicked = true;
        enter code here
           }
    
       } 
      child: Container(),
    )
    

    【讨论】:

    • 你可以使用设置状态
    【解决方案2】:

    检查下面的代码一个简单的逻辑它可能对你有帮助,

    bool isLoading = false; //global variable
    
    onTap: () {
    if(!isLoading)
      {
    
        isLoading = true;
        try{
          FirebaseFirestore.instance.runTransaction((transaction) async {
            DocumentSnapshot freshSnap =  await transaction.get(document.reference);
            await transaction.update(freshSnap.reference, {'votes': freshSnap['votes'] + 1,});
            isLoading = false;
          });
        }catch((e){
          isLoading = false
        });
      }
    },
    

    【讨论】:

    • 如果问题的作者不想使用有状态类,你可以选择一个带有isLoading 变量的单例控制器类。
    • 如果使用无状态类,此代码对我不起作用
    【解决方案3】:

    为了真正禁用onTap 处理程序,您必须将null 传递给onTap。我会在这个类中创建一个变量来跟踪onTap 是否已经被按下,如果已经按下,则将null 传递给onTap 而不是你的回调函数。

    onTap: onTapPressed ? null : () {
      setState(() {
        // call set state here so that the UI will be updated.
        onTapPressed = true;
      });
      FirebaseFirestore.instance.runTransaction((transaction) async {
        DocumentSnapshot freshSnap =
        await transaction.get(document.reference);
        await transaction.update(freshSnap.reference, {
          'votes': freshSnap['votes'] + 1,
        });
      });
    },
    

    然后在您的小部件中添加此成员。

    bool onTapPressed = false;
    

    此外,ListTile 也有一个名为 enabled 的可选参数,您可以将其设置为 false,而不是将 null 传递给 onTap。这种方法将禁用ListTile 上的所有处理程序,而不仅仅是onTap(例如,您可能还有一个onLongPress 处理程序)。它还将更新样式以使用当前Theme 中的disabled 颜色。

    disabled: !onTapPressed,
    onTap: () {
      setState(() {
        // call set state here so that the UI will be updated.
        onTapPressed = true;
      });
      FirebaseFirestore.instance.runTransaction((transaction) async {
        DocumentSnapshot freshSnap =
        await transaction.get(document.reference);
        await transaction.update(freshSnap.reference, {
          'votes': freshSnap['votes'] + 1,
        });
      });
    },
    

    【讨论】:

      【解决方案4】:

      请参考以下代码

      IgnorePointer 是 Fl​​utter 中的内置小部件,类似于 AbsorbPointer 小部件,它们都可以防止其子小部件发生点击、单击、拖动、滚动和悬停的指针事件。IgnorePointer 小部件只是忽略指针事件而不终止它,这意味着如果 IgnorePointer 小部件树下有任何其他元素,那么它将能够体验该指针事件。

      bool disableOnClick = false;
      
      IgnorePointer(
                ignoring: disableOnClick ?? false,
                child: ListTile(
                  tileColor: Color(0xff99c2ec),
                  title: Row(
                    children: [
                      Expanded(
                        child: Text(document['name'],
                            style: TextStyle(
                              color: Colors.black87,
                              fontSize: 20,
                            )),
                      ),
                      Container(
                        decoration: const BoxDecoration(
                          color: Color(0xffecc399),
                        ),
                        padding: const EdgeInsets.all(10.0),
                        child: Text(
                          document['votes'].toString(),
                          style: Theme.of(context).textTheme.headline4,
                        ),
                      ),
                    ],
                  ),
                  onTap: () {
                    FirebaseFirestore.instance.runTransaction((transaction) async {
                      DocumentSnapshot freshSnap =
                          await transaction.get(document.reference);
                      await transaction.update(freshSnap.reference, {
                        'votes': freshSnap['votes'] + 1,
                      });
                    });
                    disableOnClick = true;
                    setState(() {});
                  },
                ),
              )
      
      
      

      【讨论】:

        猜你喜欢
        • 2022-08-09
        • 2020-10-08
        • 2020-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多