如何垂直居中 SliverAppBar 的标题?

如何解决如何垂直居中 SliverAppBar 的标题?

我想在我的 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'),);
 

输出:

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?