[英]flutter place widget on the border of a stack
I am trying to achieve an effect like this:我试图达到这样的效果:
My solution so far is to have a container for the background and the child of that container is a Stack that has an aligned Widget that is offset by half of its width using transform.translate and the LayoutBuilder.到目前为止,我的解决方案是为背景设置一个容器,该容器的子项是一个 Stack,它具有一个对齐的 Widget,使用 transform.translate 和 LayoutBuilder 偏移其宽度的一半。 The code for that looks like this:代码如下所示:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return IconOverlayTest();
}
}
class IconOverlayTest extends StatelessWidget {
const IconOverlayTest({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Container(
width: 200,
color: Colors.red,
child: Stack(
children: [
Align(
alignment: Alignment.centerRight,
child: LayoutBuilder(
builder: (_, constraint) {
final _halfWidth = constraint.maxWidth / 2;
return Transform.translate(
offset: Offset(_halfWidth, 0),
child: Container(
padding: EdgeInsets.all(10),
decoration: BoxDecoration(
color: Colors.white,
shape: BoxShape.circle,
),
child: Icon(Icons.close),
),
);
},
),
)
],
),
),
),
);
}
}
The problem now is that the Layoutbuilder width picks up the width of the parent container and _width thus becomes 100 so I am not sure how to combine LayoutBuilder with transform.translate.现在的问题是 Layoutbuilder 宽度选择了父容器的宽度,因此 _width 变为 100,所以我不确定如何将 LayoutBuilder 与 transform.translate 结合起来。 Is that even a good approach for this sort of problem?对于这类问题,这甚至是一个好方法吗? I tried using positioned inside of the Stack but that would cut off the part that goes outside of the red background.我尝试在 Stack 内部使用定位,但这会切断红色背景之外的部分。
Any help is greatly appreciated!任何帮助是极大的赞赏!
double iconSize = 35;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text('Drag widget on screen'),
),
body: Container(
width: 200,
color: Colors.red,
child: Stack(
clipBehavior: Clip.none,
children: [
Positioned(
left: 200-iconSize,
top: MediaQuery.of(context).size.height/2-(iconSize*1.5),
child: Container(
padding: EdgeInsets.all(10),
decoration: BoxDecoration(
color: Colors.white,
shape: BoxShape.circle,
),
child: Icon(Icons.close, color:Colors.grey, size: iconSize),
),
)
],
),
),
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.