Flutter Row Column MainAxisAlignment Expanded

博客地址:flutterall.com

RowColumn类似于Android里面的线性布局,只不过分别是水平方向和垂直方向。

Row

水平布局Widget

构造方法

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>[],
  })

Demo


class FlutterRow extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Row(
        children: <Widget>[Text("One"), Text("Two"), Text("Three")],
      ),
    );
  }
}

Row

Column

垂直布局Widget

构造方法

跟Row基本一致


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>[],
  })

Column

Column Row属性

MainAxisAlignment(布局的主轴对齐约束)

enum MainAxisAlignment {
  start,
  end,
  center,
  spaceBetween,
  spaceAround,
  spaceEvenly,
}

MainAxisAlignment start center end图示

看代码说话:

Code


Row(
        children: <Widget>[Text("One"), Text("Two"), Text("Three")],
      )

Row默认

Row MainAxisAlignment.start center end
Row(
        mainAxisAlignment: MainAxisAlignment.end,
        children: <Widget>[Text("One"), Text("Two"), Text("Three")],
      )

MainAxisAlignment.start center end

Column MainAxisAlignment.start center end

Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[Text("One"), Text("Two"), Text("Three")],
      )

start
center

end

MainAxisAlignment.spaceBetween spaceAround spaceEvenly

spaceBetween spaceAround spaceEvenly

Expanded

如果我们在布局的时候,想对Widget进行等分,或者按比例占空间。就可以使用Expanded,使用这个Widget时候,会对非Expanded的子child进行布局,剩下的控件区域,留给Expanded的子child。
Expanded构造

Expanded({
   Key key,
   int flex = 1,//默认一等分。类似LinearLayout中的weight=1
   @required Widget child,
 }) 

Demo

class FlutterRow extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Column(
          children: <Widget>[
            Expanded(child: Text("One")),
            Expanded(child: Text("Two")),
            Expanded(
              child: Text("Three"),
            )
          ]
      ),
    );
  }
}


Expanded处理前后(一等分)
Expanded flex属性
类似于LinearLayout的weight

class FlutterRow extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Column(
        children: <Widget>[
          Expanded(child: Container(color: Colors.brown,),flex: 1,),
          Expanded(child: Container(color: Colors.blue,), flex: 1,),
          Container(color: Colors.amberAccent,constraints: BoxConstraints(minHeight: 100, maxHeight: 100),)//未参与Expanded,
// 除了此Widget的空余区域均留给Expanded修饰的Colors.brown & Colors.blue,进行1:1等分
        ],
      ),
    );
  }
}

Expanded flex

本地代码地址

flutterall

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值