首页 > 解决方案 > 确保 transform.translate 不会离开容器

问题描述

有没有办法确保 Transform.translate 不会超出容器?即使这些价值观在逻辑上把它放在外面?因此,如果容器位于屏幕的中心并且在该容器的 -200 处移动了某些东西,我希望它只显示容器内部的部分 SizedBox 和 ConstrainedBox 似乎没有帮助

我当前的代码是:

return ConstrainedBox(
    constraints: BoxConstraints.tight(Size(400, 400)),
    child: Container(
        width: 400,
        height: 400,
        color: Colors.blue,
        child: Stack(
            children: [
                AnimatedBuilder(
                    animation: animationController,
                    child: // ...,
                    builder: (BuildContext context, Widget child) {
                        return Transform.translate(
                            offset: Offset(positionLeft, positionRight),
                            child: child
                        );
                    },
                ),
                //...
            ]
        )
    )
)

标签: flutterdart

解决方案


显然我找到了一个解决方案,用 ClipRect 而不是 ContrainedBox 包装容器做到了


推荐阅读