Flutter右侧对齐失效求助:如何将Row内文本容器右对齐
解决Flutter中Row内组件右对齐的问题
这问题我之前也碰到过!你直接给右侧的Container设置alignment没效果,是因为Row默认的MainAxisAlignment是start(靠左排列),而且Container本身的宽度是包裹内容的,对齐只在自己的小范围内生效,根本不会让它贴到Row的右侧。给你两个简单有效的解决方案:
方案1:用Row的MainAxisAlignment.spaceBetween
直接给Row设置mainAxisAlignment: MainAxisAlignment.spaceBetween,这样Row里的子组件会分别贴紧左右两端,完美实现你要的效果:
///Transactions Heading new Column( children: <Widget>[ new Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, // 核心修改 children: <Widget>[ new Container( margin: new EdgeInsets.only(top: 20.0, left: 10.0), child: new Text( "Transactions", style: new TextStyle( color: primaryTextColor, fontSize: 25.0, fontWeight: FontWeight.w700, letterSpacing: 0.1, ), ), ), new Container( margin: new EdgeInsets.only(top: 25.0, right: 10.0), // 加个右边距更美观 child: new Text( currentGoalTransactions.length.toString() + " items", style: new TextStyle( color: darkHeadingsTextColor, fontSize: 15.0), ), ), ], ), ]);
方案2:用Spacer()占位
如果之后你可能在中间添加其他组件,用Spacer()会更灵活——它会自动占据Row中剩余的所有空间,把右侧的组件挤到最右边:
///Transactions Heading new Column( children: <Widget>[ new Row( children: <Widget>[ new Container( margin: new EdgeInsets.only(top: 20.0, left: 10.0), child: new Text( "Transactions", style: new TextStyle( color: primaryTextColor, fontSize: 25.0, fontWeight: FontWeight.w700, letterSpacing: 0.1, ), ), ), Spacer(), // 新增Spacer占据中间空白 new Container( margin: new EdgeInsets.only(top: 25.0, right: 10.0), child: new Text( currentGoalTransactions.length.toString() + " items", style: new TextStyle( color: darkHeadingsTextColor, fontSize: 15.0), ), ), ], ), ]);
两种方法都能解决你的问题,选哪个看你后续的布局需求~
内容的提问来源于stack exchange,提问作者Musa Usman
相关产品推荐
相关产品推荐

