Flutter GestureDetector,如何实现onTap功能?

Flutter GestureDetector, how to implement onTap Function?

我想在手势检测器中实现 OnTap,我只是不知道如何...最初我在 Inkwell 中有 OnTap,但它没有用,因为它没有根据调试器,所以我想试试手势检测器是否可以提供帮助...

Widget _buildCardDeck() {
    return Container(
      child: Row(
        children: <Widget>[
          InkWell(
            child: cardDeckClosed.isNotEmpty
                ? Padding(
              padding: const EdgeInsets.all(2.0),
              child: TransformedCard(
                playingCard: cardDeckClosed.last, attachedCards: [], columnIndex: 0,
              ),
            )
                : Opacity(
                 opacity: 1.0,
                  child: Center(
                    widthFactor: 5.0,
                    heightFactor: 7.0,
                    child: GestureDetector(
                      child: Container(
                      padding: const EdgeInsets.all(5.0),
                      color: Colors.white,
                      width: 70.5,
                      height: 100.0,
                      ),
                    ),
                  ),
                  ),
            onTap: () {
              setState(() {
                if (cardDeckClosed.isEmpty) {
                  cardDeckClosed.addAll(cardDeckOpened.map((card) {
                    return card
                      ..opened = false
                      ..faceUp = false;
                  }));
                  cardDeckOpened.clear();
                } else {
                  cardDeckOpened.add(
                    cardDeckClosed.removeLast()
                      ..faceUp = true
                      ..opened = true,
                  );
                }
              });
            },
          ),

这是一个非常简单的小部件,这是一个可能对您有所帮助的入门代码:

GestureDetector(
          onTap: (){
            // this will fire when you tap
          },
          child: yourChildWidget(),
        )

试试下面的代码,在上面的代码中,你在 InkWell

中编写了 onTap:(){} 函数

使用GestureDetector

GestureDetector(
      onTap: () {
        print('Container Pressed');
      },
      child: Container(
        alignment: Alignment.center,
        color: Colors.blue,
        height: 50,
        width: 150,
        child: Text('Ontap Using GestureDetector'),
      ),
    ),

使用InkWell

InkWell(
      onTap: () {
        print('Container Pressed');
      },
      child: Container(
        alignment: Alignment.center,
        color: Colors.blue,
        height: 50,
        width: 150,
        child: Text('Ontap Using InkWell'),
      ),
    ),