Flutter Stack

277 阅读1分钟
child: Stack(
  //stack的大小是根据没有包裹Positioned组件,中的最大组件来确定大小的。
  //确定完大小后,再根据Positioned来布局
  //所有的都包裹Positioned后,stack的size就是尽量大。
  children: [
    Text("ajsdlkfjalkjk\njjfljafkaljsdkfllakdjkl"),
    Positioned(left: 0,top: 0,child: FlutterLogo(size: 100,)),
    Container(width: 20, height: 20, color: Colors.red,)
  ],
),
simulator_screenshot_1AAC47E0-A2D1-40B9-A21A-490AC34C3467.png
children: [
  Positioned(top: 0, right: 0,child: Text("ajsdlkfjalkjk\njjfljafkaljsdkfllakdjkl")),
  Positioned(left: 0,top: 0,child: FlutterLogo(size: 100,)),
  Positioned(right: 0,bottom:0,child: Container(width: 20, height: 20, color: Colors.red,))
],
simulator_screenshot_8536EF38-FFC8-457A-ADCF-A9957D52A9A6.png
child: Stack(
  fit: StackFit.expand,//是放大了子组件的约束。stack会根据子组件的约束方法
  //stack的大小是根据没有包裹Positioned组件,中的最大组件来确定大小的。
  //确定完大小后,再根据Positioned来布局
  //所有的都包裹Positioned后,stack的size就是尽量大。
  children: [
    Container(width: 80, height: 80, color: Colors.red,),

    Text("ajsdlkfjalkjk\njjfljafkaljsdkfllakdjkl"),
    FlutterLogo(size: 100,),
  ],
),
simulator_screenshot_FA3CBC86-ACA5-4F26-BC58-2B0ABA0D888A.png
fit: StackFit.loose,//宽松约束,就是按照组件中最大的子组件来进行约束
simulator_screenshot_45C84461-5439-44E8-8669-C2E9AC04108B.png
child: Container(
  width: 200,
  height: 200,
  color: Colors.orange,
  child: Stack(
    fit: StackFit.passthrough,//会将父组件的约束,传递到子组件
    //stack的大小是根据没有包裹Positioned组件,中的最大组件来确定大小的。
    //确定完大小后,再根据Positioned来布局
    //所有的都包裹Positioned后,stack的size就是尽量大。
    children: [
      Container(width: 80, height: 80, color: Colors.red,),//自身的宽高失效

      Text("ajsdlkfjalkjk\njjfljafkaljsdkfllakdjkl"),
      FlutterLogo(size: 100,),//自身的宽高失效。
    ],
  ),
),
simulator_screenshot_1F37DCED-D4C3-4C67-AD2D-04D8DA4CDC25.png
clipBehavior: Clip.none,//裁剪,超出部分不会接收用户的点击事件
clipBehavior: Clip.hardEdge,//裁剪
Positioned(
    left: 10,
    width: 100,
    child: Transform.translate(
        //在使用transform进行位移之后,超出部分并没有裁剪
        offset: const Offset(-20, -20),
        child: Container(
          width: 80,
          height: 80,
          color: Colors.red,
        ))),
simulator_screenshot_1ED98F7D-FFEE-4EA8-90FB-F08EAE4D7499.png