Flutter 基础控件学习(一)

2019-02-02  本文已影响9人  过期的薯条

1.引言

Dart学完了,就开始正式入手学习Flutter。先学习基础控件,等熟练写界面了,在接着学习后面的知识。

2. 正题

此时Center控件的宽高是铺满全屏的

class MyHomePage extends StatelessWidget {
  var container = new Center(
    child: new Container(
      color: Colors.red,
      alignment: Alignment.centerLeft,
      width: 100,
      height: 100,
    ),
  );
  @override
  Widget build(BuildContext context) {
    return container;
  }
}

此时Center的控件宽高是等于子宽高乘以相应的因子:

class MyHomePage extends StatelessWidget {
  var container = new Center(
    widthFactor: 2,
    heightFactor: 2,
    child: new Container(
      color: Colors.red,
      alignment: Alignment.centerLeft,
      width: 100,
      height: 100,
    ),
  );
  @override
  Widget build(BuildContext context) {
    return container;
  }
}
class MyHomePage extends StatelessWidget {
  var container = new Container(
    child: new Container(
      alignment: Alignment(10, 50),
      decoration: BoxDecoration(color: Colors.blue,border: BorderDirectional(top: BorderSide(color: Colors.red,width: 10,)
          ,bottom: BorderSide(color: Colors.red,width: 10,)
      )),
      height: 100,
    ),
  );
  @override
  Widget build(BuildContext context) {
    return container;

上面的例子为Containers 设置了上下边:


image.png

属性四:transform变换
旋转平移放大等都是通过transform参数控制。

image.png

1.Row控件的宽度取决于mainAxisSize 属性:当mainAxisSize==MainAxisSize.max Row的宽度将铺满剩下的空间;当mainAxisSize==MainAxisSize.min的时候,Row的宽度是子view宽度的总和

2.Row控件的高度 是子View中最高的高度。假设副轴设置了CrossAxisAlignment.stretch 那么Row的高度将填满剩余空间

Row控件分主轴和副轴。通过mainAxisAlignment,crossAxisAlignment 可以设置子View在主副轴的分布。

下面代码是将Container放到屏幕正中央:

class MyHomePage3 extends StatelessWidget {
  var s = new Column(
    children: <Widget>[
      new Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          new Container(
            alignment: Alignment.center,
            color: Colors.red,
            width: 100,
            height: 100,
          ),
        ],
      ),
    ],
    mainAxisAlignment: MainAxisAlignment.center,
  );

  @override
  Widget build(BuildContext context) {
    return s;
  }
}
image.png
class MyHomePage4 extends StatelessWidget {
  var s = new Flex(
    direction: Axis.horizontal,
    mainAxisAlignment: MainAxisAlignment.center,
    mainAxisSize: MainAxisSize.max,
    children: <Widget>[
      new Container(
        color: Colors.red,
        width: 100,
        height: 100,
      )

    ],
  );
  @override
  Widget build(BuildContext context) {
    return s;
  }
}
上一篇下一篇

猜你喜欢

热点阅读