我正在尝试一个颤抖的Web项目(可能不会有所作为,但值得一提),我想添加图像,但是每次尝试都会出错,
我已将正确的资产添加到pubspec.yaml文件中,并且这些文件位于文件夹中。
我尝试重新启动我的ide,然后轻拂干净。 完全没有变化。
class _homePageState extends State<homePage> {
@override
Widget build(BuildContext context) {
var textStyle = TextStyle(
color: Colors.black,
fontSize: 30,
fontWeight: FontWeight.w100,
);
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
fontFamily: "MontSerrat"
),
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.grey[400],
title: Text("Germain Leignel",
style: TextStyle(
color: Colors.black,
fontSize: 40,
fontWeight: FontWeight.w100,
),
)
),
body:
Container(
color: Colors.grey[400],
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.asset('assets/images/first_image.jpg')
],
),
)
),
);
}
}
pubspec.yaml如下:
name: epq_webapp
description: An app built using Flutter for web
environment:
# You must be using Flutter >=1.5.0 or Dart >=2.3.0
sdk: '>=2.3.0-dev.0.1 <3.0.0'
dependencies:
flutter_web: any
flutter_web_ui: any
dev_dependencies:
build_runner: ^1.4.0
build_web_compilers: ^2.0.0
pedantic: ^1.0.0
dependency_overrides:
flutter_web:
git:
url: https://github.com/flutter/flutter_web
path: packages/flutter_web
flutter_web_ui:
git:
url: https://github.com/flutter/flutter_web
path: packages/flutter_web_ui
flutter:
fonts:
- family: MontSerrat
fonts:
- asset: assets\fonts\montserrat\Montserrat-Regular.ttf
assets:
- assets/
- assets/images/first_image.jpg
我希望我的代码显示图像,但是会收到错误消息,
无法加载资产:assets / images / first_image.jpg
答案 0 :(得分:0)
Flutter使用位于项目根目录的pubspec.yaml文件来识别应用程序所需的资产。
flutter:
assets:
- assets/my_icon.png
- assets/background.png
确保您的图片位于资产目录中
然后
Widget build(BuildContext context) {
// ...
return DecoratedBox(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/background.png'),
// ...
),
// ...
),
);
//
}
您应该渲染图像
答案 1 :(得分:0)
以此更新您的pubspec.yaml
`
fonts:
- family: MontSerrat
fonts:
- asset: assets\fonts\montserrat\Montserrat-Regular.ttf
uses-material-design: true <--- line added ---
assets:
- assets/
- assets/images/first_image.jpg
您还可以检查this可能会帮助您
答案 2 :(得分:0)
我通过将资产放在Web文件夹而不是项目的根目录下来解决了我的问题。然后使用
Image.asset(filename)
显示它们。