可容纳多个组件,按照指定方向依次排布,可以很方便处理孩子的间距,当越界时可以自动换行。拥有主轴和交叉轴的对齐方式,比较灵活。

相关组件

Flex Row

Wrap的基础用法

  1. <br />【children】 : 组件列表 【List<Widget><br />【spacing】 : 主轴条目间距 【double】<br />【runSpacing】 : 交叉轴条目间距 【double】<br />【direction】 : 主轴对齐 【Axis】<br />![image.png](https://cdn.nlark.com/yuque/0/2020/png/326147/1589509154599-fcca1245-e58a-44fb-a527-da054af8c28b.png#align=left&display=inline&height=129&margin=%5Bobject%20Object%5D&name=image.png&originHeight=129&originWidth=351&size=5108&status=done&style=none&width=351)
  1. import 'package:flutter/material.dart';
  2. class DirectionWrap extends StatelessWidget {
  3. @override
  4. Widget build(BuildContext context) {
  5. return Wrap(
  6. children: Axis.values
  7. .map((mode) => Column(children: <Widget>[
  8. Container(
  9. margin: EdgeInsets.all(5),
  10. width: 160,
  11. height: 100,
  12. color: Colors.grey.withAlpha(33),
  13. child: _buildItem(mode)),
  14. Text(mode.toString().split('.')[1])
  15. ]))
  16. .toList());
  17. }
  18. final yellowBox = Container(
  19. color: Colors.yellow,
  20. height: 30,
  21. width: 50,
  22. );
  23. final redBox = Container(
  24. color: Colors.red,
  25. height: 40,
  26. width: 40,
  27. );
  28. final greenBox = Container(
  29. color: Colors.green,
  30. height: 40,
  31. width: 20,
  32. );
  33. final blackBox = Container(
  34. color: Colors.black,
  35. height: 10,
  36. width: 10,
  37. );
  38. final purpleBox = Container(
  39. color: Colors.purple,
  40. height: 20,
  41. width: 20,
  42. );
  43. final orangeBox = Container(
  44. color: Colors.orange,
  45. height: 80,
  46. width: 20,
  47. );
  48. final cyanBox = Container(
  49. color: Colors.cyanAccent,
  50. height: 10,
  51. width: 20,
  52. );
  53. _buildItem(mode) => Wrap(
  54. direction: mode,
  55. runSpacing: 10,
  56. spacing: 10,
  57. children: <Widget>[
  58. yellowBox, redBox, greenBox, cyanBox,
  59. blackBox, purpleBox, orangeBox,
  60. ],
  61. );
  62. }

Wrap的alignment属性

   <br />【alignment】 : 主轴对齐   【WrapAlignment】<br />![image.png](https://cdn.nlark.com/yuque/0/2020/png/326147/1589509168866-7318feee-872c-401d-ab35-039105a94ff7.png#align=left&display=inline&height=383&margin=%5Bobject%20Object%5D&name=image.png&originHeight=383&originWidth=343&size=14708&status=done&style=none&width=343)
import 'package:flutter/material.dart';
class WrapAlignmentWrap extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Wrap(
        children: WrapAlignment.values
            .map((mode) => Column(children: <Widget>[
          Container(
              margin: EdgeInsets.all(5),
              width: 160,
              height: 100,
              color: Colors.grey.withAlpha(88),
              child: _buildItem(mode)),
          Text(mode.toString().split('.')[1])
        ]))
            .toList());
  }

  final yellowBox = Container(
    color: Colors.yellow,
    height: 30,
    width: 50,
  );

  final redBox = Container(
    color: Colors.red,
    height: 40,
    width: 40,
  );
  final greenBox = Container(
    color: Colors.green,
    height: 40,
    width: 20,
  );
  final blackBox = Container(
    color: Colors.black,
    height: 10,
    width: 10,
  );
  final purpleBox = Container(
    color: Colors.purple,
    height: 20,
    width: 20,
  );
  final orangeBox = Container(
    color: Colors.orange,
    height: 80,
    width: 20,
  );
  final cyanBox = Container(
    color: Colors.cyanAccent,
    height: 10,
    width: 20,
  );

  _buildItem(mode) => Wrap(
    alignment: mode,
    runSpacing: 10,
    spacing: 10,
    children: <Widget>[
      yellowBox, redBox,
      greenBox, cyanBox,
      blackBox, purpleBox,
      orangeBox,
    ],
  );
}

