Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 颤振-如何在使用手势检测器进行onTap后更改SVG_Flutter_Dart_Svg_Gesturedetector - Fatal编程技术网

Flutter 颤振-如何在使用手势检测器进行onTap后更改SVG

Flutter 颤振-如何在使用手势检测器进行onTap后更改SVG,flutter,dart,svg,gesturedetector,Flutter,Dart,Svg,Gesturedetector,想要实现与图像相同的功能: 我正在使用这个软件包: 尽管我能够在IconButton中实现这一点,但我需要在GestureDetector中实现这一点,而不是在IconButton中实现这一点,所以我要明确一点,需要帮助如何将SVG实现到GestureDetector而不是IconButton中 我能够在屏幕上呈现SVG,但不知道如何在GestureDetector中实现它,要在屏幕上呈现SVG,我是这样做的: Expanded svgTest({double blackKeyWidth})

想要实现与图像相同的功能:

我正在使用这个软件包:

尽管我能够在IconButton中实现这一点,但我需要在GestureDetector中实现这一点,而不是在IconButton中实现这一点,所以我要明确一点,需要帮助如何将SVG实现到GestureDetector而不是IconButton中

我能够在屏幕上呈现SVG,但不知道如何在GestureDetector中实现它,要在屏幕上呈现SVG,我是这样做的:

Expanded svgTest({double blackKeyWidth}) {
    return Expanded(
      child: Container(
        child: IconButton(
            icon: SvgPicture.asset(
              'assets/images/1.svg',
              fit: BoxFit.fill,
              allowDrawingOutsideViewBox: true,
              width: 500,
            ),
            onPressed: null //do something,
            ),
      ),
    );
  }
无法理解如何在手势检测器中实现SVG,它无法进入,因为图像和手势检测器不接受图标,或者我做得不对


谢谢

如果您想更改
iconButton
onTap
方法中的图像,则必须使用
setState
更改
onTap
方法中的
图像的路径

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";
                });
},
            ),
      ),
    );
  }
编辑:(对于手势检测器,您只需将SVG包装在其中即可)

iconbutton
替换为
gesturedector
,并添加其
onTap
onTapUp
方法

  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,
    ),
  ),

您想一次更改还是每次点击都要更改?每次,我都想将SVG实现到GestureDetector而不是IconButton中,我在这里发布了IconButton代码,作为我自己能够实现的示例,尽管我希望SVG每次都在OnTappdown和OnTappUpant中更改,以便在GestureDetector中实现SVG,需要它提供的额外功能,比如SVG再次改变onTapUp@Jan请检查更新的代码,并告知您在使用手势检测器时面临的问题