user9047282
user9047282

Reputation:

Flutter - GestureDetector onTapDown color changing

I do want to implement in my bottom sheet a gesturedetector, that should change the color of the container when it calls the onTapDown and the onTapCancel function of the GestureDetector. But the function is not changing anything. I've also put the code below inside a StatefulWidget so that I am able to call setState((){}).

This is my code:

bool enabled = false;
    return InkWell(
      child: Container(
        child: Row(
          children: <Widget>[
            Padding(
              padding: EdgeInsets.only(top: 16.0, bottom: 16.0, left: 30.0),                
            ),
            Text(
              text,
              style: TextStyle(
                color: (enabled
                  ? textColor
                  : Colors.black54
                ),
                fontWeight: FontWeight.bold
              ),
            )
          ],
        ),
        decoration: BoxDecoration(
          color: (enabled
            ? background
            : Colors.transparent
          )
        ),
      ),
      onTapDown: (TapDownDetails details){
        setState(() {
          enabled = true;
        });
      },
      onTapCancel: (){
        setState(() {
          enabled = false;
        });
      },
      onTap: (){
        String r_value;
        if(text == sheetText[0]){
          r_value = "delete";
        } else if(text == sheetText[1]){
          r_value = "edit";
        } else if(text == sheetText[2]){
          r_value = "notification";
        } else {
          return;
        }
        Navigator.pop(context, r_value);
      },
    );

I hope somebody is able to help me.

Upvotes: 6

Views: 14677

Answers (1)

diegoveloper
diegoveloper

Reputation: 103441

Remove bool enabled = false; from your build method, because every time you call setState it will have the same value.

Change your enabled variable to an instance field.

    class YourClass ...

    bool enabled = false;

    ..
    @override
      Widget build(BuildContext context) {

Upvotes: 6

Related Questions