You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中如何将AppBar标题对齐到右侧?

Flutter实现AppBar标题右对齐的两种方法

针对AppBar标题需要右对齐的需求,这里提供两种简洁的实现方式:

方法一:通过titleTextStyle设置文本对齐

直接在AppBar的titleTextStyle中添加textAlign: TextAlign.right,让标题文本自身右对齐:

import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          toolbarHeight: 50.0,
          titleTextStyle: const TextStyle(
            color: Colors.black,
            fontSize: 18.0,
            fontWeight: FontWeight.w500,
            textAlign: TextAlign.right,
          ),
          title: const Text('This is the title !'),
          backgroundColor: Colors.amber,
        ),
      ),
    ));

方法二:用Align组件包裹标题

将标题文本包裹在Align组件内,通过alignment: Alignment.centerRight强制标题靠右对齐,这种方式在AppBar存在左侧leading组件时,依然能保证标题紧贴右侧:

import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          toolbarHeight: 50.0,
          title: Align(
            alignment: Alignment.centerRight,
            child: const Text(
              'This is the title !',
              style: TextStyle(color: Colors.black),
            ),
          ),
          backgroundColor: Colors.amber,
        ),
      ),
    ));

内容的提问来源于stack exchange,提问作者Chathura Abeygunawardhana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 23:55:13