本文介绍了Flutter - InkWell 对灵活内部的 onTap 没有反应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚为什么 InkWell 中的 onTap() 方法不起作用.InkWell 小部件位于 Flexible 小部件内.这个 Flexible 小部件也在里面一个 Row.

I am trying to figure out, why the onTap() method inside my InkWell is not working. The InkWell widget is inside a Flexible widget. This Flexible widget is also inside a Row.

这是我的代码:

TextEditingController controller = new TextEditingController();

@override
void dispose(){
    super.dispose();
    controller.dispose();
}

@override
    Widget build(BuildContext context) {
      return Card(        
        child: Container(          
          child: Row(
            children: <Widget>[
              Flexible(                
                child: InkWell(
                  onTap: () => print("Search"), //Is not printing anything
                  child: TextField(                    
                    controller: controller,
                    decoration: InputDecoration(
                      border: InputBorder.none,
                      hintText: "Searching..."
                    ),                 
                  ),
                ),
              )
            ],
          ),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.all(
              Radius.circular(8.0)
            ),
          ),
        ),     
      );
    }

我不知道如何解决我的问题.如果有人能够解决它,那就太棒了XD.

I do not know how to solve my problem. It would be awesome, if somebody would be able to solve it XD.

推荐答案

IgnorePointer 包裹在 Inkwell 的孩子中将解决这个问题:

Wrapping IgnorePointer inside the Inkwell's child will solve this:

Widget build(BuildContext context) {
    return Card(
      child: Container(
        child: Row(
          children: <Widget>[
            Flexible(
              child: InkWell(
                onTap: () => print("Search"), //Is not printing anything
                child: IgnorePointer(
                  child: TextField(
                    controller: controller,
                    decoration: InputDecoration(
                        border: InputBorder.none,
                        hintText: "Searching..."
                    ),
                  ),
                ),
              ),
            )
          ],
        ),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.all(
              Radius.circular(8.0)
          ),
        ),
      ),
    );
  }

这篇关于Flutter - InkWell 对灵活内部的 onTap 没有反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 23:00