Wrap的crossAxisAlignment属性

   <br />【crossAxisAlignment】 : 交叉轴对齐   【CrossAxisAlignment】<br />![image.png](https://cdn.nlark.com/yuque/0/2020/png/326147/1589509183012-1934e0a8-f6c1-4669-9a3b-4809e65c544a.png#align=left&display=inline&height=263&margin=%5Bobject%20Object%5D&name=image.png&originHeight=263&originWidth=351&size=7167&status=done&style=none&width=351)
import 'package:flutter/material.dart';
class CrossAxisAlignmentWrap extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Wrap(
        children: WrapCrossAlignment.values
            .map((mode) => Column(children: <Widget>[
          Container(
              margin: EdgeInsets.all(5),
              width: 160,
              height: 100,
              color: Colors.grey.withAlpha(88),
              child: _buildItem(mode)),
          Text(mode.toString().split('.')[1])
        ]))
            .toList());
  }

  final yellowBox = Container(
    color: Colors.yellow,
    height: 30,
    width: 50,
  );

  final redBox = Container(
    color: Colors.red,
    height: 40,
    width: 40,
  );
  final greenBox = Container(
    color: Colors.green,
    height: 40,
    width: 20,
  );
  final blackBox = Container(
    color: Colors.black,
    height: 10,
    width: 10,
  );
  final purpleBox = Container(
    color: Colors.purple,
    height: 20,
    width: 20,
  );
  final orangeBox = Container(
    color: Colors.orange,
    height: 80,
    width: 20,
  );
  final cyanBox = Container(
    color: Colors.cyanAccent,
    height: 10,
    width: 20,
  );

  _buildItem(mode) => Wrap(
    crossAxisAlignment: mode,
    runSpacing: 10,
    spacing: 10,
    children: <Widget>[
      yellowBox, redBox,
      greenBox, cyanBox,
      blackBox, purpleBox,
      orangeBox,
    ],
  );
}

Wrap的textDirection属性

【textDirection】 : 文字方向 【TextDirection】
image.png

import 'package:flutter/material.dart';
class TextDirectionWrap extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Wrap(
        children: TextDirection.values
            .map((mode) => Column(children: <Widget>[
          Container(
              margin: EdgeInsets.all(5),
              width: 160,
              height: 100,
              color: Colors.grey.withAlpha(88),
              child: _buildItem(mode)),
          Text(mode.toString().split('.')[1])
        ]))
            .toList());
  }

  final yellowBox = Container(
    color: Colors.yellow,
    height: 30,
    width: 50,
  );

  final redBox = Container(
    color: Colors.red,
    height: 40,
    width: 40,
  );
  final greenBox = Container(
    color: Colors.green,
    height: 40,
    width: 20,
  );
  final blackBox = Container(
    color: Colors.black,
    height: 10,
    width: 10,
  );
  final purpleBox = Container(
    color: Colors.purple,
    height: 20,
    width: 20,
  );
  final orangeBox = Container(
    color: Colors.orange,
    height: 80,
    width: 20,
  );
  final cyanBox = Container(
    color: Colors.cyanAccent,
    height: 10,
    width: 20,
  );

  _buildItem(mode) => Wrap(
    textDirection: mode,
    runSpacing: 10,
    spacing: 10,
    children: <Widget>[
      yellowBox, redBox, greenBox, cyanBox,
      blackBox, purpleBox, orangeBox,
    ],
  );
}

Wrap的verticalDirection属性

   <br />【verticalDirection】 : 竖直方向  【VerticalDirection】<br />![image.png](https://cdn.nlark.com/yuque/0/2020/png/326147/1589509205540-b416b3d9-b083-4530-a9f7-c8910efe3dc6.png#align=left&display=inline&height=132&margin=%5Bobject%20Object%5D&name=image.png&originHeight=132&originWidth=359&size=4458&status=done&style=none&width=359)
import 'package:flutter/material.dart';
class VerticalDirectionWrap extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Wrap(
        children: VerticalDirection.values
            .map((mode) => Column(children: <Widget>[
          Container(
              margin: EdgeInsets.all(5),
              width: 160,
              height: 100,
              color: Colors.grey.withAlpha(88),
              child: _buildItem(mode)),
          Text(mode.toString().split('.')[1])
        ]))
            .toList());
  }

  final yellowBox = Container(
    color: Colors.yellow,
    height: 30,
    width: 50,
  );

  final redBox = Container(
    color: Colors.red,
    height: 40,
    width: 40,
  );
  final greenBox = Container(
    color: Colors.green,
    height: 40,
    width: 20,
  );
  final blackBox = Container(
    color: Colors.black,
    height: 10,
    width: 10,
  );
  final purpleBox = Container(
    color: Colors.purple,
    height: 20,
    width: 20,
  );
  final orangeBox = Container(
    color: Colors.orange,
    height: 80,
    width: 20,
  );
  final cyanBox = Container(
    color: Colors.cyanAccent,
    height: 10,
    width: 20,
  );

  _buildItem(mode) => Wrap(
    verticalDirection: mode,
    direction: Axis.vertical,
    runSpacing: 10,
    spacing: 10,
    children: <Widget>[
      yellowBox, redBox, greenBox, cyanBox,
      blackBox, purpleBox, orangeBox,
    ],
  );
}