简体   繁体   English

Flutter,访问父容器widget的padding值

[英]Flutter, Access the padding value of parent container widget

I have a GridView widget nested inside of a SizedBox widget nested inside of a Container widget我有一个 GridView 小部件嵌套在一个 SizedBox 小部件内,该小部件嵌套在一个 Container 小部件内

    Container(
      padding: EdgeInsets.all(10),
      alignment: Alignment(0, 0),
      child: SizedBox(
        width: min(MediaQuery.of(context).size.width,
                MediaQuery.of(context).size.height) -
            20,
        height: min(MediaQuery.of(context).size.width,
                MediaQuery.of(context).size.height) -
            20,
        child: GridView.count(
          padding: EdgeInsets.all(0),
          crossAxisCount: sqrt(tiles.length).round(),
          mainAxisSpacing: 5,
          crossAxisSpacing: 5,
          children: tiles,
          shrinkWrap: true,
        ),
      ),
    ),

The Container Widget has an edge inset of 10, so in the SizedBox I want the width and height to be the min screen width/height minus this edge inset (so minus 20 since it's on each side). Container Widget 的边缘插入为 10,因此在 SizedBox 中,我希望宽度和高度为最小屏幕宽度/高度减去此边缘插入(因此为负 20,因为它在每一侧)。

How can I read values of a parent widget so that I can know it's edgeInset?如何读取父小部件的值以便知道它是 edgeInset? Can a MediaQuery accomplish this through the context? MediaQuery 可以通过上下文完成此操作吗?

I know that I can just store the edge inset as a variable earlier on and use that in both places, but as I am trying to learn Flutter I am more curious if there is a way to read the values/properties of parent widgets so that their children can reference them on the fly.我知道我可以更早地将边缘插入作为变量存储并在两个地方使用它,但是当我尝试学习 Flutter 时,我更好奇是否有办法读取父小部件的值/属性,以便他们的孩子可以随时参考它们。

If you provide some Widget a global key如果你提供一些 Widget 一个全局键

final _key = GlobalKey();最终_key = GlobalKey();

Table(
   key: _key,
   children: _getTableRows(),
);

then you can calculate the size of the widget like this:然后你可以像这样计算小部件的大小:

void _getSize(_) {
final RenderBox _boardRender = _key.currentContext.findRenderObject();
final boardSize = MediaQuery.of(_key.currentContext).size;
final boardOffset = _boardRender.localToGlobal(Offset.zero);

//print('$boardSize:$boardOffset:$cellSize');

} }

You may look at https://api.flutter.dev/flutter/widgets/LayoutBuilder-class.html你可以看看https://api.flutter.dev/flutter/widgets/LayoutBuilder-class.html

Container has a somewhat complicated behaviour so the answer to your question would depend on who the parent of the container is. Container行为有些复杂,因此您的问题的答案将取决于container的父级是谁。From the documentation :从文档

Container tries, in order: to honor alignment, to size itself to the child, to honor the width, height, and constraints, to expand to fit the parent, to be as small as possible.容器尝试的顺序是:遵守对齐,根据子级调整自身大小,遵守宽度、高度和约束,扩展以适应父级,尽可能小。

For example: If you place your container in the body paramenter of a Scaffold and remove the SizedBox then GridView will fill the screen and will have the padding you specified.例如:如果您将容器放在Scaffoldbody参数中并移除SizedBoxGridView将填充屏幕并具有您指定的填充。 (In which case you don't even need Container and you can just use Padding widget instead.) (在这种情况下,您甚至不需要Container而可以使用Padding小部件。)

More broadly speaking Flutter works in a different paradigm where you don't need to "know" specifics of your parent as you potentialy can place any widget in any other widget.更广泛地说,Flutter 在不同的范式中工作,您不需要“知道”父级的细节,因为您可以将任何小部件放置在任何其他小部件中。

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

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