Dart 如何将背景图像添加到Flatter应用程序?
我正在尝试为我的Flitter应用程序添加一个背景图像,我已经完成了所有类似的问题。应用程序m运行正常,但图像未显示 以下是我的小部件代码:Dart 如何将背景图像添加到Flatter应用程序?,dart,flutter,Dart,Flutter,我正在尝试为我的Flitter应用程序添加一个背景图像,我已经完成了所有类似的问题。应用程序m运行正常,但图像未显示 以下是我的小部件代码: @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( // Here we take the value from the MyHomePage obje
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
actions: <Widget>[
new IconButton(icon: const Icon(Icons.list), onPressed: _loadWeb)
],
),
body: new Stack(
children: <Widget>[
Container(
child: new DecoratedBox(
decoration: new BoxDecoration(
image: new DecorationImage(
image: new AssetImage("images/logo.png"),
fit: BoxFit.fill,
),
),
),
),
Center(
child: _authList(),
)
],
),
floatingActionButton: FloatingActionButton(
onPressed: getFile,
tooltip: 'Select file',
child: Icon(Icons.sd_storage),
), // This trailing comma makes auto-formatting nicer for build methods.
));
}
@覆盖
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
appBar:appBar(
//在这里,我们从MyHomePage对象中获取由创建的值
//使用App.build方法,并使用它设置appbar标题。
标题:文本(widget.title),
行动:[
新图标按钮(图标:const图标(Icons.list),按下时:_loadWeb)
],
),
正文:新堆栈(
儿童:[
容器(
孩子:新装饰的盒子(
装饰:新盒子装饰(
图片:新装饰图片(
图像:新资产图像(“images/logo.png”),
fit:BoxFit.fill,
),
),
),
),
居中(
子项:_authList(),
)
],
),
浮动操作按钮:浮动操作按钮(
onPressed:getFile,
工具提示:“选择文件”,
子:图标(Icons.sd_存储),
),//此尾随逗号使生成方法的自动格式设置更方便。
));
}
应用程序运行良好,堆栈上的第二个小部件(listView)正常工作,但图像不显示
有什么想法吗?使用
BoxDecoration
作为Container
的decoration
属性:
Container(
decoration: new BoxDecoration(
image: new DecorationImage(
image: new AssetImage("images/logo.png"),
fit: BoxFit.fill,
),
),
),
Scaffold
不支持任何背景图像的概念。您可以做的是为脚手架
提供一种透明的颜色,并将其放入容器
中,然后使用装饰
属性拉入所需的背景图像。应用程序栏也是透明的
Widget build(BuildContext context) {
return MaterialApp(
title: 'Welcome to Flutter',
home: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("images/logo.png"), fit: BoxFit.cover)),
child: Scaffold(
backgroundColor: Colors.transparent,
appBar: AppBar(
elevation: 0,
backgroundColor: Colors.transparent,
title: Text('My App'),
centerTitle: true,
leading: IconButton(
icon: Icon(
Icons.list,
color: Colors.white,
),
onPressed: () {}),
),
),
),
);
}
使用此选项,它将不会具有透明背景的黑色表示。使用
装饰框
:
@覆盖
小部件构建(构建上下文){
返回装饰盒(
装饰:盒子装饰(
图像:装饰图像(
图片:AssetImage(“您的_资产”),
适合:BoxFit.cover,
),
),
儿童://。。。,
);
}
谢谢!效果很好。我还为容器添加了颜色,以避免我的图像被放置在黑色背景上。来吧,堆栈溢出!请考虑格式化您的响应,并添加解释,以便观众可以理解您的代码所做的操作,以及它如何解决问题中提出的问题。请编辑您的答案并提供简要说明,因为StackOverflow会将没有简要说明的答案放在低质量帖子审阅队列中,以查看是否应删除这些答案。。。原因是成千上万的人匆忙地浏览答案,寻找最能解决问题的答案,如果他们不得不花大量时间研究答案以确定答案是否是他们真正需要的答案,并且必须阅读问题,这将浪费很多人的时间,对您的答案进行反向工程,并可能阅读语言文档,以了解您的修复程序解决了什么问题或它是如何工作的。
return MaterialApp(
title: "MoonLight",
home: Container(
decoration:new BoxDecoration(
image: new DecorationImage(
image: new AssetImage("graphics/moon.jpg"),
fit: BoxFit.cover,)
),
child: Scaffold(
backgroundColor: Colors.transparent,
),
),
),
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Container(
decoration: BoxDecoration(
color: Colors.white,
image: DecorationImage(
image: AssetImage("asset/images/image.png"),
fit: BoxFit.cover)),
child: Scaffold(
backgroundColor: Colors.transparent,
),
);
}