Skip to content

Commit eacf0f9

Browse files
authored
Explain when and why to use CrossAxisAlignment.baseline (flutter#143632)
Improves the docs around horizontal alignment of text, due to several issues expressing confusion about this topic.
1 parent 718c817 commit eacf0f9

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

packages/flutter/lib/src/rendering/flex.dart

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ enum MainAxisAlignment {
142142
/// See also:
143143
///
144144
/// * [Column], [Row], and [Flex], the flex widgets.
145+
/// * [Flex.crossAxisAlignment], the property on flex widgets that
146+
/// has this type.
145147
/// * [RenderFlex], the flex render object.
146148
enum CrossAxisAlignment {
147149
/// Place the children with their start edge aligned with the start side of
@@ -185,16 +187,35 @@ enum CrossAxisAlignment {
185187

186188
/// Place the children along the cross axis such that their baselines match.
187189
///
190+
/// Consider using this value for any horizontal main axis (as with [Row])
191+
/// where the children primarily contain text. If the different children
192+
/// have text with different font metrics (for example because they differ
193+
/// in [TextStyle.fontSize] or other [TextStyle] properties, or because
194+
/// they use different fonts due to being written in different scripts),
195+
/// then this typically produces better visual alignment than the other
196+
/// [CrossAxisAlignment] values, which use no information about
197+
/// where the text sits vertically within its bounding box.
198+
///
199+
/// The baseline of a widget is typically the typographic baseline of the
200+
/// first text in the first [Text] or [RichText] widget it encloses, if any.
201+
/// The typographic baseline is a horizontal line used for aligning text,
202+
/// which is specified by each font; for alphabetic scripts, it ordinarily
203+
/// runs along the bottom of letters excluding any descenders.
204+
///
188205
/// Because baselines are always horizontal, this alignment is intended for
189-
/// horizontal main axes. If the main axis is vertical, then this value is
190-
/// treated like [start].
206+
/// horizontal main axes (as with [Row]). If the main axis is vertical
207+
/// (as with [Column]), then this value is treated like [start].
191208
///
192209
/// For horizontal main axes, if the minimum height constraint passed to the
193210
/// flex layout exceeds the intrinsic height of the cross axis, children will
194211
/// be aligned as close to the top as they can be while honoring the baseline
195212
/// alignment. In other words, the extra space will be below all the children.
196213
///
197214
/// Children who report no baseline will be top-aligned.
215+
///
216+
/// See also:
217+
///
218+
/// * [RenderBox.getDistanceToBaseline], which defines the baseline of a box.
198219
baseline,
199220
}
200221

packages/flutter/lib/src/widgets/basic.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4629,6 +4629,13 @@ class Flex extends MultiChildRenderObjectWidget {
46294629
///
46304630
/// For example, [CrossAxisAlignment.center], the default, centers the
46314631
/// children in the cross axis (e.g., horizontally for a [Column]).
4632+
///
4633+
/// When the cross axis is vertical (as for a [Row]) and the children
4634+
/// contain text, consider using [CrossAxisAlignment.baseline] instead.
4635+
/// This typically produces better visual results if the different children
4636+
/// have text with different font metrics, for example because they differ in
4637+
/// [TextStyle.fontSize] or other [TextStyle] properties, or because
4638+
/// they use different fonts due to being written in different scripts.
46324639
final CrossAxisAlignment crossAxisAlignment;
46334640

46344641
/// Determines the order to lay children out horizontally and how to interpret
@@ -4778,6 +4785,14 @@ class Flex extends MultiChildRenderObjectWidget {
47784785
/// If you only have one child, then consider using [Align] or [Center] to
47794786
/// position the child.
47804787
///
4788+
/// By default, [crossAxisAlignment] is [CrossAxisAlignment.center], which
4789+
/// centers the children in the vertical axis. If several of the children
4790+
/// contain text, this is likely to make them visually misaligned if
4791+
/// they have different font metrics (for example because they differ in
4792+
/// [TextStyle.fontSize] or other [TextStyle] properties, or because
4793+
/// they use different fonts due to being written in different scripts).
4794+
/// Consider using [CrossAxisAlignment.baseline] instead.
4795+
///
47814796
/// {@tool snippet}
47824797
///
47834798
/// This example divides the available space into three (horizontally), and

0 commit comments

Comments
 (0)