英文:
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"),
)
],
),
);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论