Defines the size of a track (a row or a column) in a Grid.

Use the companion functions (e.g., Fixed, Flex) to create instances.

Summary

Public companion functions

GridTrackSize
Fixed(size: Dp)

A track with a fixed Dp size.

Cmn
GridTrackSize
Flex(weight: @FloatRange(from = 0.0) Fr)

A flexible track that takes a share of the remaining space in the grid after non-flexible tracks (like Fixed and Percentage) are allocated.

Cmn
GridTrackSize
MinMax(min: Dp, max: @FloatRange(from = 0.0) Fr)

A flexible track with an explicitly defined minimum base size and a flexible maximum size.

Cmn
GridTrackSize
Percentage(value: @FloatRange(from = 0.0) Float)

A track sized as a percentage of the total available size of the grid container.

Cmn

Public companion properties

GridTrackSize

A track that behaves as minmax(min-content, max-content).

Cmn
GridTrackSize

A track that sizes itself to fit the maximum intrinsic size of its contents.

Cmn
GridTrackSize

A track that sizes itself to fit the minimum intrinsic size of its contents.

Cmn

Public functions

open String
Cmn

Public companion functions

Fixed

fun Fixed(size: Dp): GridTrackSize

A track with a fixed Dp size.

Parameters
size: Dp

The size of the track.

Throws
IllegalArgumentException

if size is negative or Dp.Unspecified.

Flex

fun Flex(weight: @FloatRange(from = 0.0) Fr): GridTrackSize

A flexible track that takes a share of the remaining space in the grid after non-flexible tracks (like Fixed and Percentage) are allocated.

Intrinsic Sizing: By default, a Flex track behaves like CSS 1fr (which implies minmax(min-content, <weight>fr)). Before distributing the remaining space, it queries the minimum intrinsic size (min-content) of its children to establish a base size and ensure content is not crushed.

Jetpack Compose strictly forbids querying the intrinsic size of a SubcomposeLayout (such as LazyColumn or LazyRow). Placing a lazy list directly inside a standard Flex track will result in an IllegalStateException crash. To safely place lazy lists in a flexible track, use MinMax instead.

Parameters
weight: @FloatRange(from = 0.0) Fr

The flexible weight. Remaining space is distributed proportionally to this weight divided by the sum of all flex weights. Must be non-negative.

Throws
IllegalArgumentException

if weight is negative.

See also
MinMax

MinMax

fun MinMax(min: Dp, max: @FloatRange(from = 0.0) Fr): GridTrackSize

A flexible track with an explicitly defined minimum base size and a flexible maximum size. Conceptually, this behaves identically to the CSS Grid minmax(min, max) function.

Difference from Flex: While a standard Flex track inherently queries the min-content intrinsic size of its children to determine its minimum base size, MinMax strictly uses the provided min size.

Usage with Lazy Lists: Because MinMax relies on a predefined min size (e.g., 0.dp), it entirely bypasses the intrinsic measurement pass. This makes it the required choice when placing SubcomposeLayout-backed components (such as LazyColumn or LazyRow) inside a flexible grid track, as these components will crash if their intrinsic sizes are queried.

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Grid
import androidx.compose.foundation.layout.GridTrackSize
import androidx.compose.foundation.layout.columns
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.Text
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp

Grid(
    modifier = Modifier.fillMaxSize().padding(16.dp),
    config = {
        column(120.dp) // Sidebar width
        column(minmax(0.dp, 1.fr)) // Content width

        row(60.dp) // Header height
        // IMPORTANT:
        // Flex track '1.fr' queries child intrinsic sizes. Since SubcomposeLayouts
        // (like LazyColumn) crash on intrinsic queries, we MUST use 'GridTrackSize.MinMax' with
        // an explicit minimum size (0.dp) to bypass the measurement crash safely!
        row(minmax(0.dp, 1.fr))

        gap(16.dp)
    },
) {
    // Top Header spanning both columns
    Box(
        Modifier.gridItem(row = 1, column = 1, columnSpan = 2)
            .background(Color.DarkGray)
            .fillMaxSize(),
        contentAlignment = Alignment.Center,
    ) {
        Text("App Header", color = Color.White)
    }

    // Left Sidebar
    Box(
        Modifier.gridItem(row = 2, column = 1).background(Color.LightGray).fillMaxSize(),
        contentAlignment = Alignment.Center,
    ) {
        Text("Navigation")
    }

    // Scrollable LazyColumn safely constrained in the flex area
    LazyColumn(
        modifier = Modifier.gridItem(row = 2, column = 2).fillMaxSize(),
        verticalArrangement = Arrangement.spacedBy(8.dp),
    ) {
        items(50) { index ->
            Box(Modifier.fillMaxWidth().background(Color(0xFFE0E0FF)).padding(16.dp)) {
                Text("Scrollable Content #$index")
            }
        }
    }
}
Parameters
min: Dp

The explicit minimum fixed base size (e.g., 0.dp).

max: @FloatRange(from = 0.0) Fr

The maximum flexible distribution weight (e.g., 1.fr).

Throws
IllegalArgumentException

if min is negative or max is negative.

See also
Flex

Percentage

fun Percentage(value: @FloatRange(from = 0.0) Float): GridTrackSize

A track sized as a percentage of the total available size of the grid container. Note: In this implementation, percentages are calculated based on the remaining available space after gaps. This differs from the W3C CSS Grid spec, where percentages are based on the container size regardless of gaps. This behavior prevents unexpected overflows when mixing gaps and percentages (e.g., 50% + 50% + gap will fit perfectly here, but would overflow in CSS).

Parameters
value: @FloatRange(from = 0.0) Float

The percentage of the container size.

Throws
IllegalArgumentException

if value is negative.

Public companion properties

Auto

val AutoGridTrackSize

A track that behaves as minmax(min-content, max-content). It occupies at least its minimum content size, and grows to fit its maximum content size if space is available.

MaxContent

val MaxContentGridTrackSize

A track that sizes itself to fit the maximum intrinsic size of its contents.

MinContent

val MinContentGridTrackSize

A track that sizes itself to fit the minimum intrinsic size of its contents.

Public functions

toString

open fun toString(): String