我在flutter应用程序中添加了appBar。我的屏幕已经有一个背景图像,我不想设置appBar颜色或不想为appBar设置单独的背景图像。
我也想向appBar显示相同的屏幕背景图像。
我已经尝试过将appBar颜色设置为透明,但是它显示的颜色像灰色。
我的代码:
appBar: new AppBar(
centerTitle: true,
// backgroundColor: Color(0xFF0077ED),
elevation: 0.0,
title: new Text(
"DASHBOARD",
style: const TextStyle(
color: const Color(0xffffffff),
fontWeight: FontWeight.w500,
fontFamily: "Roboto",
fontStyle: FontStyle.normal,
fontSize: 19.0
)),
)
答案 0 :(得分:39)
您可以使用Scaffold的属性“ extendBodyBehindAppBar : true ” 不要忘记用SafeArea包裹孩子
@Override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
widget.title,
style: TextStyle(color: Colors.black),
),
backgroundColor: Colors.transparent,
elevation: 0.0,
),
extendBodyBehindAppBar: true,
body: Container(
width: double.infinity,
height: double.infinity,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/background/home.png'),
fit: BoxFit.cover,
),
),
child: SafeArea(
child: Center(
child: Container(
width: 300,
height: 300,
decoration: BoxDecoration(
color: Colors.green,
),
child: Center(child: Text('Test')),
),
)),
),
);
}
答案 1 :(得分:5)
这就是我所做的,并且一直在起作用
现在Scaffold支持此功能(稳定-v1.12.13 + hotfix.5)。
将脚手架extendBodyBehindAppBar设置为true, 将AppBar高程设置为0以消除阴影, 根据需要设置AppBar backgroundColor透明度。
最好的问候
答案 2 :(得分:4)
这些似乎都不适合我,我的想法是这样的:
//this.userPaymentLookupList has the drop down values.
HomeFormBean homeFormBean = new HomeFormBean();
homeFormBean.setPaymentServiceTypeList(this.userPaymentLookupList);
model.addAttribute("homeFormBean", homeFormBean);
答案 3 :(得分:3)
您可以使用Stack小部件来这样做。请遵循以下示例。
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context)
{
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
),
home: new Home(),
builder: (BuildContext context,Widget child){
return Padding(
child: child,
padding: EdgeInsets.only(bottom: 50.0),
);
},
);
}
}
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
return Stack(
children: <Widget>[
new Container(
height: double.infinity,
width: double.infinity,
decoration:new BoxDecoration(
image: new DecorationImage(
image: new AssetImage("images/jocker.jpg"),
fit: BoxFit.cover,
),
),
),
Scaffold(
backgroundColor: Colors.transparent,
appBar: new AppBar(
title: new Text("csd"),
backgroundColor: Colors.transparent,
elevation: 0.0,
),
body: new Container(
color: Colors.transparent,
),
),
],
);
}
}
答案 4 :(得分:2)
输出:
答案很多,但没人能解释extendBodyBehindAppBar
为何起作用?
之所以起作用,是因为当我们将extendBodyBehindAppBar
设置为true时,小部件的主体的高度为AppBar
,并且我们看到了覆盖AppBar
区域的图像。
简单示例:
Size size = MediaQuery.of(context).size;
return Scaffold(
extendBodyBehindAppBar: true,
body: Container(
// height: size.height * 0.3,
child: Image.asset(
'shopping_assets/images/Fruits/pineapple.png',
fit: BoxFit.cover,
height: size.height * 0.4,
width: size.width,
),
),
);
答案 5 :(得分:1)
Scaffold(extendBodyBehindAppBar: true);
答案 6 :(得分:0)
在我的情况下,我这样做的是:
另外使用自定义后退按钮(在本例中为FloatingActionButton
)创建应用栏。您仍然可以在Stack
内添加小部件。
class Home extends StatefulWidget {
@override
_EditProfilePageState createState() => _EditProfilePageState();
}
class _HomeState extends State< Home > {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: <Widget>[
this._backgroundImage(), // --> Background Image
Positioned( // --> App Bar
child: AppBar(
backgroundColor: Colors.transparent,
elevation: 0.0,
leading: Padding( // --> Custom Back Button
padding: const EdgeInsets.all(8.0),
child: FloatingActionButton(
backgroundColor: Colors.white,
mini: true,
onPressed: this._onBackPressed,
child: Icon(Icons.arrow_back, color: Colors.black),
),
),
),
),
// ------ Other Widgets ------
],
),
);
}
Widget _backgroundImage() {
return Container(
height: 272.0,
width: MediaQuery.of(context).size.width,
child: FadeInImage(
fit: BoxFit.cover,
image: NetworkImage(
'https://images.unsplash.com/photo-1527555197883-98e27ca0c1ea?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80'),
placeholder: AssetImage('assetName'),
),
);
}
void _onBackPressed() {
Navigator.of(context).pop();
}
}
在下面的链接中,您可以找到更多信息Link
答案 7 :(得分:0)
您可以尝试使用此代码对我有用
@override
Widget build(BuildContext context) {
_buildContext = context;
sw = MediaQuery.of(context).size.width;
sh = MediaQuery.of(context).size.height;
return new Container(
child: new Stack(
children: <Widget>[
new Container(
child: Stack(
children: <Widget>[
Container(
padding: EdgeInsets.all(20.0),
decoration: BoxDecoration(image: backgroundImage),
),
],
),
),
new Scaffold(
backgroundColor: Colors.transparent,
appBar: new AppBar(
title: new Text(Strings.page_register),
backgroundColor: Colors.transparent,
elevation: 0.0,
centerTitle: true,
),
body: SingleChildScrollView(
padding: EdgeInsets.all(20.0),
physics: BouncingScrollPhysics(),
scrollDirection: Axis.vertical,
child: new Form(
key: _formKey,
autovalidate: _autoValidate,
child: FormUI(),
),
),
)
],
),
);
}
backgroundImage
DecorationImage backgroundImage = new DecorationImage(
image: new ExactAssetImage('assets/images/welcome_background.png'),
fit: BoxFit.cover,
);
答案 8 :(得分:0)
现在Scaffold支持此功能(稳定-v1.12.13 + hotfix.5)。
extendBodyBehindAppBar
设置为true,elevation
设置为0以消除阴影,backgroundColor
的透明度。 @override
Widget build(BuildContext context) {
return Scaffold(
extendBodyBehindAppBar: true,
backgroundColor: Colors.red,
appBar: AppBar(
// backgroundColor: Colors.transparent,
backgroundColor: Color(0x44000000),
elevation: 0,
title: Text("Title"),
),
body: Center(child: Text("Content")),
);
}
答案 9 :(得分:0)
使用堆栈
@override Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: <Widget>[
backgroundBGContainer(),
Scaffold(
backgroundColor: Colors.transparent,
appBar: appBarWidgetCustomTitle(context: context, titleParam: ""),
body: SingleChildScrollView(
child: Column(
children: <Widget>[
_spaceWdgt(),
Center(
child: Stack(
children: <Widget>[
new Image.asset(
"assets/images/user_icon.png",
width: 117,
height: 97,
),
],
),
),
Widget backgroundBGContainer() {
return Container(
decoration: new BoxDecoration(
image: new DecorationImage(
image: new AssetImage("assets/images/ground_bg_image.png"),
fit: BoxFit.cover,
),
color: MyColor().groundBackColor),
);
}