Flutter不常用组件----IntrinsicHeight,IntrinsicWidth

终于有勇气做一些不常用组件的博文,最近行情不景气,大家的工作应当也是不少,笔者也是一样。之前筹备了好久想着做一些不常用的组件分享,一直没付诸于行动。现在打算开始慢慢做起来,各位粉丝如果有是一些使用场景代码繁杂的困惑(考虑是不是有系统组件自己不了解的),又或者已经知道一些组件不太清楚具体使用场景等,欢迎评论或者私信我,我们一起解答学习。

前言

        现实中有没有遇到在一个Row组件中存放多个元素。某一个元素的高度希望根据另一个元素的高度自适应的场景?比如一个可换行的文本元素,另一个是一个高度设计需要“顶天立地”的分割线。

        你会不会通过文本计算来判定高度?还是加载完成之后通过key来获取依赖组件的高度控制分割线的高度展示。现在我们来介绍一个IntrinsicHeight组件来解决这种尴尬的问题。

官方介绍


/// A widget that sizes its child to the child's intrinsic height.
///
/// This class is useful, for example, when unlimited height is available and
/// you would like a child that would otherwise attempt to expand infinitely to
/// instead size itself to a more reasonable height.
///
/// The constraints that this widget passes to its child will adhere to the
/// parent's constraints, so if the constraints are not large enough to satisfy
/// the child's maximum intrinsic height, then the child will get less height
/// than it otherwise would. Likewise, if the minimum height constraint is
/// larger than the child's maximum intrinsic height, the child will be given
/// more height than it otherwise would.
///
/// This class is relatively expensive, because it adds a speculative layout
/// pass before the final layout phase. Avoid using it where possible. In the
/// worst case, this widget can result in a layout that is O(N²) in the depth of
/// the tree.
///
/// See also:
///
///  * [Align], a widget that aligns its child within itself. This can be used
///    to loosen the constraints passed to the [RenderIntrinsicHeight],
///    allowing the [RenderIntrinsicHeight]'s child to be smaller than that of
///    its parent.
///  * [Column], which when used with [CrossAxisAlignment.stretch] can be used
///    to loosen just the height constraints that are passed to the
///    [RenderIntrinsicHeight], allowing the [RenderIntrinsicHeight]'s child's
///    height to be smaller than that of its parent.
///  * [The catalog of layout widgets](https://flutter.dev/widgets/layout/).
class IntrinsicHeight extends SingleChildRenderObjectWidget {
  /// Creates a widget that sizes its child to the child's intrinsic height.
  ///
  /// This class is relatively expensive. Avoid using it where possible.
  const IntrinsicHeight({ super.key, super.child });

  @override
  RenderIntrinsicHeight createRenderObject(BuildContext context) => RenderIntrinsicHeight();
}

翻译如下:
一个小部件,将其子部件的大小调整为子部件的固有高度。

这个类很有用,例如,当可以使用无限高度,并且您想要一个子节点,否则它会尝试无限扩展,而不是将自身大小调整为更合理的高度时。

此小部件传递给其子部件的约束将遵循父部件的约束,因此,如果约束不够大,无法满足子部件的最大固有高度,则子部件获得的高度将低于其他情况。同样,如果最小高度约束大于子节点的最大固有高度,则子节点将获得比其他情况下更高的高度。

这个类的成本相对较高,因为它在最终布局阶段之前添加了一个推测性布局通道。尽可能避免使用它。在最坏的情况下,这个小部件可能导致树的布局深度为0 (N2)。

从官方解释里面可以看出它使用了一个预测醒的布局通道,谨慎使用。他是一个比较昂贵的组件,尽可能避免使用它。

如何使用

//外层,高度根据卡片高度决定
Container(
//继承最外层的高度
      child: IntrinsicHeight(
        child: Row(
          children: [
            Container(
//设置高度无限大(会根据右侧高度来约束展示喜爱那个会对高度)
                height: double.infinity,
                child: "高度依赖部分"),
            Container(child: Text("内容不固定,高度内部不固定,变化部分")),
          ],
        ),
      ),
    );

IntrinsicWidth类似,宽度继承最外层的宽度容器组件。

  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值