【发布时间】:2023-03-11 07:50:01
【问题描述】:
是否可以在 Scaffold 的 AppBar 中添加背景图片?我知道 sliver,但是当您向下滚动时,图像会被隐藏并且 AppBar 会改变颜色,对吗?所以我想知道这是否可能,如果没有,是否有任何现有的解决方法?谢谢!
【问题讨论】:
是否可以在 Scaffold 的 AppBar 中添加背景图片?我知道 sliver,但是当您向下滚动时,图像会被隐藏并且 AppBar 会改变颜色,对吗?所以我想知道这是否可能,如果没有,是否有任何现有的解决方法?谢谢!
【问题讨论】:
appBar: AppBar(
title: Text('Current deals'),
flexibleSpace: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('images/nav-header.png'),
fit: BoxFit.cover,
),
),
),
),
【讨论】:
我在使用 iOS 和 Hafiz Nordin 的回答时遇到了一些问题。在 iOS 上,图像不会覆盖整个应用栏,留下一个小的透明空间。
我的解决方案是改用带有DecorationImage 的容器。
AppBar(
flexibleSpace: Container(
decoration:
BoxDecoration(
image: DecorationImage(
image: AssetImage(),
fit: BoxFit.cover,
),
),
),
backgroundColor: Colors.transparent,
title: Text("App Bar"),
);
【讨论】:
与其使用Zulfiqar did 之类的Stack 小部件,不如在AppBar 小部件的flexibleSpace 参数中传递您的背景图像:
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('App Bar!'),
flexibleSpace: Image(
image: AssetImage('assets/image.png'),
fit: BoxFit.cover,
),
backgroundColor: Colors.transparent,
),
body: Container(),
);
}
【讨论】:
AppBar 与您的图像一样大,它会剪切图像。
Widget build(BuildContext context) {
return new Container(
child: new Stack(children: <Widget>[
new Container(
child: new Image.asset('assets/appimage.jpg'),
color: Colors.lightGreen,
),
new Scaffold(
appBar: new AppBar(title: new Text('Hello'),
backgroundColor: Colors.transparent,
elevation: 0.0,
),
backgroundColor: Colors.transparent,
body: new Container(
color: Colors.white,
child: new Center(
child: new Text('Hello how are you?'),),)
)
],),
);
}
【讨论】: