Flutter让按钮填充满所在container

博客介绍了在Dart里,使用FlatButton时遇到文本对齐问题的解决办法。直接用text会使文本居中,且无法用textAlign修改为左对齐,因此改用row实现,在FlatButton的child中嵌套container来实现文本左对齐。

反例:

Container(
              alignment: Alignment.topLeft,
              // padding: EdgeInsets.all(1),
              // margin:new EdgeInsets.only(right:1.0),
              decoration: BoxDecoration(
                color: Colors.green,
                //每条列表项底部加一个边框
                border: Border(
                    bottom: BorderSide(width: 0.5, color: Color(0xFFd9d9d9))),
              ),
              child: FlatButton(
                // padding: new EdgeInsets.only(right: 200),
                color: Colors.blueGrey,
                child: Text(
                  "A. ${Provider.of<GetData>(context).answerA}",
                  style: TextStyle(fontSize: 22.0, color: Colors.white),
                ),
                onPressed: () {
                 
                },
              ),
            ),
image.png

改用row实现代码:

Row(
              children: <Widget>[
                Expanded(
                  child: FlatButton(
                    color: Colors.blueGrey,
                    child: Container(
                      alignment: Alignment.topLeft,
                      child: Text(
                        "A. ${Provider.of<GetData>(context).answerA}",
                        style: TextStyle(fontSize: 22.0, color: Colors.white),
                        textAlign: TextAlign.start,
                        maxLines: 1,
                      ),
                    ),
                    onPressed: () {
                      
                    },
                  ),
                )
              ],
            ),
image.png

这里FlatButton里的child没有直接使用text,而是嵌套了container的原因是, 直接用text,会导致文本居中, 用textAlign也没有办法修改为左对齐,所以只好中间加了个container实现左对齐.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值