Flutter布局问题:如何将TextButton置于文本下方?
要实现TextButton在文本下方的效果,核心是把文本和按钮放在垂直布局的Column组件中,替代原来单独包裹文本的Align。以下是调整后的完整代码:
Positioned( bottom: -50, child: Container( width: 281, height: 111, decoration: BoxDecoration( color: orangeDetails.withOpacity(0.8), borderRadius: BorderRadius.circular(9), boxShadow: [ BoxShadow( color: Colors.black.withOpacity(0.1), spreadRadius: 3, blurRadius: 4, offset: Offset(0, 2) // muda a posição da sombra ), ], ), child: Row( mainAxisAlignment: MainAxisAlignment.start, children: [ Image.asset( "assets/images/metro.png", width: 100, ), // 用Column替换原来的Align,实现文本和按钮垂直排列 Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( "Acompanhe a rotas das linhas \n e estações do metrô \n de Recife", style: TextStyle( color: Colors.white, fontSize: 12, fontWeight: FontWeight.w700, ), ), SizedBox(height: 8), // 增加文本与按钮的间距,优化视觉效果 TextButton( onPressed: (){}, child: Text("VER LINHAS"), style: ButtonStyle( foregroundColor: MaterialStateProperty.all(Colors.black), backgroundColor: MaterialStateProperty.all(blueDetails), fixedSize: MaterialStateProperty.all(const Size(95, 30)), // 调整按钮高度,避免过扁 shape: MaterialStateProperty.all<RoundedRectangleBorder>( RoundedRectangleBorder( borderRadius: BorderRadius.circular(3), ) ) ), ), ], ), ], ), ), ),
关键调整说明:
- 替换
Align为Column,通过垂直布局实现按钮在文本下方的排列 - 给
Column设置mainAxisAlignment: MainAxisAlignment.center,保证文本和按钮整体在容器内垂直居中 - 添加
SizedBox给文本和按钮增加间距,提升界面美观度 - 修改
TextButton的fixedSize高度,避免原设置的高度5导致按钮过扁无法正常显示
内容的提问来源于stack exchange,提问作者Flavio Henrique
相关产品推荐
相关产品推荐

