我想在AppBar中添加渐变颜色,如果是,那么我怎么能这样做呢?那么我是否需要制作一个容器并在列中创建一个列装饰和渐变颜色.
Rik*_*137 74
这应该可以完美地工作:
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
flexibleSpace: Container(
decoration: const BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: <Color>[Colors.black, Colors.blue]),
),
),
),
);
Run Code Online (Sandbox Code Playgroud)
Man*_*ans 20
我不相信你可以将渐变传递给AppBar,因为它需要Color而不是渐变.
但是,您可以创建自己的小部件来模仿AppBar,除非使用渐变.看一下这个我从Planets-Flutter教程拼凑在一起的例子以及它下面的代码.
import "package:flutter/material.dart";
class Page extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Column(children : <Widget>[new GradientAppBar("Custom Gradient App Bar"), new Container()],);
}
}
class GradientAppBar extends StatelessWidget {
final String title;
final double barHeight = 50.0;
GradientAppBar(this.title);
@override
Widget build(BuildContext context) {
final double statusbarHeight = MediaQuery
.of(context)
.padding
.top;
return new Container(
padding: new EdgeInsets.only(top: statusbarHeight),
height: statusbarHeight + barHeight,
child: new Center(
child: new Text(
title,
style: new TextStyle(fontSize: 20.0, color: Colors.white, fontWeight: FontWeight.bold),
),
),
decoration: new BoxDecoration(
gradient: new LinearGradient(
colors: [Colors.red, Colors.blue],
begin: const FractionalOffset(0.0, 0.0),
end: const FractionalOffset(0.5, 0.0),
stops: [0.0, 1.0],
tileMode: TileMode.clamp
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.如果您有任何疑问,请告诉我.
AppBar默认不具有该功能。但是您可以制作一个类似于AppBar的小部件,它将具有如下渐变背景:
Widget build(BuildContext context) {
return new Scaffold(
appBar: new PreferredSize(
child: new Container(
padding: new EdgeInsets.only(
top: MediaQuery.of(context).padding.top
),
child: new Padding(
padding: const EdgeInsets.only(
left: 30.0,
top: 20.0,
bottom: 20.0
),
child: new Text(
'Arnold Parge',
style: new TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.w500,
color: Colors.white
),
),
),
decoration: new BoxDecoration(
gradient: new LinearGradient(
colors: [
Colors.red,
Colors.yellow
]
),
boxShadow: [
new BoxShadow(
color: Colors.grey[500],
blurRadius: 20.0,
spreadRadius: 1.0,
)
]
),
),
preferredSize: new Size(
MediaQuery.of(context).size.width,
150.0
),
),
body: new Center(
child: new Text('Hello'),
),
);
}
Run Code Online (Sandbox Code Playgroud)
这里boxShadow会给人以提升的感觉。
| 归档时间: |
|
| 查看次数: |
14567 次 |
| 最近记录: |