[英]Adding image to Flutter
this is my first time using Flutter and when I tried to add image it didn't quite work.这是我第一次使用 Flutter,当我尝试添加图像时,它并没有很好地工作。 First I tried adding Image to child:Container via Image.assets('assets/lpp_bezpozadine.png') - didnt work Then I tried making a separate method:首先我尝试通过 Image.assets('assets/lpp_bezpozadine.png') 将 Image 添加到 child:Container - 没有工作然后我尝试制作一个单独的方法:
class LppImage extends StatelessWidget {
@override
Widget build(BuildContext context) {
AssetImage assetImage = AssetImage('assets/lpp_bezpozadine.png');
Image image = Image(image: assetImage);
return Container(
child: image,
);
}
}
From which I tried to call children: - but it said that I can't use children??我试图从中称呼孩子:-但它说我不能使用孩子?
I have added the picture to pubspec.yaml as follow:我已将图片添加到 pubspec.yaml 如下:
assets:
- assets/lpp_bezpozadine.png
So if anyone could help me solve this, because I'm just starting at flutter.所以如果有人能帮我解决这个问题,因为我刚从 flutter 开始。 Thanks in advcance this is my code:提前感谢这是我的代码:
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
const darkBlueColor = const Color(0xff131f40);
const lightBlueColor = const Color(0xff445c9e);
return Scaffold(
body: Center(
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
darkBlueColor,
lightBlueColor,
],
stops: [0.5, 1],
begin: const FractionalOffset(0.5, 0.2),
end: const FractionalOffset(1, 1),
),
),
),
),
);
}
}
Error log when I try to add children it says that convert it to child:当我尝试添加孩子时,错误日志显示将其转换为孩子:
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
const darkBlueColor = const Color(0xff131f40);
const lightBlueColor = const Color(0xff445c9e);
return Scaffold(
body: Center(
children: <Widget>[
LppImage(),
],
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
darkBlueColor,
lightBlueColor,
],
stops: [0.5, 1],
begin: const FractionalOffset(0.5, 0.2),
end: const FractionalOffset(1, 1),
),
),
),
),
);
}
}
I don't know if flutter is able to find the image because there's no error logs, but this is the correct way to use an asset image:我不知道 flutter 是否能够找到图像,因为没有错误日志,但这是使用资产图像的正确方法:
class LppImage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
child: Image.asset('assets/lpp_bezpozadine.png'),
);
}
}
Next to that, I see you're trying to use Center
with children
.接下来,我看到您正在尝试将Center
与children
一起使用。 Center
only takes on child
. Center
只收child
。 So this is how you would use it:所以这就是你将如何使用它:
Center(
child: LppImage(),
),
If you want to use multiple children you should consider a Column
so you can do this:如果你想使用多个孩子,你应该考虑一个Column
以便你可以这样做:
Column(
children: [
Center(
child: LppImage(),
),
Container(),
],
),
Did you try a full app restart?您是否尝试过重新启动完整的应用程序? some times hot reloaded does not see assets.有时热重装看不到资产。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.