[英]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
.我确实想在我的底部工作表中实现一个手势检测器,它应该在调用 GestureDetector 的
onTapDown
和onTapCancel
函数时更改容器的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((){})
.我还将下面的代码放在
StatefulWidget
中,这样我就可以调用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.我希望有人能够帮助我。
Remove bool enabled = false;
删除
bool enabled = false;
from your build
method, because every time you call setState
it will have the same value.从你的
build
方法,因为每次你调用setState
时它都会有相同的值。
Change your enabled
variable to an instance field.将
enabled
的变量更改为实例字段。
class YourClass ...
bool enabled = false;
..
@override
Widget build(BuildContext context) {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.