可容纳一个子组件,并解除该组件的所有区域约束

相关组件

ConstrainedBox

UnConstrainedBox基本使用

  1. <br />【child】 : 孩子组件 【Widget】<br />【constrainedAxis】 : 仍受约束的轴*2 【Axis】<br />【alignment】 : 对齐方式 【AlignmentGeometry】<br />![191.gif](https://cdn.nlark.com/yuque/0/2020/gif/326147/1589508320746-5faec62c-18cc-4ba0-9bbb-9a4d4e32d828.gif#align=left&display=inline&height=151&margin=%5Bobject%20Object%5D&name=191.gif&originHeight=151&originWidth=397&size=85597&status=done&style=none&width=397)
import 'package:flutter/material.dart';
class CustomUnConstrainedBox extends StatefulWidget {
  @override
  _CustomUnConstrainedBoxState createState() => _CustomUnConstrainedBoxState();
}

class _CustomUnConstrainedBoxState extends State<CustomUnConstrainedBox> {
  var _value = false;

  @override
  Widget build(BuildContext context) {
    return Wrap(
      spacing: 20,
      children: <Widget>[_buildUnconstrainedBox(), _buildConstrainedAxis()],
    );
  }

  Widget _buildUnconstrainedBox() {
    var child = Container(
      color: Colors.cyanAccent,
      width: 60,
      height: 60,
      child: Switch(
        value: _value,
        onChanged: (v) {
          setState(() {
            _value = v;
          });
        },
      ),
    );

    return Column(
      children: <Widget>[
        Container(
          color: Colors.grey.withAlpha(22),
          width: 150,
          height: 100,
          child: _value
              ? UnconstrainedBox(alignment: Alignment.center, child: child)
              : child,
        ),
        Text(_value ? "已解除约束" : "子组件受约束")
      ],
    );
  }

  Widget _buildConstrainedAxis() {
    return Column(
      children: <Widget>[
        Container(
          color: Colors.grey.withAlpha(22),
          width: 150,
          height: 100,
          child: UnconstrainedBox(
              alignment: Alignment.center,
              constrainedAxis: Axis.vertical,
              child: Container(
                color: Colors.cyanAccent,
                width: 60,
                height: 60,
              )),
        ),
        Text("竖直方向仍约束")
      ],
    );
  }
}