这是我的pubspec.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
# To add assets to your application, add an assets section, like this:
assets:
- images/img_rectangle1.jpg我已经建立了一个资产文件夹,并将其包含在my.png图片中;但是,我在使用这些小部件时遇到了困难。
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Korean vocabulary related to traits"),
centerTitle: false,
backgroundColor: const Color(0xFFEC74C8),
),
body: Row(
children: [
Expanded(
child: ListView.builder(
itemCount: koreanNameList.length,
itemBuilder: (context, index) {
KoreanItem item = koreanNameList[index];
return Draggable<KoreanItem>(
data: item,
dragAnchorStrategy: pointerDragAnchorStrategy,
feedback: KoreanNameCard(item: item),
child: KoreanNameCard(item: item));
},
)),
Expanded(
child: ListView.builder(
itemCount: englishanswers.length,
itemBuilder: (context, index) {
return buildEnglishanswerColumn(englishanswers[index]);
},
)),
],
),
);
}那么,放背景图片的容器到底应该放在哪里呢?
我试过用
AssetImage但它并没有显示在我的应用程序上。
发布于 2022-11-22 16:35:47
您需要在pubspec.yaml上指定 assets
Flutter使用位于项目根目录的pubspec.yaml文件来标识应用程序所需的资产。
下面是一个示例:
颤振:资产:-资产/图像/背景。
若要包含目录下的所有资产,请指定目录名,其末尾为/字符:
颤振:资产:-资产/-资产/图像/
使用图像资产的样本
Container(
height: double.infinity,
width: double.infinity,
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/splash/background.png"),
fit: BoxFit.cover,
),
),
child: const Center(
child: Text(
'Hello World!',
style: TextStyle(color: Colors.white),
),
),
);参考:添加资产和图像
发布于 2022-11-22 15:57:22
要将图像设置为背景,请使用此容器小部件作为Scaffold的主体。
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/images/image.jpg"),
fit: BoxFit.cover,
),
),
child: child
)将image.jpg替换为您的图像名称。
https://stackoverflow.com/questions/74535370
复制相似问题