“`dart PageView 中的图像未显示出来,使用 Flutter “`

huangapple go评论63阅读模式
英文:

Images not showing up in PageView with Flutter

问题

我正在创建一个PageView来显示可以在水平方向滚动的树图片。PageView还包括每棵树的标题。然而,虽然标题显示正常,但图片没有显示在屏幕上。

我有一个TreeModel类,其中包含树的idtitlepath。我正在使用AssetImage小部件中的路径来显示图片。

这是我的TreeModel的代码:

class TreeModel {
  final int id;
  final String title;
  final String path;

  TreeModel(this.id, this.title, this.path);
}

List<TreeModel> treeModelList = [
  TreeModel(1, 'Tree 1', 'images/tree_illust.png'),
  TreeModel(2, 'Tree 2', 'images/tree_illust2.png'),
  TreeModel(3, 'Tree 3', 'images/tree_illust3.png'),
  TreeModel(4, 'Tree 4', 'images/tree_illust4.png'),
];

这是我的TreeDesignPage的代码:

import 'package:app/models/tree_model.dart';
import 'package:flutter/material.dart';

class TreeDesignPage extends StatefulWidget {
  const TreeDesignPage({Key? key}) : super(key: key);

  @override
  State<TreeDesignPage> createState() => _TreeDesignPageState();
}

class _TreeDesignPageState extends State<TreeDesignPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        constraints: const BoxConstraints.expand(),
        decoration: const BoxDecoration(
          image: DecorationImage(
            image: AssetImage('images/landscape1.jpg'), // 背景图片
            fit: BoxFit.cover,
          ),
        ),
        child: PageView.builder(
          itemCount: treeModelList.length,
          itemBuilder: (context, index) {
            return Stack(
              children: [
                InkWell(
                  onTap: () {
                    print("Clicked ${treeModelList[index].id}!");
                  },
                  child: Container(
                    decoration: BoxDecoration(
                      image: DecorationImage(
                        image: AssetImage(treeModelList[index].path), // 这里有问题?
                      ),
                    ),
                  ),
                ),
                Positioned(
                  left: 113.7,
                  top: 70,
                  child: SizedBox(
                    width: 184,
                    child: Text(
                      treeModelList[index].title,
                      textAlign: TextAlign.center,
                      style: const TextStyle(fontSize: 40),
                    ),
                  ),
                ),
              ],
            );
          },
        ),
      ),
    );
  }
}

然而,尽管使用了正确的AssetImage路径,但图片没有显示在屏幕上。

这是文件结构:

- lib/
  - models/
    - tree_model.dart
  - tree_design_page.dart
- images/
  - landscape1.jpg
  - tree_illust.png
  - tree_illust2.png
  - tree_illust3.png
  - tree_illust4.png
- pubspec.yaml

我已经包含了pubspec.yaml文件的相关部分,以确保资源正确配置。

可以有人请仔细查看我的代码并帮助我找出导致图片不显示在PageView中的问题吗?对于解决这个问题的任何见解或建议,我将不胜感激。谢谢!

英文:

I am creating a PageView to display tree images that can be scrolled horizontally. The PageView also includes the titles for each tree. However, while the titles are showing up correctly, the images are not being displayed on the screen.

I have a TreeModel class that contains id, title, and path of trees. I am using the path inside the AssetImage widget to display the images.

Here's the code for my TreeModel:

class TreeModel {
  final int id;
  final String title;
  final String path;

  TreeModel(this.id, this.title, this.path);
}

