1// Copyright 2014 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'dart:ui';
6
7import 'box.dart';
8
9/// Signature for a function that takes a [RenderBox] and returns the [Size]
10/// that the [RenderBox] would have if it were laid out with the given
11/// [BoxConstraints].
12///
13/// [ChildLayoutHelper.dryLayoutChild] and [ChildLayoutHelper.layoutChild] adhere
14/// to this signature.
15typedef ChildLayouter = Size Function(RenderBox child, BoxConstraints constraints);
16
17/// Signature for a function that takes a [RenderBox] and returns the baseline
18/// offset this [RenderBox] would have if it were laid out with the given
19/// [BoxConstraints].
20///
21/// [ChildLayoutHelper.getDryBaseline] and [ChildLayoutHelper.getBaseline] adhere
22/// to this signature.
23typedef ChildBaselineGetter =
24 double? Function(RenderBox child, BoxConstraints constraints, TextBaseline baseline);
25
26/// A collection of static functions to layout a [RenderBox] child with the
27/// given set of [BoxConstraints].
28///
29/// All of the functions adhere to the [ChildLayouter] signature.
30abstract final class ChildLayoutHelper {
31 /// Returns the [Size] that the [RenderBox] would have if it were to
32 /// be laid out with the given [BoxConstraints].
33 ///
34 /// This method calls [RenderBox.getDryLayout] on the given [RenderBox].
35 ///
36 /// This method should only be called by the parent of the provided
37 /// [RenderBox] child as it binds parent and child together (if the child
38 /// is marked as dirty, the child will also be marked as dirty).
39 ///
40 /// See also:
41 ///
42 /// * [layoutChild], which actually lays out the child with the given
43 /// constraints.
44 static Size dryLayoutChild(RenderBox child, BoxConstraints constraints) {
45 return child.getDryLayout(constraints);
46 }
47
48 /// Lays out the [RenderBox] with the given constraints and returns its
49 /// [Size].
50 ///
51 /// This method calls [RenderBox.layout] on the given [RenderBox] with
52 /// `parentUsesSize` set to true to receive its [Size].
53 ///
54 /// This method should only be called by the parent of the provided
55 /// [RenderBox] child as it binds parent and child together (if the child
56 /// is marked as dirty, the child will also be marked as dirty).
57 ///
58 /// See also:
59 ///
60 /// * [dryLayoutChild], which does not perform a real layout of the child.
61 static Size layoutChild(RenderBox child, BoxConstraints constraints) {
62 child.layout(constraints, parentUsesSize: true);
63 return child.size;
64 }
65
66 /// Convenience function that calls [RenderBox.getDryBaseline].
67 static double? getDryBaseline(
68 RenderBox child,
69 BoxConstraints constraints,
70 TextBaseline baseline,
71 ) {
72 return child.getDryBaseline(constraints, baseline);
73 }
74
75 /// Convenience function that calls [RenderBox.getDistanceToBaseline].
76 ///
77 /// The given `child` must be already laid out with `constraints`.
78 static double? getBaseline(RenderBox child, BoxConstraints constraints, TextBaseline baseline) {
79 assert(!child.debugNeedsLayout);
80 assert(child.constraints == constraints);
81 return child.getDistanceToBaseline(baseline, onlyReal: true);
82 }
83}
84