简体   繁体   English

将具有自定义形状的 Flutter 小部件完美地放置在另一个小部件中

[英]Place Flutter widget with custom shape perfectly in another widget

I would like to display the frame of a smartphone (SVG Image) and display any widgets in it that should not go beyond the frame of the smartphone.我想显示智能手机的框架(SVG 图像)并在其中显示不应 go 超出智能手机框架的任何小部件。

图片

In the image above I have the frame in a Stack and on top a ListView with Cards as child widget.在上图中,我在Stack中有框架,在ListView顶部有Cards作为子小部件。 The frame must be below the child widget for gestures to work.框架必须位于子小部件下方才能使手势起作用。 The ListView 's width is limited with a SizedBox with hardcoded width, but with that it's not responsive. ListView的宽度受到硬编码宽度的SizedBox的限制,但它没有响应。 On a slightly smaller screen, the ListView exceeds the frame (as you can see in the image).在稍小的屏幕上, ListView超出了框架(如图所示)。 How can I make sure that the child widget always stays in the frame even if the screen size changes.即使屏幕尺寸发生变化,如何确保子小部件始终留在框架中。

I also tried using ClipPath but I couldn't fit it perfectly into the frame.我也尝试过使用ClipPath ,但我无法将它完美地放入框架中。 But I think ClipPath is needed because I want to place other child widgets in the frame eg Video that should cover all the space in the frame.但我认为ClipPath是必需的,因为我想在框架中放置其他子小部件,例如应该覆盖框架中所有空间的视频。

To make a widget responsive you can use MediaQuery.of(context).size.width which takes the width of your screen in pixels, if you multiply for example by 0.7 it will occupy 70% of your screen.要使小部件具有响应性,您可以使用MediaQuery.of(context).size.width以像素为单位获取屏幕宽度,例如,如果乘以 0.7,它将占据屏幕的 70%。

//square widget 1:1
SizedBox(
  child: SvgPicture.asset('lib/your.svg'),
  width: MediaQuery.of(context).size.width * 0.7,
  height: MediaQuery.of(context).size.width * 0.7,
),

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

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