Flutter教学

Flutter(50):Layout组件之Constrained

2020-10-14  本文已影响0人  starryxp

Flutter教学目录持续更新中

Github源代码持续更新中

1.ConstrainedBox介绍

对其子项施加附加约束的widget,在约束条件上跟Container基本一直,有兴趣的可以看一下:Layout组件之Container

2.ConstrainedBox属性

3.BoxConstraints属性

4.例子

1602656686427.jpg
  @override
  Widget build(BuildContext context) {
    _myChild() {
      return DecoratedBox(
        decoration: BoxDecoration(color: Colors.red),
      );
    }

    return Scaffold(
      appBar: AppBar(
        title: Text('ConstrainedBox'),
      ),
      body: Container(
        color: Colors.white,
        alignment: Alignment.topCenter,
        child: Column(
          children: [
            ConstrainedBox(
              child: _myChild(),
              constraints: BoxConstraints(
                minWidth: 50,
                minHeight: 50,
                maxWidth: 300,
                maxHeight: 300,
              ),
            ),
            Container(
              color: Colors.amber,
              margin: EdgeInsets.only(top: 10),
              child: ConstrainedBox(
                child: _myChild(),
                constraints: BoxConstraints.tight(Size(100, 60)),
              ),
            ),
            Container(
              color: Colors.amber,
              margin: EdgeInsets.only(top: 10),
              constraints: BoxConstraints(
                maxWidth: 90,
                maxHeight: 40,
              ),
              child: ConstrainedBox(
                child: _myChild(),
                constraints: BoxConstraints.tight(Size(100, 60)),
              ),
            ),
            Container(
              color: Colors.amber,
              margin: EdgeInsets.only(top: 10),
              constraints: BoxConstraints(
                maxHeight: 100,
              ),
              child: ConstrainedBox(
                child: _myChild(),
                constraints: BoxConstraints.tight(Size(100, 20)),
              ),
            ),
            Container(
              color: Colors.amber,
              margin: EdgeInsets.only(top: 10),
              constraints: BoxConstraints(
                maxHeight: 10,
              ),
              child: ConstrainedBox(
                child: _myChild(),
                constraints: BoxConstraints(
                  minWidth: 120,
                  minHeight: 100,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

下一节:Flutter(51):Layout组件之Baseline

Layout组件之Baseline

Flutter教学目录持续更新中

Github源代码持续更新中

上一篇 下一篇

猜你喜欢

热点阅读