If you want to change the image on the onTap
method of the iconButton
then you should have to change the path of the image
in onTap
method using setState
String imagePath = "assets/images/1.svg";
Expanded svgTest({double blackKeyWidth}) {
return Expanded(
child: Container(
child: IconButton(
icon: SvgPicture.asset(
'${imagePath}',
fit: BoxFit.fill,
allowDrawingOutsideViewBox: true,
width: 500,
),
onPressed: (){
setState(() {
imagePath = "assets/images/2.svg";
});
},
),
),
);
}
EDITED: (For Gesture Detector You Can Simply Wrap the SVG in it)
Replace the iconbutton
with the GestureDetector
and Add its onTap
and onTapUp
methods too.
GestureDetector(
onTap: () {
setState(() {
imagePath = "assets/images/2.svg";
});
},
onTapUp: (TapUpDetails tapUpDetails) {
setState(() {
imagePath = "assets/images/1.svg";
});
},
child: SvgPicture.asset(
'${imagePath}',
//fit: BoxFit.fill,
//allowDrawingOutsideViewBox: true,
//width: 500,
),
),
For More About GestureDetector and Flutter SVG Click Here
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…