Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter switch change value with tap instead of sliding (unable to get focus)

Tags:

flutter

dart

I have a CupertinoSwitch which works great for sliding but I would also like it to change it when I tap on it.

I tried wrapping it in an inkwell and a gesture detector and setting the value like this:

                                   GestureDetector(
                                      child: CupertinoSwitch(
                                        value: notificationsAllowed,
                                        activeColor: Colors.orange,
                                        onChanged: (notificationsSelection) {
                                          userProfile
                                              .userNotificationsPermitted =
                                              notificationsSelection;
                                        },
                                      ),

                                      onTap: (){
                                        setState(() {
                                          print ('tapped');
                                          notificationsAllowed = true;
                                        });

                                      },
                                    ),

But what I have found is that the inkwell is not registering the tap, I assume because it is not gaining focus.

I tried both inkwell and gesture but I am not sure why it is not registering the tap event.

Thanks

like image 738
Nicholas Muir Avatar asked Dec 04 '25 18:12

Nicholas Muir


1 Answers

    GestureDetector(
      child: Container(
        width: 42.0,
        height: 24.0,
        color: Colors.white,
        padding: EdgeInsets.symmetric(21.0),
        child: CupertinoSwitch(
          value: notificationsAllowed,
          activeColor: Colors.orange,
          onChanged: (notificationsSelection) {
            userProfile
                .userNotificationsPermitted =
                notificationsSelection;
          },
        ),
      ),

      onTap: (){
        setState(() {
          print ('tapped');
          notificationsAllowed = true;
        });

      },
    ),

but CupertinoSwitch onChanged method not work!

like image 68
askme Avatar answered Dec 06 '25 16:12

askme



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!