(Flutter 六)布局:水平、垂直、层叠、卡片

一、Row:横向水平布局

Row的属性列表如下:

Row({
    Key key,
    MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start,
    MainAxisSize mainAxisSize = MainAxisSize.max,
    CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.center,
    TextDirection textDirection,
    VerticalDirection verticalDirection = VerticalDirection.down,
    TextBaseline textBaseline,
    List<Widget> children = const <Widget>[],
  })

可以看如下示例,

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp (
      title: 'Row Widget Demo',
      home: Scaffold(
        appBar: new AppBar(
          title: new Text('水平方向布局'),
        ),
        body: new Row(
          children: <Widget>[
            new RaisedButton(
              onPressed: (){},
              color: Colors.orange,
              child: new Text('Orange Button'),
            ),
            Expanded(child: new RaisedButton( /*
            expanded:扩充的,填满的。是一个组件。
            如果不使用这个组件,那么Row就会根据button原始大小填充,如果使用expanded,它就会填充满。
            如果这些子元素都使用expanded元素,那么这三个按钮会均匀分布填满整个屏幕。
            如果前后两个都使用的灵活布局中间使用expended的话,那么中间这个按钮会把剩下的空间都填充满。
              */
            onPressed: (){},
              color: Colors.red,
              child: new Text('Red Button'),
            ), ),
            new RaisedButton(
              onPressed: (){},
              color: Colors.yellow,
              child: new Text('Yellow Button'),
            ),
          ],
        ),
      ),
    );
  }
}

运行结果如下:


二、Column:纵向水平布局

Column的属性列表如下:

Column({
    Key key,
    MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start,
    MainAxisSize mainAxisSize = MainAxisSize.max,
    CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.center,
    TextDirection textDirection,
    VerticalDirection verticalDirection = VerticalDirection.down,
    TextBaseline textBaseline,
    List<Widget> children = const <Widget>[],
  }) : super(
    children: children,
    key: key,
    direction: Axis.vertical,
    mainAxisAlignment: mainAxisAlignment,
    mainAxisSize: mainAxisSize,
    crossAxisAlignment: crossAxisAlignment,
    textDirection: textDirection,
    verticalDirection: verticalDirection,
    textBaseline: textBaseline,
  );

示例如下:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp (
      title: 'Row Widget Demo',
      home: Scaffold(
        appBar: new AppBar(
          title: new Text('垂直方向布局'),
        ),
        body: new Column(
          crossAxisAlignment: CrossAxisAlignment.center, //对齐属性,根据最长的内容进行对齐,而不是针对整个屏幕
          mainAxisAlignment: MainAxisAlignment.center , //主轴方向对齐,由于现在是column组件,所以垂直方向是主轴,crossAxis是水平
          children: <Widget>[
            Text('I am cat'),
            Text('My name is Amy'),
            Text('I like cat'),
            Text('I like dog'),
          ],
        ),
      ),
    );
  }
}

运行结果如下:


由此我们可以看见这个布局主要是针对Column,如果想要针对整个屏幕居中,需要对整个column进行居中设置,核心代码块如下:

          body: Center( //对整个Column进行剧中设置
             child: Column(
              crossAxisAlignment: CrossAxisAlignment.center, //对齐属性,根据最长的内容进行对齐,而不是针对整个屏幕
              mainAxisAlignment: MainAxisAlignment.center , //主轴方向对齐,由于现在是column组件,所以垂直方向是主轴,crossAxis是水平
              children: <Widget>[
                Text('I am cat'),
                Text('My name is Amy'),
                Text('I like cat'),
                Text('I like dog'),
              ],
            ),
          ),

运行结果如下:


image.png

在实际开发过程中,如果需要头部和底部文本固定,中间的部分任意使用。可以使用Expended组件来实现。核心代码块如下:

body: Center(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center, //对齐属性,根据最长的内容进行对齐,而不是针对整个屏幕
              mainAxisAlignment: MainAxisAlignment.center , //主轴方向对齐,由于现在是column组件,所以垂直方向是主轴,crossAxis是水平
              children: <Widget>[
                Text('I am cat'),
                Expanded(child: Text('My name is Amy')),
                Text('I like cat'),
                Text('I like eat'),

              ],
            ),
          ),

PS:主轴和副轴的区分
在设置对齐方式的时候你会发现右mainAxisAlignment属性,意思就是主轴对齐方式,那什么是主轴,什么又是幅轴那。
main轴:如果你用column组件,那垂直就是主轴,如果你用Row组件,那水平就是主轴。
cross轴:cross轴我们称为幅轴,是和主轴垂直的方向。比如Row组件,那垂直就是幅轴,Column组件的幅轴就是水平方向的。
主轴和幅轴我们搞清楚,才能在实际工作中随心所欲的进行布局。
比如现在我们要把上面的代码,改成垂直方向居中。因为用的是Column组件,所以就是主轴方向,这时候你要用的就是主轴对齐了。

三、Stack:层叠布局

Stack:层叠布局。层叠顾名思义需要两个以上的组件才能进行。
Stack的属性列表如下:

