Flutter第4个变量/点击在else if语句中不起作用

ffscu2ro  于 2023-01-02  发布在  Flutter
关注(0)|答案(2)|浏览(149)

在FLUTTER中,我尝试刷新屏幕4次。我有4个变量1个升高按钮和if语句。它正在更改imagePath1、imagePath2、imagePath3变量的图像,但不适用于imagePath4变量。
这是我的变量。

String imagepath1 = 'images/flame-833.png';
String imagepath2 = 'images/flame-859.png';
String imagepath3 = 'images/flame-891.png';
String imagepath4 = 'images/flame-4.png';
String currentPath = imagePath1;

下面是我的ElevatedButton,其中包含if语句和Image小部件。

ElevatedButton(
        onPressed: () {
          setState(() {
            if (currentPath == imagePath1) {
              currentPath = imagePath2;
            } else if (currentPath == imagepath2) {
              currentPath = imagepath3;
            } else if (currentPath == imagepath3) {
              currentPath = imagepath4;
            } else {
              currentPath = imagepath1;
            }
          });
        },
        child: const Text('Add Image'),
      ),
      Center(child: Image.asset(currentPath)),

1.一旦我转到这个页面,我得到imagePath1图片。
1.一旦我单击添加图像,我得到imagePath2图片。
1.一旦我点击添加图像第二次我得到imagePath3图片。
1.一旦我点击添加图像第三十三次,我没有得到任何图片。没有屏幕变化。

gblwokeq

gblwokeq1#

您的代码可以完美地工作,唯一的问题是变量必须在Widget构建之外示例化

class _TesteScreenState extends State<TesteScreen> {
  String imagepath1 = 'images/flame-833.png';
  String imagepath2 = 'images/flame-859.png';
  String imagepath3 = 'images/flame-891.png';
  String imagepath4 = 'images/flame-4.png';
  String currentPath = 'images/flame-833.png';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          ElevatedButton(
            onPressed: () {
              setState(() {
                if (currentPath == imagepath1) {
                  currentPath = imagepath2;
                } else if (currentPath == imagepath2) {
                  currentPath = imagepath3;
                } else if (currentPath == imagepath3) {
                  currentPath = imagepath4;
                } else {
                  currentPath = imagepath1;
                }
              });
            },
            child: const Text('Add Image'),
          ),
          Center(child: Image.asset(currentPath)),
        ],
      ),
    );
  }
}
ny6fqffe

ny6fqffe2#

最后我找到了解决方案。唯一的问题是变量名的大小写。但我不知道为什么flutter没有识别出这个错误。
下面是正确的变量名称代码:

String imagePath1 = 'images/flame-833.png';
String imagePath2 = 'images/flame-859.png';
String imagePath3 = 'images/flame-891.png';
String imagePath4 = 'images/flame-4.png';
String currentPath = imagePath1;

下面是小部件代码:

ElevatedButton(
        onPressed: () {
          setState(() {
            if (currentPath == imagePath1) {
              currentPath = imagePath2;
            } else if (currentPath == imagePath2) {
              currentPath = imagePath3;
            } else if (currentPath == imagePath3) {
              currentPath = imagePath4;
            } else {
              currentPath = imagePath1;
            }
          });
        },
        child: const Text('Add Image'),
      ),
      Center(child: Image.asset(currentPath)),

相关问题