Wrap Widget

某些时候在显示行或者列时,由于宽度或者高度超过了屏幕,多余的部分无法显示,并且还会报出错误.
使用Wrap来替代Row或者Column,就可以自适应的进行新增行或者列来进行显示.还可以设置对齐方式alignment,runAlignment;间距spacing,runspacing等.

import 'package:flutter/material.dart';

class GoogleWarp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Row(
          children: <Widget>[
            Container(
              color: Colors.blue,
              height: 50.0,
              width: 100.0,
            ),
            Container(
              color: Colors.orange,
              height: 50.0,
              width: 100.0,
            ),
            Container(
              color: Colors.blueGrey,
              height: 50.0,
              width: 100.0,
            ),
            Container(
              color: Colors.red,
              height: 50.0,
              width: 100.0,
            ),
            Container(
              color: Colors.green,
              height: 50.0,
              width: 100.0,
            ),
          ],
        ),
        SizedBox(
          height: 10.0,
        ),
        Divider(height: 1.0, color: Colors.red,),
        SizedBox(
          height: 10.0,
        ),
        Wrap(
          alignment: WrapAlignment.center,
          spacing: 10.0,
          runSpacing: 20.0,
          runAlignment: WrapAlignment.start,
          children: <Widget>[
            Container(
              color: Colors.blue,
              height: 50.0,
              width: 100.0,
            ),
            Container(
              color: Colors.orange,
              height: 50.0,
              width: 100.0,
            ),
            Container(
              color: Colors.blueGrey,
              height: 50.0,
              width: 100.0,
            ),
            Container(
              color: Colors.red,
              height: 50.0,
              width: 100.0,
            ),
            Container(
              color: Colors.green,
              height: 50.0,
              width: 100.0,
            ),
          ],
        ),
        SizedBox(
          height: 10.0,
        ),
        Divider(height: 1.0, color: Colors.red,),
        SizedBox(
          height: 10.0,
        ),
        Wrap(
          alignment: WrapAlignment.spaceAround,
          spacing: 10.0,
          direction: Axis.vertical,
          runSpacing: 20.0,
          runAlignment: WrapAlignment.start,
          children: <Widget>[
            Container(
              color: Colors.blue,
              height: 50.0,
              width: 100.0,
            ),
            Container(
              color: Colors.orange,
              height: 50.0,
              width: 100.0,
            ),
            Container(
              color: Colors.blueGrey,
              height: 50.0,
              width: 100.0,
            ),
            Container(
              color: Colors.red,
              height: 50.0,
              width: 100.0,
            ),
            Container(
              color: Colors.green,
              height: 50.0,
              width: 100.0,
            ),
          ],
        ),
        SizedBox(
          height: 10.0,
        ),
        Divider(height: 1.0, color: Colors.red,),
        SizedBox(
          height: 10.0,
        ),
        Wrap(
          spacing: 10.0,
          runSpacing: 20.0,
          textDirection: TextDirection.rtl,
          verticalDirection: VerticalDirection.up,
          children: <Widget>[
            Container(
              color: Colors.blue,
              height: 50.0,
              width: 100.0,
            ),
            Container(
              color: Colors.orange,
              height: 50.0,
              width: 100.0,
            ),
            Container(
              color: Colors.blueGrey,
              height: 50.0,
              width: 100.0,
            ),
            Container(
              color: Colors.red,
              height: 50.0,
              width: 100.0,
            ),
            Container(
              color: Colors.green,
              height: 50.0,
              width: 100.0,
            ),
          ],
        )
      ],
    );
  }
}

基本属性

  • alignment 主轴对齐
  • crossAixsAligment 横轴对齐
  • direction 子控件排序方向,垂直或者水平
  • runAligment 横轴显示方向
  • runSpacing 垂直间距
  • spacing 横向间距
  • textDirection 横向顺序,从左到右,从右到左
  • vericalDirection 垂直顺序

我的博客
代码github

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前言 本文的目的是为了让读者掌握不同布局类Widget的布局特点,分享一些在实际使用过程遇到的一些问题,在《Flu...
    xqqlv阅读 10,655评论 0 18
  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 14,684评论 1 92
  • 原文:UIStackView Tutorial: Introducing Stack Views最初教程由Jaww...
    Andy矢倉阅读 25,581评论 2 56
  • 一、Unity简介 1. Unity界面 Shift + Space : 放大界面 Scene界面按钮渲染模式2D...
    MYves阅读 12,664评论 0 22
  • 本文主要介绍Flutter布局中的Flow、Table、Wrap控件,详细介绍了其布局行为以及使用场景,并对源码进...
    Q吹个大气球Q阅读 14,886评论 2 20