[英]image doesn't display on the app even though it was in assets folder
I added the image file on assets folder.我在资产文件夹中添加了图像文件。 but when I run the app, image doesn't display at all.但是当我运行应用程序时,图像根本不显示。 and there isn't any error message.并且没有任何错误信息。 I don't know the reason why I cannot see the picture I added.我不知道为什么我看不到我添加的图片。 (of course, it isn't the white picture. It isthe normal picture.) Please let me know the reason. (当然不是白图,是正常图。)请告诉我原因。
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Title'),
),
body: Center(
child: GestureDetector(
onTap: (){
Navigator.push(
context,
MaterialPageRoute(builder: (context) => HeroDetailPage()),
);
},
child: Hero(
tag: 'image',
child: Image.asset(
'assets/sample.jpg',
width: 100,
height: 100,
),
),
),
),
);
}
}
class HeroDetailPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Hero Detail'),
),
body: Hero(
tag: 'image',
child: Image.asset('assets/sample.jpg'),
),
);
}
}
To add assets to your application, add an assets section, like this in your pubspec.yaml
file:要将资产添加到您的应用程序,请在pubspec.yaml
文件中添加资产部分,如下所示:
assets:
- assets/
Please check: https://flutter.dev/docs/development/ui/assets-and-images first.请先检查: https : //flutter.dev/docs/development/ui/assets-and-images 。
There you can find this:在那里你可以找到这个:
Flutter uses the pubspec.yaml file, located at the root of your project, to identify assets required by an app. Flutter 使用位于项目根目录的 pubspec.yaml 文件来识别应用程序所需的资产。
Here is an example:下面是一个例子:
flutter:
assets:
- assets/my_icon.png
- assets/background.png
To include all assets under a directory, specify the directory name with the / character at the end:要包含目录下的所有资产,请在结尾处使用 / 字符指定目录名称:
flutter:
assets:
- directory/
- directory/subdirectory/
First add your image file in pubspec.yml file首先在pubspec.yml文件中添加您的图像文件
assets:
- assets/sample.jpg
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.