英文:
Images not showing up in PageView with Flutter
问题
我正在创建一个PageView来显示可以在水平方向滚动的树图片。PageView还包括每棵树的标题。然而,虽然标题显示正常,但图片没有显示在屏幕上。
我有一个TreeModel
类,其中包含树的id
,title
和path
。我正在使用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<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'),
];
And here's the code for my 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'), // the background image
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), // 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!
答案1
得分: 1
您在Assets图像中传递了字符串:
您只需要传递路径,而不是字符串。您应该使用treeModelList[index].path
,而不是字符串文字"treeModelList[index].path"
。
解决方案代码:
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 "treeModelList[index].path"
.
Solution Code:
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'), // The background image
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), // 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("treeModelList[index].path"),
use this:
image: AssetImage(treeModelList[index].path.toString()),
> note: .toString() is optional and is there, just to ensure that type casting.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论