Flutter——最详细Stack(堆叠布局)使用教程

本文详细介绍了Flutter中的Stack组件,用于在屏幕上堆叠多个子组件,并通过Alignment属性进行位置调整。同时讨论了Positioned组件,用于更精确地定位内部子组件,以及如何使用clipBehavior属性处理内容裁剪。此外,还提到了Align组件,它能方便地控制子组件的对齐方式。示例代码展示了不同属性设置下的布局效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.Stack简介:

可以容纳多个组件,以叠加的方式摆放子组件,后者居上。拥有Alignment属性,可以与Positioned组件联合使用,精准并有效摆放。同AndroidFramLayout布局相似。

属性作用
alignment子组件摆放的位置
clipBehavior剪辑小部件的内容
  • 使用场景:

比如开发中需要用户头像上面添加一个特殊标识,就需要是用到堆叠布局;

在这里插入图片描述

创建一个堆叠布局


class CustomStack extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var yellowBox = Container(
      color: Colors.yellow,
      height: 100,
      width: 100,
    );

    var redBox = Container(
      color: Colors.red,
      height: 90,
      width: 90,
    );

    var greenBox = Container(
      color: Colors.green,
      height: 80,
      width: 80,
    );

    return Container(
      width: 200,
      height: 120,
      color: Colors.grey.withAlpha(33),
      child: Stack(
        textDirection: TextDirection.rtl,
        fit: StackFit.loose,
        alignment: Alignment.topRight,
        children: <Widget>[yellowBox, redBox, greenBox],
      ),
    );
  }
}

在这里插入图片描述

属性Alignment.center

在这里插入图片描述

属性Alignment.bottomLeft

在这里插入图片描述

2.组件Positioned

精准堆叠布局内部,子组件的位置与排列;

属性作用
left向左距离
top向上距离
right向右距离
bottom向下距离
Stack(
        textDirection: TextDirection.rtl,
        fit: StackFit.loose,
        alignment: Alignment.bottomLeft,
        children: <Widget>[
          yellowBox,
          redBox,
          Positioned(
            bottom: 10,
            right: -30,
            child: greenBox,
          )
        ],
      )

在这里插入图片描述

可以看到绿色组件有一部分被裁剪调了,是因为没有使用clipBehavior属性,接下来我们来看Clip.none属性效果

属性clipBehavior: Clip.none

Stack(
        textDirection: TextDirection.rtl,
        fit: StackFit.loose,
        alignment: Alignment.bottomLeft,
        clipBehavior: Clip.none,
        children: <Widget>[
          yellowBox,
          redBox,
          Positioned(
            bottom: 10,
            right: -30,
            child: greenBox,
          )
        ],
      ),

加上该属性之后我们可以看到,绿色组件超出的部分也显示出来了。该属性的意义也就一目了然。

3.组件Align

精准控制子组件的位置,同Positioned相似。

创建一个带Align组件的样式

Stack(
        textDirection: TextDirection.rtl,
        fit: StackFit.loose,
        alignment: Alignment.bottomLeft,
        clipBehavior: Clip.none,
        children: <Widget>[
          Align(
            alignment: const Alignment(0, 0),
            child: redBox,
          ),
        ],
      )

在这里插入图片描述

可以看到该布局显示再正中间,Alignment(0, 0) 该属性分为 x 轴跟 y轴,范围值都是 -11 之间;

看Alignment(0, 1)
在这里插入图片描述

可以看到当y等于1时,红色组件排列再最底部;

看 Alignment(-0.5, -0.5)
在这里插入图片描述

当x等于 -0.5 时,很明显组件位于横轴-1到0的中间;
当y等于 -0.5 时,很明显组件位于主轴-1到0的中间;
总结x值的大小是根据横轴排列,y值的大小是根据主轴排列;

属性 Alignment(1, 4)
在这里插入图片描述

当x与y大于1时,子组件并没有被裁剪。说明使用Align属性并不受clipBehavior: Clip.none影响;

项目地址

https://github.com/z244370114/flutter_demo

### PyQt5实现堆叠布局翻页效果 在PyQt5中,`QStackedWidget` 控件用于管理多个子窗口部件,并一次只显示其中一个。这种特性非常适合用来创建具有不同页面的应用程序,用户可以通过按钮或其他交互方式来切换这些页面。 下面是一个简单的例子,展示了如何利用 `QPushButton` 和 `QStackedWidget` 来构建一个多页面应用程序并实现翻页的效果: ```python import sys from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QLabel, QStackedWidget class StackedExample(QWidget): def __init__(self): super().__init__() layout = QVBoxLayout(self) self.stack = QStackedWidget() # 创建三个页面实例 page1 = QWidget() page2 = QWidget() page3 = QWidget() # 设置每个页面的内容 label1 = QLabel("Page 1", page1) label2 = QLabel("Page 2", page2) label3 = QLabel("Page 3", page3) # 添加到stack widget里边去 self.stack.addWidget(page1) self.stack.addWidget(page2) self.stack.addWidget(page3) # 创建导航按钮 prev_button = QPushButton('Previous') next_button = QPushButton('Next') # 绑定信号槽函数 prev_button.clicked.connect(lambda: self.change_page(-1)) next_button.clicked.connect(lambda: self.change_page(+1)) # 构建整体布局 button_layout = QHBoxLayout() button_layout.addWidget(prev_button) button_layout.addWidget(next_button) layout.addLayout(button_layout) layout.addWidget(self.stack) # 初始化UI组件位置和大小 self.setGeometry(300, 300, 280, 170) self.setWindowTitle('Stacked Widget Example') def change_page(self, direction): current_index = self.stack.currentIndex() new_index = max(min(current_index + direction, self.stack.count()-1), 0) self.stack.setCurrentIndex(new_index) if __name__ == '__main__': app = QApplication(sys.argv) ex = StackedExample() ex.show() sys.exit(app.exec_()) ``` 上述代码定义了一个名为 `StackedExample` 的类继承自 `QWidget`, 使用垂直布局 (`VBoxLayou`) 安排两个水平排列的按钮以及一个 `QStackedWidget`. 当点击 "Previous" 或者 "Next" 按钮时,会触发相应的事件处理器方法 `change_page()` 改变当前展示的页面索引[^1].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

怀君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值