Flutter ConstrainedBox


简介

添加额外的限制条件到child上

  • 比如说,你限制child最小高度为50.0像素,就可以用constraints: const BoxConstraints(minHeight:50)

基本用法

添加额外的限制条件到child上

  • ex:添加ConstrainedBox约束如下,传入不同Width约束的Container效果
minWidth: 100.0,
minHeight: 20.0,
maxWidth: 300.0,
maxHeight: 50.0

实例演示

class ConstrainedBoxCreate extends StatelessWidget {
  final double currWidth;
  final String describe;

  const ConstrainedBoxCreate({Key key,this.currWidth,this.describe}):
        super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        ConstrainedBox(
          //制定一个指定大小的盒子,限定最大最小宽高
          constraints: const BoxConstraints(
            minWidth: 100.0,
            minHeight: 20.0,
            maxWidth: 300.0,
            maxHeight: 50.0,
          ),
          child:  Container(
            margin: const EdgeInsets.only(left: 20.0, right: 20.0),
            //child 宽高超过制定限制范围失效,效果宽/高=100/20区域
            width: currWidth,
            height: 250.0,
            child:  Text(
              describe,
              style: TextStyle(color: Colors.white),
            ),
            color: Color(0xfff8bbd0),
          ),
        ),
      ],
    );
  }
}