List&lt;TreeModel&gt; treeModelList = [
  TreeModel(1, &#39;Tree 1&#39;, &#39;images/tree_illust.png&#39;),
  TreeModel(2, &#39;Tree 2&#39;, &#39;images/tree_illust2.png&#39;),
  TreeModel(3, &#39;Tree 3&#39;, &#39;images/tree_illust3.png&#39;),
  TreeModel(4, &#39;Tree 4&#39;, &#39;images/tree_illust4.png&#39;),
];

And here's the code for my TreeDesignPage:

import &#39;package:app/models/tree_model.dart&#39;;
import &#39;package:flutter/material.dart&#39;;

class TreeDesignPage extends StatefulWidget {
  const TreeDesignPage({Key? key}) : super(key: key);

  @override
  State&lt;TreeDesignPage&gt; createState() =&gt; _TreeDesignPageState();
}

class _TreeDesignPageState extends State&lt;TreeDesignPage&gt; {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        constraints: const BoxConstraints.expand(),
        decoration: const BoxDecoration(
          image: DecorationImage(
            image: AssetImage(&#39;images/landscape1.jpg&#39;), // the background image
            fit: BoxFit.cover,
          ),
        ),
        child: PageView.builder(
          itemCount: treeModelList.length,
          itemBuilder: (context, index) {
            return Stack(
              children: [
                InkWell(
                  onTap: () {
                    print(&quot;Clicked ${treeModelList[index].id}!&quot;);
                  },
                  child: Container(
                    decoration: BoxDecoration(
                      image: DecorationImage(
                        image: AssetImage(treeModelList[index].path), // PROBLEM HERE?
                      ),
                    ),
                  ),
                ),
                Positioned(
                  left: 113.7,
                  top: 70,
                  child: SizedBox(
                    width: 184,
                    child: Text(
                      treeModelList[index].title,
                      textAlign: TextAlign.center,
                      style: const TextStyle(fontSize: 40),
                    ),
                  ),
                ),
              ],
            );
          },
        ),
      ),
    );
  }
}

However, despite using the correct AssetImage path, the images are not showing up on the screen.

Here's the file structure:

- lib/
  - models/
    - tree_model.dart
  - tree_design_page.dart
- images/
  - landscape1.jpg
  - tree_illust.png
  - tree_illust2.png
  - tree_illust3.png
  - tree_illust4.png
- pubspec.yaml

I have included the relevant portion of my pubspec.yaml file to ensure the assets are correctly configured.

Could someone kindly review my code and help me identify the issue that's causing the images not to show up in the PageView? Any insights or suggestions to resolve this problem would be greatly appreciated. Thank you!

“`dart
PageView 中的图像未显示出来,使用 Flutter
“`

答案1

得分: 1

您在Assets图像中传递了字符串:

您只需要传递路径,而不是字符串。您应该使用treeModelList[index].path,而不是字符串文字&quot;treeModelList[index].path&quot;

解决方案代码:

class TreeDesignPage extends StatefulWidget {
  const TreeDesignPage({
    Key? key,
  }) : super(key: key);

  @override
  State<TreeDesignPage> createState() => _TreeDesignPageState();
}

class _TreeDesignPageState extends State<TreeDesignPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        constraints: const BoxConstraints.expand(),
        decoration: const BoxDecoration(
          image: DecorationImage(
            image: AssetImage('images/landscape1.jpg'), // 背景图像
            fit: BoxFit.cover,
          ),
        ),
        child: PageView.builder(
          itemCount: treeModelList.length,
          itemBuilder: (context, index) {
            return Stack(
              children: [
                InkWell(
                  onTap: () {
                    print("Clicked ${treeModelList[index].id}!");
                  },
                  child: Container(
                    decoration: BoxDecoration(
                      image: DecorationImage(
                        image: AssetImage(treeModelList[index].path), // 更新后的图像路径
                      ),
                    ),
                  ),
                ),
                Positioned(
                  left: 113.7,
                  top: 70,
                  child: SizedBox(
                    width: 184,
                    child: Text(
                      treeModelList[index].title,
                      textAlign: TextAlign.center,
                      style: const TextStyle(fontSize: 40),
                    ),
                  ),
                ),
              ],
            );
          },
        ),
      ),
    );
  }
}

现在您将会显示图像。

如果图像没有显示,请确保在pubspec.yaml文件中添加了assets/images

英文:

You have pass String in your Assets Image:

You need to pass only the path not the path as string. You should use treeModelList[index].path instead of the string literal &quot;treeModelList[index].path&quot;.

Solution Code:

class TreeDesignPage extends StatefulWidget {
  const TreeDesignPage({
    Key? key,
  }) : super(key: key);

  @override
  State&lt;TreeDesignPage&gt; createState() =&gt; _TreeDesignPageState();
}

class _TreeDesignPageState extends State&lt;TreeDesignPage&gt; {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        constraints: const BoxConstraints.expand(),
        decoration: const BoxDecoration(
          image: DecorationImage(
            image: AssetImage(&#39;images/landscape1.jpg&#39;), // The background image
            fit: BoxFit.cover,
          ),
        ),
        child: PageView.builder(
          itemCount: treeModelList.length,
          itemBuilder: (context, index) {
            return Stack(
              children: [
                InkWell(
                  onTap: () {
                    print(&quot;Clicked ${treeModelList[index].id}!&quot;);
                  },
                  child: Container(
                    decoration: BoxDecoration(
                      image: DecorationImage(
                        image: AssetImage(treeModelList[index].path), // Updated image path
                      ),
                    ),
                  ),
                ),
                Positioned(
                  left: 113.7,
                  top: 70,
                  child: SizedBox(
                    width: 184,
                    child: Text(
                      treeModelList[index].title,
                      textAlign: TextAlign.center,
                      style: const TextStyle(fontSize: 40),
                    ),
                  ),
                ),
              ],
            );
          },
        ),
      ),
    );
  }
}

Now you will show the image.

If image is not showing. Make sure that you have added assets/images on your pubspec.ymal file.

答案2

得分: 1

使用以下代码替代:

image: AssetImage(treeModelList[index].path.toString()),

注意:.toString()是可选的,只是为了确保类型转换。

英文:

Instead of

image: AssetImage(&quot;treeModelList[index].path&quot;),

use this:

image: AssetImage(treeModelList[index].path.toString()),

> note: .toString() is optional and is there, just to ensure that type casting.

huangapple
  • 本文由 发表于 2023年7月23日 15:38:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747110.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定