Flutter Widget:将宽度设置为剩余宽度而不使用绝对数字

时间:2020-04-14 23:48:15

标签: flutter

我有以下Flutter Widget。在“ +”的右侧,我们仍然看到很多可用的灰色空间。如何设置白框的宽度以使用所有剩余的空间而不使用绝对数?

enter image description here 导入'package:flutter / material.dart';

class QtyWidget extends StatefulWidget {
  String title;
  QtyWidget({this.title});
  @override
  _QtyWidgetState createState() => new _QtyWidgetState();
}

class _QtyWidgetState extends State<QtyWidget> {
  int _itemCount = 1;
  @override
  Widget build(BuildContext context) {

    return Container(
      padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
      color: Colors.grey[200],
      child: Row(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Container(
              width: 100,
              padding: EdgeInsets.fromLTRB(0, 0, 10, 0),
              child: Text(
                'Qty',
                style: TextStyle(
                    fontWeight: FontWeight.normal,
                    fontSize: 16,
                    color: Colors.grey[600]),
              ),
            ),
            Container(
              padding: EdgeInsets.fromLTRB(15, 0, 15, 0),
              decoration: BoxDecoration(
                  color: Colors.white,
                  border: Border.all(
                    color: Colors.amber[800],
                  ),
                  borderRadius: BorderRadius.all(Radius.circular(10))
              ),
              child: Row(
                children: <Widget>[
                  IconButton(icon: new Icon(Icons.remove),
                      onPressed: () {
                        setState(() {
                          _itemCount = _itemCount - 1 ;
                          if (_itemCount < 0) {
                            _itemCount = 1;
                          }
                        });

                      }),
                  Text(_itemCount.toString()),
                  IconButton(icon: new Icon(Icons.add),
                      onPressed: ()=>setState(()=>_itemCount++))
                ],
              ),
            ),
          ]),
    );

  }
}

1 个答案:

答案 0 :(得分:0)

您可以在下面复制粘贴运行完整代码
您可以将ContainerExpanded一起使用RowMainAxisAlignment.center

代码段

CrossAxisAlignment.center

工作演示

enter image description here

完整代码

Expanded(
              child: Container(
                padding: EdgeInsets.fromLTRB(15, 0, 15, 0),
                decoration: BoxDecoration(
                    color: Colors.white,
                    border: Border.all(
                      color: Colors.amber[800],
                    ),
                    borderRadius: BorderRadius.all(Radius.circular(10))),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,