你好,我正在尝试创建一个列表项。我已经创建了这样的布局,但是图像没有显示。
有人知道原因吗?
import 'package:flutter/material.dart';
class CommonListItem extends StatefulWidget {
@override
State<StatefulWidget> createState() => new _CommonListItemState();
}
class _CommonListItemState extends State<CommonListItem> {
@override
Widget build(BuildContext context) {
return new Scaffold(
body: Container(
child: new Column(
children: <Widget>[
Container(
height: 100.0,
width: 100.0,
child: new Image(image: AssetImage("assets/images/mountains.jpg")),
)
]
),
),
);
}
}
pubspec.yml
name: xx
description: xx
dependencies:
flutter:
sdk: flutter
cloud_firestore: 0.7.4
firebase_auth: 0.5.18
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
dev_dependencies:
flutter_test:
sdk: flutter
# For information on the generic Dart part of this file, see the
# following page: https://www.dartlang.org/tools/pub/pubspec
# The following section is specific to Flutter.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.io/assets-and-images/#resolution-aware.
# For details regarding adding assets from package dependencies, see
# https://flutter.io/assets-and-images/#from-packages
# To add custom fonts to your application, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts from package dependencies,
# see https://flutter.io/custom-fonts/#from-packages
答案 0 :(得分:1)
请注意Flutter中的标签。看看下面的链接。
答案 1 :(得分:0)
要使用资产中的图像,请执行以下操作:
Image.asset("assets/images/source.jpg")
只需确保在pubspec.yaml中也设置了资产。
答案 2 :(得分:0)
尝试在您的pubsec.yaml
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
assets:
- assets/
然后使用资产
答案 3 :(得分:0)
您从资产中引入了错误的url,如果您的文件夹名为images,则url为:images / mountains.jpg,因此您需要更改代码:
new Image(image: AssetImage("images/mountains.jpg"))
答案 4 :(得分:0)
new Image(image: AssetImage("assets/images/mountains.jpg")),
使用上面的代码,请确保您已在应用程序的根目录中创建了目录(区分大小写!),例如c:/ myapp / assets / images /
然后在pubspec文件中,您需要取消注释指向资产的那一行
# To add assets to your application, add an assets section, like this:
assets:
- assets/images/*
您不必列出文件夹中的所有项目,只需使用星号即可包含所有文件。