Stack({
    Key key,
    this.alignment = AlignmentDirectional.topStart,
    this.textDirection,
    this.fit = StackFit.loose,
    this.overflow = Overflow.clip,
    List<Widget> children = const <Widget>[],
  })
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    //定义一个Stack组件,再放入到Body中
    var stack = new Stack(
      children: <Widget>[
        new CircleAvatar( //圆形头像
          backgroundImage: new NetworkImage('https://aod-image-material.cdn.bcebos.com/5/pic/610fdd209d7aa5d5e0d909f0af85d0e4.jpg'),
          radius: 100.0,
        ),
        new Container(
          decoration: new BoxDecoration(
            color: Colors.lightBlue,
          ),
          padding: EdgeInsets.all(5.0),
          child: Text('A Beauty'),
        )
      ],
    );

    return MaterialApp (
      title: 'Row Widget Demo',
      home: Scaffold(
        appBar: new AppBar(
          title: new Text('层叠布局'),
        ),
          body: Center(
            child: stack,
          ),
      ),
    );
  }
}

运行结果如下:


如果想要修改两个组件之间的位置,需要在Stack中修改alignment对齐属性, alignment: const FractionalOffset(0.5, 0.95),组件的对齐方式,x轴和y轴的的数值设置都是0~1之间,如果不设置的话文本组件会和头像组件基于左上角对齐。
修改之后的代码块如下:

var stack = new Stack(
      //组件的对齐方式,x轴和y轴的的数值设置都是0~1之间,如果不设置的话文本组件会和头像组件基于左上角对齐
      alignment: const FractionalOffset(0.5, 0.95),
      children: <Widget>[
        new CircleAvatar( //圆形头像
          backgroundImage: new NetworkImage('https://aod-image-material.cdn.bcebos.com/5/pic/610fdd209d7aa5d5e0d909f0af85d0e4.jpg'),
          radius: 100.0,
        ),
        new Container(
          decoration: new BoxDecoration(
            color: Colors.lightBlue,
          ),
          padding: EdgeInsets.all(5.0),
          child: Text('A Beauty'),
        ),
      ],
    );

修改之后的运行结果:


Positioned属性

上面示例是两个组件之间的层叠布局,如果是三个组件布局该怎么办呢?
这个时候需要使用Positioned组件来修改组件之间的位置。

const Positioned({
    Key key,
    this.left,
    this.top,
    this.right,
    this.bottom,
    this.width,
    this.height,
    @required Widget child,
  })

代码示例如下:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    //定义一个Stack组件,再放入到Body中
    var stack = new Stack(
      //组件的对齐方式,x轴和y轴的的数值设置都是0~1之间,如果不设置的话文本组件会和头像组件基于左上角对齐
      alignment: const FractionalOffset(0.5, 0.95),
      children: <Widget>[
        new CircleAvatar( //圆形头像
          backgroundImage: new NetworkImage('https://aod-image-material.cdn.bcebos.com/5/pic/610fdd209d7aa5d5e0d909f0af85d0e4.jpg'),
          radius: 100.0,
        ),
        new Positioned(
          top: 10.0,
          left: 60.0,
          child: new Text('A Beauty',
          style: TextStyle(
            fontSize: 25,
            color: Colors.red,
            backgroundColor: Colors.lightBlue,
          ),),
        ),
        new Positioned(
          bottom: 10.0,
          right: 40.0,
          child: new Text('18 years',
          style: TextStyle(
            fontSize: 30,
            color: Colors.yellow,
            backgroundColor: Colors.lightBlue,
          ),),
        ),
      ],
    );

运行结果如下:


四、Card:卡片布局

卡片布局的属性列表如下:

const Card({
    Key key,
    this.color,
    this.elevation,
    this.shape,
    this.borderOnForeground = true,
    this.margin,
    this.clipBehavior,
    this.child,
    this.semanticContainer = true,
  })

示例代码:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var card = new Card(
      child: Column(
        children: <Widget>[
          ListTile(
            title: Text('Amy', style: TextStyle(fontWeight: FontWeight.w400),),
            subtitle: Text('18 years'),
            leading: new Icon(Icons.account_box,color: Colors.lightBlue,),
          ),
          new Divider(),//添加分割线
          ListTile(
            title: Text('Bob', style: TextStyle(fontWeight: FontWeight.w400),),
            subtitle: Text('20 years'),
            leading: new Icon(Icons.account_box,color: Colors.lightBlue,),
          ),
          new Divider(),//添加分割线
          ListTile(
            title: Text('Cola', style: TextStyle(fontWeight: FontWeight.w400),),
            subtitle: Text('10 years'),
            leading: new Icon(Icons.account_box,color: Colors.lightBlue,),
          )
        ],
      ),
    );
    return MaterialApp (
      title: 'Row Widget Demo',
      home: Scaffold(
        appBar: new AppBar(
          title: new Text('层叠布局'),
        ),
          body: Center(
            child: card,
          ),
      ),
    );
  }
}

运行结果如下:


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,188评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,464评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,562评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,893评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,917评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,708评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,430评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,342评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,801评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,976评论 3 337
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,115评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,804评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,458评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,008评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,135评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,365评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,055评论 2 355

推荐阅读更多精彩内容

  • 这是16年5月份编辑的一份比较杂乱适合自己观看的学习记录文档,今天18年5月份再次想写文章,发现简书还为我保存起的...
    Jenaral阅读 2,762评论 2 9
  • 1. 简介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的...
    笨鸟慢飞阅读 5,523评论 0 4
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,457评论 0 13
  •   JavaScript 与 HTML 之间的交互是通过事件实现的。   事件,就是文档或浏览器窗口中发生的一些特...
    霜天晓阅读 3,495评论 1 11
  • 饭后下楼去上班,在电梯内,有一爷爷奶奶送孙子去上幼儿园,孩子泪汪汪的,“我不去上学”,在9楼又上了一个学生,他们是...
    星之梦lyx阅读 137评论 0 0