如何使 SliverAppBar 的标题垂直居中?
How to center the title of a SliverAppBar vertically?
我想在我的 SliverAppBar 中将标题垂直居中。我在互联网上找到了一个解决方案,您可以将空容器放在标题的上方和下方,以便文本居中,但问题是当您向上滚动并且只有小的应用栏在那里时,您看不到标题更多,因为空容器太大。我已经将我的标题水平居中,但我还需要它垂直居中。有人知道这个问题的解决方案吗?
这是我目前的代码:
SliverAppBar(
expandedHeight: MediaQuery.of(context).size.height * 0.20,
floating: false,
pinned: true,
flexibleSpace: FlexibleSpaceBar(
centerTitle: true,
title: Text("Training"),
background: Image.asset(
"assets/purpleBackground.png",
fit: BoxFit.cover,
),
),
),
这是可行的方法之一:
@override
Widget build(BuildContext context) {
double height = MediaQuery.of(context).size.height;
return Scaffold(
body: CustomScrollView(
slivers: <Widget>[
SliverAppBar(
pinned: true,
expandedHeight: height * 0.2,
collapsedHeight: height * 0.075,
flexibleSpace: LayoutBuilder(
builder: (context, constraints) {
double appBarHeight = constraints.biggest.height; //getting AppBar height
bool isExpanded = appBarHeight > height * 0.2; //check if AppBar is expanded
return FlexibleSpaceBar(
titlePadding: EdgeInsets.zero,
centerTitle: true,
title: SizedBox(
height: height * 0.15,
child: Column(
mainAxisAlignment: isExpanded
? MainAxisAlignment.center
: MainAxisAlignment.end,
children: <Widget>[
Container(
padding:
isExpanded ? EdgeInsets.zero : EdgeInsets.all(20),
child: Text(
"Training",
),
),
],
),
),
);
},
),
),
SliverList(
delegate: SliverChildListDelegate.fixed(
List.generate(
50,
(index) => ListTile(
title: Text('Index $index'),
),
),
),
),
],
),
);
输出:
我想在我的 SliverAppBar 中将标题垂直居中。我在互联网上找到了一个解决方案,您可以将空容器放在标题的上方和下方,以便文本居中,但问题是当您向上滚动并且只有小的应用栏在那里时,您看不到标题更多,因为空容器太大。我已经将我的标题水平居中,但我还需要它垂直居中。有人知道这个问题的解决方案吗?
这是我目前的代码:
SliverAppBar(
expandedHeight: MediaQuery.of(context).size.height * 0.20,
floating: false,
pinned: true,
flexibleSpace: FlexibleSpaceBar(
centerTitle: true,
title: Text("Training"),
background: Image.asset(
"assets/purpleBackground.png",
fit: BoxFit.cover,
),
),
),
这是可行的方法之一:
@override
Widget build(BuildContext context) {
double height = MediaQuery.of(context).size.height;
return Scaffold(
body: CustomScrollView(
slivers: <Widget>[
SliverAppBar(
pinned: true,
expandedHeight: height * 0.2,
collapsedHeight: height * 0.075,
flexibleSpace: LayoutBuilder(
builder: (context, constraints) {
double appBarHeight = constraints.biggest.height; //getting AppBar height
bool isExpanded = appBarHeight > height * 0.2; //check if AppBar is expanded
return FlexibleSpaceBar(
titlePadding: EdgeInsets.zero,
centerTitle: true,
title: SizedBox(
height: height * 0.15,
child: Column(
mainAxisAlignment: isExpanded
? MainAxisAlignment.center
: MainAxisAlignment.end,
children: <Widget>[
Container(
padding:
isExpanded ? EdgeInsets.zero : EdgeInsets.all(20),
child: Text(
"Training",
),
),
],
),
),
);
},
),
),
SliverList(
delegate: SliverChildListDelegate.fixed(
List.generate(
50,
(index) => ListTile(
title: Text('Index $index'),
),
),
),
),
],
),
);
输出: