Flutter 颤振中多个屏幕的一个背景图像
在每一页上,我都喜欢这样:Flutter 颤振中多个屏幕的一个背景图像,flutter,Flutter,在每一页上,我都喜欢这样: return Scaffold( body: Container( width: MediaQuery.of(context).size.width, height: MediaQuery.of(context).size.height, decoration: BoxDecoration( color: Colors.black, image: DecorationI
return Scaffold(
body: Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
decoration: BoxDecoration(
color: Colors.black,
image: DecorationImage(
image: AssetImage('assets/images/background.png'),
fit: BoxFit.cover,
colorFilter: new ColorFilter.mode(Colors.black.withOpacity(0.9), BlendMode.dstATop),
)
),
child:...
);
return ScaffoldWithBackground(
child:...//Code for screen
);
我在flutter中的所有屏幕都只使用一个图像吗?您可以为此定义自定义小部件
customContainer(child){
return Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
decoration: BoxDecoration(
color: Colors.black,
image: DecorationImage(
image: AssetImage('assets/images/background.png'),
fit: BoxFit.cover,
colorFilter: new ColorFilter.mode(Colors.black.withOpacity(0.9), BlendMode.dstATop),
)
),
child: child,
);
}
和。。无论你在哪里需要它
return Scaffold(
body: customContainer(yourChildWidget),
);
希望它能回答您的问题。您可以为此定义自定义小部件
customContainer(child){
return Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
decoration: BoxDecoration(
color: Colors.black,
image: DecorationImage(
image: AssetImage('assets/images/background.png'),
fit: BoxFit.cover,
colorFilter: new ColorFilter.mode(Colors.black.withOpacity(0.9), BlendMode.dstATop),
)
),
child: child,
);
}
和。。无论你在哪里需要它
return Scaffold(
body: customContainer(yourChildWidget),
);
希望它能回答您的问题。好吧,没有一个函数/小部件/方法/任何东西可以全局设置。您可能要做的是将公共代码放在一个可重用类中,比如MyBackground(),然后像这样在任何地方使用它:
return Scaffold(
body: MyBackground(),
child:...
);
这样,您可以在任何需要的地方重复使用此代码,并且一个地方的更改将影响所有地方。好的,没有一个函数/小部件/方法/任何东西可以全局设置。您可能要做的是将公共代码放在一个可重用类中,比如MyBackground(),然后像这样在任何地方使用它:
return Scaffold(
body: MyBackground(),
child:...
);
通过这种方式,您可以在任何需要的地方重复使用此代码,并且一个地方的更改将影响所有地方。一种方法是将具有此背景的
脚手架的代码提取到自定义可重用小部件中,该小部件从各个屏幕接收子小部件,如下所示:
class ScaffoldWithBackground extends StatelessWidget {
final Widget child;
ScaffoldWithBackground({@required this.child});
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
decoration: BoxDecoration(
color: Colors.black,
image: DecorationImage(
image: AssetImage('assets/images/background.png'),
fit: BoxFit.cover,
colorFilter: new ColorFilter.mode(
Colors.black.withOpacity(0.9), BlendMode.dstATop),
)),
child: child));
}
}
在您的个人屏幕中,您可以这样使用它:
return Scaffold(
body: Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
decoration: BoxDecoration(
color: Colors.black,
image: DecorationImage(
image: AssetImage('assets/images/background.png'),
fit: BoxFit.cover,
colorFilter: new ColorFilter.mode(Colors.black.withOpacity(0.9), BlendMode.dstATop),
)
),
child:...
);
return ScaffoldWithBackground(
child:...//Code for screen
);
一种方法是将具有此背景的Scaffold
的代码提取到一个定制的可重用小部件中,该小部件从如下各个屏幕接收子小部件:
class ScaffoldWithBackground extends StatelessWidget {
final Widget child;
ScaffoldWithBackground({@required this.child});
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
decoration: BoxDecoration(
color: Colors.black,
image: DecorationImage(
image: AssetImage('assets/images/background.png'),
fit: BoxFit.cover,
colorFilter: new ColorFilter.mode(
Colors.black.withOpacity(0.9), BlendMode.dstATop),
)),
child: child));
}
}
在您的个人屏幕中,您可以这样使用它:
return Scaffold(
body: Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
decoration: BoxDecoration(
color: Colors.black,
image: DecorationImage(
image: AssetImage('assets/images/background.png'),
fit: BoxFit.cover,
colorFilter: new ColorFilter.mode(Colors.black.withOpacity(0.9), BlendMode.dstATop),
)
),
child:...
);
return ScaffoldWithBackground(
child:...//Code for screen
);