将子部件对齐到一个定位的子部件。

huangapple go评论98阅读模式
英文:

Align child widgets to a positioned child widget

问题

需要将子部件(w1)相对于其父部件定位(在示例中查看30%处的白色框),并将另外两个子部件(w2、w3)定位在右侧和左侧。

定位的子部件(w1)是锚点,应保持在30%位置,不受w2和w3的宽度影响,而其他子部件应相应地定位。

这是我要实现的效果:

将子部件对齐到一个定位的子部件。

我尝试使用Stack部件作为父部件。
问题在于我不能将定位子部件放在30%处,因为它只接受const值。

我还尝试将它们放在FractionalOffset中的Container中 - 这适用于单个子部件,但我不能将另一个子部件添加到此Container中。

Container(
  height: 50,
  color: Colors.blue[200],
  alignment: const FractionalOffset(0.3, 0),
  child: Text("50", style: const TextStyle(fontSize: 16, color: Colors.black))
)
英文:

I need to position a child widget (w1) at a relative position to its parent (in the example see the white box at 30%), and to position two other child widgets (w2, w3) to the right and left.

The positioned child (w1) is the anchor and should remain in 30%, regardless of w2 and w3 width, while the other child widgets should positioned accordingly.

Here's what I'm trying to achieve:

将子部件对齐到一个定位的子部件。

I tried to use Stack widget as the parent
The problem is I can't place the position child at 30%, as it only excepts const values

I also tried to placed them in Container with FractionalOffset - it works for single children, but I can't add another children to this Container.

 Container(
      height: 50,
      color: Colors.blue[200],
      alignment: const FractionalOffset(0.3, 0),
      child: Text("50",style: const TextStyle(fontSize: 16, color: Colors.black))),

答案1

得分: 1

你可以使用CompositedTransformTarget小部件。

class TestF133 extends StatelessWidget {
  const TestF133({super.key});

  @override
  Widget build(BuildContext context) {
    final LayerLink link = LayerLink();
    return Scaffold(
      backgroundColor: Colors.deepPurple,
      body: Stack(
        children: [
          Align(
            alignment: Alignment(.3, 0),
            child: CompositedTransformTarget(
              link: link,
              child: Container(
                height: 50,
                width: 50, // 你可以处理这些
                color: Colors.white,
              ),
            ),
          ),
          CompositedTransformFollower(
            link: link,
            followerAnchor: Alignment.centerLeft,
            targetAnchor: Alignment.centerRight,
            child: Text("动态右侧文本"),
          ),
          CompositedTransformFollower(
            link: link,
            followerAnchor: Alignment.centerRight,
            targetAnchor: Alignment.centerLeft,
            child: Text("动态左侧文本"),
          )
        ],
      ),
    );
  }
}
英文:

You can use CompositedTransformTarget widget.

class TestF133 extends StatelessWidget {
  const TestF133({super.key});

  @override
  Widget build(BuildContext context) {
    final LayerLink link = LayerLink();
    return Scaffold(
      backgroundColor: Colors.deepPurple,
      body: Stack(
        children: [
          Align(
            alignment: Alignment(.3, 0),
            child: CompositedTransformTarget(
              link: link,
              child: Container(
                height: 50,
                width: 50, // you can handle theses
                color: Colors.white,
              ),
            ),
          ),
          CompositedTransformFollower(
            link: link,
            followerAnchor: Alignment.centerLeft,
            targetAnchor: Alignment.centerRight,
            child: Text("dynamic right Text"),
          ),
          CompositedTransformFollower(
            link: link,
            followerAnchor: Alignment.centerRight,
            targetAnchor: Alignment.centerLeft,
            child: Text("dynamic left Text"),
          )
        ],
      ),
    );
  }
}

huangapple
  • 本文由 发表于 2023年2月13日 23:42:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438112.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定