Un FlowRow qui renvoie les puces à la ligne
Créez une véritable disposition avec retour à la ligne depuis zéro.
Un FlowRow qui renvoie les puces à la ligne est une leçon Jetpack Compose Academy gratuite sur CoddyKit. Ceci est la leçon 4 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Jetpack Compose Academy, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Jetpack Compose Academy comprend 4 leçons au total.
Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.
What a Flow Row Does
A flow row places children left to right, then wraps to a new line when it runs out of width. Perfect for tag chips and filters. 🏷️
Start From the Layout Composable
You already know the tool: the Layout composable. The wrapping logic lives entirely in how you measure and place children.
Layout(content, modifier) { measurables, constraints ->
}Measure Children Loosely
Measure each chip with the parent's maxWidth but no minimum, so chips size to their content rather than stretching full width.
val placeables = measurables.map {
it.measure(constraints.copy(minWidth = 0))
}Track the Current Row Width
Walk the placeables and keep a running row width. Each chip you add increases it by the chip's own width.
Wrap When the Row Overflows
If adding the next chip would exceed maxWidth, reset the row width and move down by the tallest chip in the finished row.
if (rowWidth + placeable.width > maxWidth) {
y += rowHeight
rowWidth = 0
}Remember Each Chip's Position
As you scan, store the chosen x and y for every chip in a list. You will replay these positions during the placement step.
Compute the Total Height
After the last row, the layout's height is the final y plus that row's height. The width is simply the constraint's maxWidth.
Report Size and Place Chips
Call layout with your total width and height, then loop through the stored positions, calling placeRelative on each placeable.
layout(maxWidth, totalHeight) {
placeables.forEachIndexed { i, p ->
p.placeRelative(positions[i])
}
}Add Horizontal Spacing
Insert a small gap between chips by adding a spacing value to the row width after each chip. Vertical gaps work the same way.
Compose Ships FlowRow Too
The official FlowRow from the layout library does all this for you. Building it yourself shows exactly how that magic works under the hood. ✨
FlowRow { Chip("Kotlin"); Chip("Compose") }You Built a Real Layout
You measured children, wrapped rows by width, and placed each chip by hand. That is a production-grade custom layout built from first principles. 🎉
Quick Check
What triggers the flow row to start a new line?
Recap: A FlowRow That Wraps Chips
You measured chips loosely, tracked row width, and wrapped to a new line on overflow, then placed each one. That is FlowRow from scratch. ✅
Questions Fréquemment Posées
La leçon « Un FlowRow qui renvoie les puces à la ligne » est-elle gratuite ?
Oui — le texte complet de « Un FlowRow qui renvoie les puces à la ligne » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Jetpack Compose Academy, passe à CoddyKit PRO. Le cours Jetpack Compose Academy comprend 4 leçons au total.
Qu'est-ce que j'apprendrai dans « Un FlowRow qui renvoie les puces à la ligne » ?
Créez une véritable disposition avec retour à la ligne depuis zéro. Tu pratiques Jetpack Compose Academy avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.
Dois-je avoir de l'expérience pour commencer Jetpack Compose Academy ?
Aucune expérience préalable n'est requise. Jetpack Compose Academy sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 4 sur 4.
Combien de temps prend la leçon « Un FlowRow qui renvoie les puces à la ligne » ?
La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.
Peux-tu écrire et exécuter du code dans cette leçon Jetpack Compose Academy ?
Oui. Chaque leçon Jetpack Compose Academy inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.
Toutes les leçons de ce cours
- Le modèle Mesurer et Placer
- Écrire un Composable de disposition
- Contraintes et mesures intrinsèques
- Un FlowRow qui renvoie les puces à la ligne