Flutter教学

Flutter(63):Layout组件之IndexedStac

2020-10-22  本文已影响0人  starryxp

Flutter教学目录持续更新中

Github源代码持续更新中

1.IndexedStack介绍

从一个子widget列表中显示单个孩子的Stack,继承自Stack
Stack我们之前已经介绍过了:Flutter(62):Layout组件之Stack、Positioned

2.IndexedStack属性

3. IndexedStack跟Stack区别

4.上代码

  _myStackChildren() {
    return [
      Container(
        width: 50,
        height: 50,
        color: Colors.red,
      ),
      Positioned(
        bottom: 20,
        child: Container(
          width: 80,
          height: 80,
          color: Colors.green,
        ),
      ),
      Container(
        width: 60,
        height: 60,
        color: Colors.cyan,
      ),
      Positioned(
        left: 30,
        top: 80,
        width: 2000,
        height: 20,
        child: Container(
          color: Colors.black,
        ),
      ),
    ];
  }
      body: Container(
        color: Colors.blue,
        child: IndexedStack(
          alignment: AlignmentDirectional.center,
          sizing: StackFit.expand,
          index: 0,
          children: _myStackChildren(),
        ),
      ),
image.png

这里是显示的第0个子组件,大小是50/50;IndexedStack大小是60/60,因为他的大小是由第2个60/60的子组件决定的,Positioned的子组件大小是不会影响到IndexedStack大小的;当然这个情况也是需要在遵循IndexedStack父约束的情况下,IndexedStack首先还是响应父约束的,例如设置父Container宽高为300,那么IndexedStack就是300/300

image.png

那我们再将0,2位置的子组件去掉,只保留Positioned的子组件看看会怎样:

image.png

这样一来IndexedStack撑满了整个父控件Container,这种时候IndexedStack尺寸就是完全由父约束来管理的。

下一节:Layout组件之Flow

Flutter(64):Layout组件之Flow

Flutter教学目录持续更新中

Github源代码持续更新中

上一篇 下一篇

猜你喜欢

热点阅读