Flutter中如何缩小AppBar标题上方间距?设置titleSpacing无效
解决Flutter AppBar标题上方间距无法缩小的问题
嘿,你踩了个小细节坑——titleSpacing属性根本不是用来调整标题垂直间距的!它的作用是控制标题和左侧leading组件(比如返回按钮)之间的水平距离,所以你设置0.0只会让标题往左靠,不会改变上下的间距~
要缩小标题的上方间距,试试这几个实用方法:
方法1:调整AppBar的整体高度(最简单)
直接修改toolbarHeight属性,默认AppBar高度是56,把它改小就能同步压缩标题的上下间距:
AppBar( backgroundColor: Colors.amber, title: Text("Flying Dutchman", style: TextStyle( color: const Color(0xFF444444), fontSize: 30.0, fontWeight: FontWeight.w900, )), centerTitle: true, elevation: 0.0, // 按需调整高度,数值越小上下间距越窄 toolbarHeight: 40.0, )
方法2:单独调整标题的内边距(不改变AppBar高度)
如果不想压缩整个AppBar的高度,给标题套个Padding组件,用负上边距让标题往上移:
AppBar( backgroundColor: Colors.amber, title: Padding( // 负数padding会缩小标题与AppBar顶部的间距,按需调整数值 padding: EdgeInsets.only(top: -8.0), child: Text("Flying Dutchman", style: TextStyle( color: const Color(0xFF444444), fontSize: 30.0, fontWeight: FontWeight.w900, )), ), centerTitle: true, elevation: 0.0, )
方法3:完全自定义AppBar(最灵活)
如果需要更精细的布局控制,用PreferredSizeWidget自己实现一个AppBar,想怎么调就怎么调:
PreferredSize( preferredSize: Size.fromHeight(45.0), // 自定义整体高度 child: Container( color: Colors.amber, alignment: Alignment.center, child: Padding( // 精准控制标题的上下间距数值 padding: EdgeInsets.only(top: 4.0, bottom: 4.0), child: Text("Flying Dutchman", style: TextStyle( color: const Color(0xFF444444), fontSize: 30.0, fontWeight: FontWeight.w900, )), ), ), )
内容的提问来源于stack exchange,提问作者esu
相关产品推荐
相关产品推荐

