简体   繁体   English

Position 一个widget,在一个stack里面,但是根据widget的中心

[英]Position a widget, inside a stack, but according to the center of the widget

I want to position a widget, CustomWidget , but at the specified position, ie, top, left values, in the Positioned widget, I want there to be the center of my CustomWidget , instead of the top-left corner of my CustomWidget .我想 position 一个小部件CustomWidget ,但在指定的 position,即顶部,左侧值,在Positioned小部件中,我希望有我的CustomWidget的中心,而不是我的CustomWidget的左上角。

I want something like this:-我想要这样的东西:-

Stack(
  children: [
    Positioned(
      left: x,
      top: y,
      alignment: Alignment.center, //Alignment of the child widget, such that at (x, y) there is the center of the widget.
      child: CustomWidget(),
    ),
  ],
);

Here is the type of UI I am trying to make, I am trying to make both cases:-这是我正在尝试制作的 UI 类型,我正在尝试制作两种情况:- 在此处输入图像描述

CustomWidget does not have any specific type, I am currently trying to make a general method, It might be a simple Text Widget with some variable value, or maybe a Widget with a Row of Image , Divider and Text . CustomWidget没有任何特定类型,我目前正在尝试制作一个通用方法,它可能是具有一些变量值的简单Text Widget,或者可能是具有ImageDividerText Row的 Widget。 I am basically using CustomWidget as a kind of popup, which will be used in case of some extra information, and also as a stamp over a widget, wherever the user clicks, in both cases, I cannot be certain of the size.我基本上将CustomWidget用作一种弹出窗口,将在一些额外信息的情况下使用,也用作小部件上的标记,无论用户单击何处,在这两种情况下,我都无法确定大小。

Note:- I do want to position the CustomWidget at a specified offset in the stack, but want that there should be the center of the CustomWidget instead of the top-left corner of CustomWidget .注意:- 我确实希望 position CustomWidget在堆栈中的指定偏移量处,但希望应该有 CustomWidget 的中心而不是CustomWidgetCustomWidget

You don't need to use the Positioned widget.您不需要使用Positioned小部件。 Try using the Align widget, remember to add alignment: Alignment.center, to center it.尝试使用Align小部件,记得添加alignment: Alignment.center,使其居中。

I have developed a package , with many helpful components (basically the components, which I use often, in all my projects, so to prevent writing the same thing everywhere ).我开发了一个package ,其中包含许多有用的组件(基本上是我在所有项目中经常使用的组件,以防止到处写相同的东西)。 So there I have added this new Widget named PositionedAlign this work according to the above question.所以我根据上面的问题添加了这个名为PositionedAlign的新Widget I hope this helps anyone who might need it.我希望这可以帮助任何可能需要它的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM