【问题标题】:Flutter Centering text in AppBar在 AppBar 中颤振居中文本
【发布时间】:2021-10-04 07:57:50
【问题描述】:

我只想将整个文本居中(当然它应该仍在 AppBar 中)。谢谢

        appBar: AppBar(
            toolbarHeight: 80.0,
            backgroundColor: Colors.amber,
            title: Row(children: [
              Text("Text",
                  style: GoogleFonts.josefinSans(
                      textStyle: const TextStyle(fontSize: 40.0))),
              Text(
                "The Game",
                style: GoogleFonts.overpass(
                    textStyle: const TextStyle(fontSize: 20.0)),
              )
            ])),

【问题讨论】:

    标签: flutter dart coding-style centering appbar


    【解决方案1】:

    Row 小部件中包含mainAxisAlignmentMainAxisAlignment.center

    示例代码:

    AppBar(
                toolbarHeight: 80.0,
                backgroundColor: Colors.amber,
                title: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
                  Text("Text",
                      style: GoogleFonts.josefinSans(
                          textStyle: const TextStyle(fontSize: 40.0))),
                  Text(
                    "The Game",
                    style: GoogleFonts.overpass(
                        textStyle: const TextStyle(fontSize: 20.0)),
                  )
                ]))
    

    【讨论】:

    • 你是上帝
    【解决方案2】:

    使用 centerTitle : true

    appBar: AppBar(
                centerTitle: true,
                toolbarHeight: 80.0,
                backgroundColor: Colors.amber,
                title: Row(children: [
                  Text("Text",
                      style: GoogleFonts.josefinSans(
                          textStyle: const TextStyle(fontSize: 40.0))),
                  Text(
                    "The Game",
                    style: GoogleFonts.overpass(
                        textStyle: const TextStyle(fontSize: 20.0)),
                  )
                ])),
    

    【讨论】:

    • 是的,我试过这个,只有当我插入“列”而不是“行”时它才有效。我不知道为什么
    • 当我们使用Text 小部件以换取title 值时,此解决方案有效。
    • @Malak 迟到的回复,你也需要设置 Row 的中心孩子。 mainAxisSize: MainAxisSize.max, mainAxisAlignment: MainAxisAlignment.center
    猜你喜欢
    • 2023-01-16
    • 2021-11-09
    • 1970-01-01
    • 2020-04-16
    • 2021-08-01
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多