Home > Software engineering >  Gesture detector onTap is not triggered
Gesture detector onTap is not triggered

Time:02-17

 Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
      appBar: AppBar(
        title: Text("new screen"),
      ),
      body: Column(
        children: [
          Container(
            height: 210,
            decoration: BoxDecoration(color: Colors.white),
            child: Column(
              children: [
                Stack(
                  children: [
                    GestureDetector(
                        behavior: HitTestBehavior.translucent,
                        onTap: () {
                          Navigator.pop(context);
                        },
                        child: Icon(Icons.arrow_back)),
                    Center(
                      child: Text(
                        'set Destination',
                        style: TextStyle(fontSize: 20),
                      ),
                    ),
                  ],
                ),
                SizedBox(
                  height: 18,
                ),
                Row(
                  children: [
                    SizedBox(
                      width: 10,
                    ),
                    Expanded(
                        child: Container(
                      decoration: BoxDecoration(
                          color: Colors.grey,
                          borderRadius: BorderRadius.circular(8)),
                      child: Padding(
                        padding: EdgeInsets.all(2),
                        child: TextField(
                          decoration: InputDecoration(
                              hintText: 'Pickup Location',
                              fillColor: Colors.grey,
                              filled: true,
                              border: InputBorder.none,
                              isDense: true,
                              contentPadding:
                                  EdgeInsets.only(left: 10, top: 8, bottom: 8)),
                        ),
                      ),
                    )),
                    SizedBox(
                      width: 10,
                    )
                  ],
                ),
                SizedBox(
                  height: 10,
                ),
                Row(
                  children: [
                    SizedBox(
                      width: 10,
                    ),
                    Expanded(
                        child: Container(
                      decoration: BoxDecoration(
                          color: Colors.grey,
                          borderRadius: BorderRadius.circular(8)),
                      child: Padding(
                        padding: EdgeInsets.all(2),
                        child: TextField(
                          decoration: InputDecoration(
                              hintText: 'Where to',
                              fillColor: Colors.grey,
                              filled: true,
                              border: InputBorder.none,
                              isDense: true,
                              contentPadding:
                                  EdgeInsets.only(left: 10, top: 8, bottom: 8)),
                        ),
                      ),
                    )),
                    SizedBox(
                      width: 10,
                    )
                  ],
                )
              ],
            ),
          )
        ],
      ),
    ));
  }

CodePudding user response:

You are not pushing any route on the Navigator stack, so you are trying to pop from an empty list (ie, there is nothing inside the stack to be popped). Your gesture detector is working fine, i had tested it on dartpad but in your stack you have nothing to show after its pop.

CodePudding user response:

is the code below?

Widget build(BuildContext context) { 
return MaterialApp( 
home: Scaffold( 
        appBar: AppBar( title: Text("new screen"), ), 
body: Column( 
    children: [ 
        Container( 
            height: 210, 
            decoration: BoxDecoration(color: Colors.white), 
            child: Column( 
                children: [ 
                    Stack( 
                        children: [ 
                        GestureDetector( behavior:      HitTestBehavior.translucent, 
                            onTap: () { Navigator.pop(context); }, 

try use this : Navigator.of(context).pop();

CodePudding user response:

hello pragneshReddy & ibehavikmakwana There is nothing wrong with the code..

I have tested your code on Dartpad and it runs without error.. for Close your current screen try to use Navigator.of(context).pop(); and also.

you should wrap your app with 2 different stateless widget for getting better approaches

enter image description here

  • Related