我想用容器包装一个脚手架,以便获得同样位于AppBar下方的渐变背景。基本上是全屏渐变背景。但是,我的尝试无济于事。 还有另一种方法,我可以保留AppBar的功能,但要使它位于整个屏幕的渐变之上?
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Test',
theme: ThemeData(
primarySwatch: Colors.yellow,
),
home: MyHomePage(title: 'Test'),
);
}
}
class MyHomePage extends StatelessWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topRight,
end: Alignment.bottomLeft,
stops: [0.1, 0.5, 0.7, 0.9],
colors: [
Colors.yellow[800],
Colors.yellow[700],
Colors.yellow[600],
Colors.yellow[400],
],
),
),
child: Scaffold(
appBar: AppBar(
title: Icon(Icons.menu),
backgroundColor: Color(0x00000000),
elevation: 0.0,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'dummy text',
),
Text(
'5',
style: Theme.of(context).textTheme.display1,
),
FloatingActionButton(
backgroundColor: Colors.white,
foregroundColor: Colors.black45,
elevation: 0.0,
onPressed: () {},
tooltip: 'Play',
child: Icon(Icons.play_arrow),
),
],
),
),
),
);
}
}
答案 0 :(得分:4)
在您的代码中-在Scaffold
下添加-backgroundColor: Colors.transparent,
child: Scaffold(
backgroundColor: Colors.transparent,
appBar: AppBar(
..
答案 1 :(得分:3)
您还可以像这样向AppBar
添加渐变,
new Scaffold(
appBar: AppBar(
title: Center(child: Text('Awesome AppBar')),
flexibleSpace: Container(
decoration: new BoxDecoration(
gradient: new LinearGradient(
colors: [
const Color(0xFF3366FF),
const Color(0xFF00CCFF),
],
begin: const FractionalOffset(0.0, 0.0),
end: const FractionalOffset(1.0, 0.0),
stops: [0.0, 1.0],
tileMode: TileMode.clamp),
),
),
),
body: ...,
);
LinearGradient参数:
colors
:要在渐变中使用的颜色的数组,在这种情况下为两种蓝色。begin
,end
:第一种颜色和最后一种颜色的位置,在这种情况下为FractionalOffset
使我们可以将x和y的坐标都视为从0到1的范围。由于我们需要水平梯度,因此两个度量均使用相同的Y,并且x从0.0(左)变为1.0(右)。stops
:此数组的大小应与颜色相同。它定义了颜色将如何分布。 [0.0,1.0]将从左到右填充。 [0.0,0.5]将从左到半条填充颜色,等等。tileMode
:如果停靠点没有塞满整个区域怎么办。在这种情况下,我们添加了钳位(它将重用上次使用的颜色),但是随着我们的渐变从0.0变为1.0,就没有必要了。希望这会有所帮助。
答案 2 :(得分:2)
我使用这段代码将background
的颜色设置为gradient
return MaterialApp(
home: Scaffold(
body: Container(
decoration: new BoxDecoration(
gradient: new LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Color.fromARGB(255, 25,178,238),
Color.fromARGB(255, 21,236,229)
],
)),
child: Align(
alignment: Alignment.center,
child: Image.asset(
"images/app_logo.png",
width: 128,
height: 128,
)),
),
),
);
答案 3 :(得分:0)
您只需在应用栏中使用FlexibleSpace
appBar: AppBar(
centerTitle: true,
title: Text(widget.title),
flexibleSpace: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: <Color>[
Colors.red,
Colors.blue
])
),
),
),
答案 4 :(得分:0)
您可以直接在代码下方使用任何容器或视图的渐变颜色
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Dashboard"),
),
body: Container(
alignment: Alignment.center,
decoration: BoxDecoration(
gradient: LinearGradient(colors: [Colors.red, Colors.blue, Colors.black])
)
),
);
}
}
答案 5 :(得分:-1)
只需在FlexibleSpace
中添加AppBar
并装饰容器。然后,应用程序栏是背景中的渐变。
appBar: AppBar(
title: Text('Flutter Demo'),
flexibleSpace: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.centerLeft,
end: Alignment.centerRight,
colors: <Color>[
Colors.red,
Colors.blue
],
),
),
),
),