Flutter 颤振:如何设置FlexibleSpaceBar标题的样式?
只需将文本包装在作为背景的容器中,并为其提供尺寸和颜色即可Flutter 颤振:如何设置FlexibleSpaceBar标题的样式?,flutter,Flutter,只需将文本包装在作为背景的容器中,并为其提供尺寸和颜色即可 Container( padding: const EdgeInsets.all(8.0), color: Colors.yellow, child: Text("New Years), ) 编辑:正如您提到的,您需要在FlexibleSpaceBar中使用此文本样式 假设您想在FlexibleSpaceBar中修饰标题文本,请注意,不必将文本小部件传递给标题字段,您可以将任何小部件传递给它 传递一个容器,其子对象是文本小
Container(
padding: const EdgeInsets.all(8.0),
color: Colors.yellow,
child: Text("New Years),
)
编辑:正如您提到的,您需要在FlexibleSpaceBar
中使用此文本样式
假设您想在FlexibleSpaceBar
中修饰标题文本,请注意,不必将文本
小部件传递给标题字段,您可以将任何小部件传递给它
传递一个容器
,其子对象是文本
小部件,并设置容器样式。请参阅以下代码:
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(home: MyApp()));
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: CustomScrollView(
physics: const BouncingScrollPhysics(),
slivers: <Widget>[
SliverAppBar(
stretch: true,
onStretchTrigger: () {
// Function callback for stretch
return;
},
expandedHeight: 300.0,
flexibleSpace: FlexibleSpaceBar(
stretchModes: <StretchMode>[
StretchMode.zoomBackground,
StretchMode.blurBackground,
StretchMode.fadeTitle,
],
centerTitle: true,
title: Container( //HERE...
color: Colors.yellow,
padding: const EdgeInsets.all(8),
child: Text(
'NEW YEARS',
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
),
),
),
background: Stack(
fit: StackFit.expand,
children: [
Image.network(
'https://flutter.github.io/assets-for-api-docs/assets/widgets/owl-2.jpg',
fit: BoxFit.cover,
),
const DecoratedBox(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment(0.0, 0.5),
end: Alignment(0.0, 0.0),
colors: <Color>[
Color(0x60000000),
Color(0x00000000),
],
),
),
),
],
),
),
),
SliverList(
delegate: SliverChildListDelegate([
ListTile(
leading: Icon(Icons.wb_sunny),
title: Text('Sunday'),
subtitle: Text('sunny, h: 80, l: 65'),
),
ListTile(
leading: Icon(Icons.wb_sunny),
title: Text('Monday'),
subtitle: Text('sunny, h: 80, l: 65'),
),
// ListTiles++
]),
),
],
),
);
}
}
导入“包装:颤振/材料.省道”;
void main()=>runApp(MaterialApp(home:MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:自定义滚动视图(
物理:常量反弹CrollPhysics(),
条子:[
滑杆(
是的,
onStretchTrigger:(){
//用于拉伸的函数回调
返回;
},
扩展高度:300.0,
flexibleSpace:FlexibleSpaceBar(
拉伸模式:[
StretchMode.zoomBackground,
StretchMode.blurBackground,
StretchMode.fadeTitle,
],
标题:对,
标题:容器(//此处。。。
颜色:颜色,黄色,
填充:常量边集。全部(8),
子:文本(
“新年”,
样式:TextStyle(
颜色:颜色,黑色,
fontWeight:fontWeight.bold,
),
),
),
背景:堆栈(
fit:StackFit.expand,
儿童:[
图像网络(
'https://flutter.github.io/assets-for-api-docs/assets/widgets/owl-2.jpg',
适合:BoxFit.cover,
),
常数装饰盒(
装饰:盒子装饰(
梯度:线性梯度(
开始:对齐(0.0,0.5),
结束:对齐(0.0,0.0),
颜色:[
颜色(0x60000000),
颜色(0x00000000),
],
),
),
),
],
),
),
),
银表(
委托:SliverChildListDelegate([
列表砖(
领先:图标(Icons.wb_sunny),
标题:文本(“星期日”),
副标题:文本(“sunny,h:80,l:65”),
),
列表砖(
领先:图标(Icons.wb_sunny),
标题:文本(“星期一”),
副标题:文本(“sunny,h:80,l:65”),
),
//列表砖++
]),
),
],
),
);
}
}
只需将文本包装在作为背景的容器中,并赋予其尺寸和颜色即可
Container(
padding: const EdgeInsets.all(8.0),
color: Colors.yellow,
child: Text("New Years),
)
编辑:正如您提到的,您需要在FlexibleSpaceBar
中使用此文本样式
假设您想在FlexibleSpaceBar
中修饰标题文本,请注意,不必将文本
小部件传递给标题字段,您可以将任何小部件传递给它
传递一个容器
,其子对象是文本
小部件,并设置容器样式。请参阅以下代码:
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(home: MyApp()));
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: CustomScrollView(
physics: const BouncingScrollPhysics(),
slivers: <Widget>[
SliverAppBar(
stretch: true,
onStretchTrigger: () {
// Function callback for stretch
return;
},
expandedHeight: 300.0,
flexibleSpace: FlexibleSpaceBar(
stretchModes: <StretchMode>[
StretchMode.zoomBackground,
StretchMode.blurBackground,
StretchMode.fadeTitle,
],
centerTitle: true,
title: Container( //HERE...
color: Colors.yellow,
padding: const EdgeInsets.all(8),
child: Text(
'NEW YEARS',
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
),
),
),
background: Stack(
fit: StackFit.expand,
children: [
Image.network(
'https://flutter.github.io/assets-for-api-docs/assets/widgets/owl-2.jpg',
fit: BoxFit.cover,
),
const DecoratedBox(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment(0.0, 0.5),
end: Alignment(0.0, 0.0),
colors: <Color>[
Color(0x60000000),
Color(0x00000000),
],
),
),
),
],
),
),
),
SliverList(
delegate: SliverChildListDelegate([
ListTile(
leading: Icon(Icons.wb_sunny),
title: Text('Sunday'),
subtitle: Text('sunny, h: 80, l: 65'),
),
ListTile(
leading: Icon(Icons.wb_sunny),
title: Text('Monday'),
subtitle: Text('sunny, h: 80, l: 65'),
),
// ListTiles++
]),
),
],
),
);
}
}
导入“包装:颤振/材料.省道”;
void main()=>runApp(MaterialApp(home:MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:自定义滚动视图(
物理:常量反弹CrollPhysics(),
条子:[
滑杆(
是的,
onStretchTrigger:(){
//用于拉伸的函数回调
返回;
},
扩展高度:300.0,
flexibleSpace:FlexibleSpaceBar(
拉伸模式:[
StretchMode.zoomBackground,
StretchMode.blurBackground,
StretchMode.fadeTitle,
],
标题:对,
标题:容器(//此处。。。
颜色:颜色,黄色,
填充:常量边集。全部(8),
子:文本(
“新年”,
样式:TextStyle(
颜色:颜色,黑色,
fontWeight:fontWeight.bold,
),
),
),
背景:堆栈(
fit:StackFit.expand,
儿童:[
图像网络(
'https://flutter.github.io/assets-for-api-docs/assets/widgets/owl-2.jpg',
适合:BoxFit.cover,
),
常数装饰盒(
装饰:盒子装饰(
梯度:线性梯度(
开始:对齐(0.0,0.5),
结束:对齐(0.0,0.0),
颜色:[
颜色(0x60000000),
颜色(0x00000000),
],
),
),
),
],
),
),
),
银表(
委托:SliverChildListDelegate([
列表砖(
领先:图标(Icons.wb_sunny),
标题:文本(“星期日”),
副标题:文本(“sunny,h:80,l:65”),
),
列表砖(
领先:图标(Icons.wb_sunny),
标题:文本(“星期一”),
副标题:文本(“sunny,h:80,l:65”),
),