Search code examples
fluttergesturedetector

how to put GestureDetector inside Container alert


I wanna put GestureDetector with container alert but it show error. anyone know how to make this code works? Here the code below which i try to put GestureDetector for the alert container.

Without GestureDetector it works fine but i wanna make whole screen touch able to return to other page.

 showPopup(BuildContext context) {
    // set up the buttons
    // ignore: deprecated_member_use

    // set up the AlertDialog
    GestureDetector(
       Container alert = Container(
        child: Stack(
      children:  <Widget>[
        if (controllers!.isNotEmpty)
          CarouselSlide2(
            controllers: controllers!,
          ),


        Padding(
          padding: const EdgeInsets.only(top:688.0,left: 90),
          child: GestureDetector(
            onTap: () async {
              isPop = false;
              Navigator.pop(context);

              _checkTimer();
            },
            // child: Icon(Icons.arrow_back,color: Colors.white,size: 100,),
            child: DefaultTextStyle(
              style: TextStyle(color: Colors.white,fontSize: 30),
              child:   Text("Tap to return",),
                 )




          ),
        )
      ],
    )));
    // show the dialog
    showDialog(
      barrierDismissible: true,
      context: context,
      builder: (BuildContext context) {
        return WillPopScope(
            onWillPop: () async {
              const shouldPop = true;
              isPop = false;
              Navigator.pop(context);

              _checkTimer();
              return shouldPop;
            },
            child: alert);
      },
    );
  }

Solution

  • You are using widget in a wrong way, try this:

    Widget alert = GestureDetector(
            onTap: () {
              print("tap");
            },
            child: Container(
                child: Stack(
              children: <Widget>[
                if (controllers!.isNotEmpty)
                  CarouselSlide2(
                    controllers: controllers!,
                  ),
                Padding(
                  padding: const EdgeInsets.only(top: 688.0, left: 90),
                  child: GestureDetector(
                      onTap: () async {
                        isPop = false;
                        Navigator.pop(context);
    
                        _checkTimer();
                      },
                      // child: Icon(Icons.arrow_back,color: Colors.white,size: 100,),
                      child: DefaultTextStyle(
                        style: TextStyle(color: Colors.white, fontSize: 30),
                        child: Text(
                          "Tap to return",
                        ),
                      )),
                )
              ],
            )),
          )