Wrap Widget

某些時候在顯示行或者列時,由于寬度或者高度超過了屏幕,多余的部分無法顯示,并且還會報出錯誤.
使用Wrap來替代Row或者Column,就可以自適應(yīng)的進行新增行或者列來進行顯示.還可以設(shè)置對齊方式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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 前言 本文的目的是為了讓讀者掌握不同布局類Widget的布局特點,分享一些在實際使用過程遇到的一些問題,在《Flu...
    xqqlv閱讀 5,405評論 0 18
  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標簽?zāi)J的外補...
    _Yfling閱讀 14,108評論 1 92
  • 原文:UIStackView Tutorial: Introducing Stack Views最初教程由Jaww...
    Andy矢倉閱讀 25,670評論 2 56
  • 一、Unity簡介 1. Unity界面 Shift + Space : 放大界面 Scene界面按鈕渲染模式2D...
    MYves閱讀 8,662評論 0 22
  • 本文主要介紹Flutter布局中的Flow、Table、Wrap控件,詳細介紹了其布局行為以及使用場景,并對源碼進...
    Q吹個大氣球Q閱讀 14,662評論 2 20

友情鏈接更多精彩內(nèi)容