簡體   English   中英

Flutter - 將 AppBar 標題對齊到頂部

[英]Flutter - Align AppBar title to top

我只想將我的 AppBar 的標題對齊到頂部而不是中間。

AppBar(
      title : Text('TITLE'),
      toolbarHeight : 100
)

我很驚訝這個簡單的問題以前從未被問過,而且幾乎沒有找到解決方案。 如果這個問題不應該在這里,請原諒我。 如果有我會立即刪除。

您可以使用 kToolbarHeight 獲得默認高度,然后您可以創建一個相同高度的大小框並在該大小的框內對齊文本

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
          appBar: AppBar(
              title: Container(
                  height: kToolbarHeight,
                  color: Colors.red,
                  child: Text(
                    "Some title",
                    style: TextStyle(
                      color: Colors.black,
                    ),
                    textAlign: TextAlign.center,
                  )))),
    );
  }
}

我添加了紅色只是為了表明 Container 填充了 AppBar。

預習

編輯

如果您希望為出現指定特定高度,也將相同的高度添加到 SizedBox..

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
          appBar: AppBar(
            toolbarHeight: 100,
              title: Container(
                  height: 100,
                  color: Colors.red,
                  child: Text(
                    "Some title",
                    style: TextStyle(
                      color: Colors.black,
                    ),
                    textAlign: TextAlign.center,
                  )))),
    );
  }
}

預覽2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM