亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

Flutter 基礎布局Widgets之Padding、Offstage詳解

Padding概述

Padding作为一种向内扩充,即用来产生间距的组件,使用的方式也很简单就是是设置内边距属性,产生内边距的空白区域,使用的成本比Container要低一些,可以替代的话推荐使用Padding

Padding构造函数

const Padding({
    Key key,
    @required this.padding,
    Widget child,
  }
  • padding 定义padding的为EdgeInsetsGeometry,一般使用EdgeInsets
  • child 即需要扩充的组件

Padding示例EdgeInsets多种使用方式

// padding  给子节点补白,即内填充操作

import 'package:flutter/material.dart';

class PaddingLearn extends StatelessWidget {
  @override
  final boxContainer = Container(
    color: Colors.blueAccent,
    height: 50,
    width: 50,
  );
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: AppBar(title: Text('Padding')),
        body: new Center(
          child: Container(
            decoration: BoxDecoration(
              border: Border.all(),
            ),
            width: 250,
            height: 350,
            // 构建一个盒子,在盒子里测试padding的属性
            child: new Column(
              children: <Widget>[
                Padding(
                  // 上下左右全部统一补白
                  padding: EdgeInsets.all(16),
                  child: boxContainer,
                ),
                Padding(
                    // 垂直和水平方向补白
                    padding: EdgeInsets.symmetric(vertical: 0, horizontal: 30),
                    child: boxContainer
                ),
                Padding(
                    // 上、下、左、右 EdgeInsets.fromLTRB(double left, double top, double right, double bottom)
                    padding: EdgeInsets.fromLTRB(0, 30, 40, 50),
                    child: boxContainer
                ),
                Padding(
                    // 仅仅填充一个方向 比如left
                    padding: EdgeInsets.only(left: 50),
                    child: boxContainer
                )
              ]
            ),
        )
      )
    );
  }
}

Padding示例效果:
C8F8AD8AADB1170DFA5FD74B8BDC5BFB.png

// Offstage

import 'package:flutter/material.dart';

class OffstageLearn extends StatefulWidget {
  @override 
  OffstageLearnState createState() => OffstageLearnState();

}

class OffstageLearnState extends State<OffstageLearn> {
  bool _offstage = false;
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(
        title: Text('Offstage'),
      ),
      body: Column(children: <Widget>[
        // 视觉上隐藏小部件  _offstage 为真即隐藏
        Offstage(
          offstage: _offstage,
          child: Container(
            decoration: BoxDecoration(
              gradient: LinearGradient(colors: [Colors.blue, Colors.purple])
            ),
            width: 300,
            height: 400,
          ),
        ),
        IconButton(
          icon: Icon(Icons.accessible),
          onPressed: (){
            setState(() {
             _offstage = !_offstage; 
            });
          },
        ),
      ],)
    );
  }
}

Offstage概述

Offstage的功能即在视觉上隐藏Widget,设定参数为_offstage,为true即隐藏,反之则相反如果是true的,则将子元素放置在树中,但是不绘制任何内容,不让子元素用于hit测试,也不占用父元素中的任何空间。如果为false,则将子元素作为正常值包含在树中。

Offstage构造函数

``` const Offstage({ Key key, this.offstage = true, Widget child })```
  • _offstage 即控制是否隐藏子元素

Offstage对比示例

_offstage=false时:

// Offstage

import 'package:flutter/material.dart';

class OffstageLearn extends StatefulWidget {
  @override 
  OffstageLearnState createState() => OffstageLearnState();

}

class OffstageLearnState extends State<OffstageLearn> {
  bool _offstage = true;
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(
        title: Text('Offstage'),
      ),
      body: Column(children: <Widget>[
        // 视觉上隐藏小部件  _offstage 为真即隐藏
        Offstage(
          offstage: _offstage,
          child: Container(
            decoration: BoxDecoration(
              gradient: LinearGradient(colors: [Colors.blue, Colors.purple])
            ),
            width: 300,
            height: 400,
          ),
        ),
        IconButton(
          icon: Icon(Icons.accessible),
          onPressed: (){
            setState(() {
             _offstage = !_offstage; 
            });
          },
        ),
      ],)
    );
  }
}

效果如下:
Offstage

_offstage=true时:

// Offstage

import 'package:flutter/material.dart';

class OffstageLearn extends StatefulWidget {
  @override 
  OffstageLearnState createState() => OffstageLearnState();

}

class OffstageLearnState extends State<OffstageLearn> {
  bool _offstage = true;
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(
        title: Text('Offstage'),
      ),
      body: Column(children: <Widget>[
        // 视觉上隐藏小部件  _offstage 为真即隐藏
        Offstage(
          offstage: _offstage,
          child: Container(
            decoration: BoxDecoration(
              gradient: LinearGradient(colors: [Colors.blue, Colors.purple])
            ),
            width: 300,
            height: 400,
          ),
        ),
        IconButton(
          icon: Icon(Icons.accessible),
          onPressed: (){
            setState(() {
             _offstage = !_offstage; 
            });
          },
        ),
      ],)
    );
  }
}

效果如下:
Offstage

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
Python工程師
手記
粉絲
23
獲贊與收藏
95

關注作者,訂閱最新文章

閱讀免費教程

  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消