Flutter组件——Stack,Card

Stack

Stack也是一个布局组件,它用于有子组件堆叠的场景,而且当我们使用Positioned组进行辅助的时候我们可以是层叠效果变得非常灵活。

  • alignment

    它接受一个AlignmentDirectional对象,它用于控制non-positioned组件在Stack组件中的位置。默认为topStart(左上角)

  • overflow

    它接受一个Overflow对象,用于控制溢出部分的显示效果

  • textDirection

    用于控制子组件排列方向,接受一个TextDirection对象有ltr,rtl这些值。

  • fit

    用于控制如何设置non-positioned节点尺寸,默认为loose。接受一个StackFit对象,里面有loose,expand,passthrough三个枚举常量。

  • children

    放置子元素组件。其中可以放置positioned组件Positioned组件中有left,right,top,bottom,width,height等属性,上下左右用来控制在Stack的位置的,这个组件不受alignment属性的影响。

  • 源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Stack({
Key key,
this.alignment = AlignmentDirectional.topStart,
this.textDirection,
this.fit = StackFit.loose,
this.overflow = Overflow.clip,
List<Widget> children = const <Widget>[],
}) : super(key: key, children: children);

const Positioned({
Key key,
this.left,
this.top,
this.right,
this.bottom,
this.width,
this.height,
@required Widget child,
}) : assert(left == null || right == null || width == null),
assert(top == null || bottom == null || height == null),
super(key: key, child: child);

Card

这是一个容器组件,就是卡片的样子

  • 源代码

1
2
3
4
5
6
7
8
9
10
11
12
13
const Card({
Key key,
this.color,
this.elevation,
this.shape,
this.borderOnForeground = true,
this.margin,
this.clipBehavior,
this.child,
this.semanticContainer = true,
}) : assert(elevation == null || elevation >= 0.0),
assert(borderOnForeground != null),
super(key: key);
  • color

    接受一个Color对象,用来设置卡片的颜色。

  • elevation

    控制z轴的高度,就是阴影效果,接受一个double值。

  • shape

    控制卡片的形状,接受一个shapeBorder对象,我们可以将RoundedRectangleBorder对象赋值给它,这是事项圆角卡片的对象。

  • borderOnForeground

    接受一个布尔值,默认为true,当设置为true的时候,边框渲染会在子元素前面,false就相反。

  • margin

    用于控制外边距,接受一个EdgeInsetsGeometry对象。

  • semanticContainer

    接受一个bool,将此标志设置为true将尝试将所有子语义合并到此节点中。将此标志设置为false会强制所有子语义节点显式。这是文档里面的解释,我不太理解,但是官方说当你子元素是多种类型的时候最好设置为false,其默认也是false。

-------------本文结束感谢阅读-------------