flutter - 在颤动中传递资产图像时显示错误
问题描述
我只是一个颤抖的初学者。我正在尝试将资产图像传递给其他类,但出现错误。这是我的代码
main.dart 文件
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Home'),
),
body: ListView(
children: <Widget>[
ListInfo(photo:"assets\images\apple.jpg",name:"Apple");
],
),
),
);
}
}
列表信息类
class ListInfo
{
final String photo;
final String name;
ListInfo({this.photo, this.name});
}
列表图像类
class ListImages extends StatelessWidget {
@override
ListInfo info = new ListInfo();
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(20.0),
child: Center(
child: Column(
children: <Widget>[
Image(
image: AssetImage(info.photo),
),
Text(
info.name,
style: TextStyle(fontSize: 30.0),
),
],
),
),
);
}
}
抱歉问了这么一个基本的问题。但我无法解决这个问题
解决方案
添加资产pubspec.yaml
# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
assets:
-assets\images\apple.jpg
-assets\images\orange.jpg
然后更新pub get
用于Image.asset('assets\images\apple.jpg')
将图像作为小部件添加到列表中
return Container(
padding: EdgeInsets.all(20.0),
child: Center(
child: Column(
children: <Widget>[
Image.asset('assets\images\apple.jpg',),
Text(
info.name,
style: TextStyle(fontSize: 30.0),
),
Image.asset('assets\images\orange.jpg',),
Text(
info.name,
style: TextStyle(fontSize: 30.0),
),
],
),
),
);
推荐阅读
- node.js - [Passportjs][Angular5] 请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin 'null' 不允许访问
- java - 对于每个带有基元的循环修改
- android - Firebase 实时数据库未显示在控制台中
- security - 如何避免在 COBOL 程序中硬编码凭据?
- c# - 三边测量 C# 如何回到“正常”坐标?
- php - php:通过另一个类的方法来获取一个类的名字
- google-schemas - Google Mail 标记中的时区
- java - Big-O 表示法和循环递归
- angular - 一般错误:未加载运行时编译器,
- javascript - Angular 4 - 从子组件发出的停止功能