@@ -231,40 +231,58 @@ private fun Flow(
231
231
placeables[j].mainAxisSize() +
232
232
if (j < placeables.lastIndex) mainAxisSpacing.roundToPx() else 0
233
233
}
234
- val arrangement = if (i < sequences.lastIndex) {
235
- mainAxisAlignment.arrangement
234
+ val alignment = if (i < sequences.lastIndex) {
235
+ mainAxisAlignment
236
236
} else {
237
- lastLineMainAxisAlignment.arrangement
237
+ lastLineMainAxisAlignment
238
238
}
239
- // TODO(soboleva): rtl support
240
- // Handle vertical direction
239
+
241
240
val mainAxisPositions = IntArray (childrenMainAxisSizes.size) { 0 }
242
- with (arrangement) {
243
- arrange(mainAxisLayoutSize, childrenMainAxisSizes, mainAxisPositions)
241
+ when (orientation) {
242
+ LayoutOrientation .Horizontal -> with (alignment.toHorizontalArrangement()) {
243
+ arrange(
244
+ mainAxisLayoutSize,
245
+ childrenMainAxisSizes,
246
+ layoutDirection,
247
+ mainAxisPositions
248
+ )
249
+ }
250
+ LayoutOrientation .Vertical -> with (alignment.toVerticalArrangement()) {
251
+ arrange(mainAxisLayoutSize, childrenMainAxisSizes, mainAxisPositions)
252
+ }
244
253
}
254
+
245
255
placeables.forEachIndexed { j, placeable ->
256
+ val crossAxisSize = placeable.crossAxisSize()
246
257
val crossAxis = when (crossAxisAlignment) {
247
258
FlowCrossAxisAlignment .Start -> 0
248
259
FlowCrossAxisAlignment .End ->
249
- crossAxisSizes[i] - placeable. crossAxisSize()
260
+ crossAxisSizes[i] - crossAxisSize
250
261
FlowCrossAxisAlignment .Center ->
251
262
Alignment .Center .align(
252
263
IntSize .Zero ,
253
264
IntSize (
254
265
width = 0 ,
255
- height = crossAxisSizes[i] - placeable. crossAxisSize()
266
+ height = crossAxisSizes[i] - crossAxisSize
256
267
),
257
268
LayoutDirection .Ltr
258
269
).y
259
270
}
271
+
272
+ val crossAxisPosition = crossAxisPositions[i] + crossAxis
260
273
if (orientation == LayoutOrientation .Horizontal ) {
261
274
placeable.place(
262
275
x = mainAxisPositions[j],
263
- y = crossAxisPositions[i] + crossAxis
276
+ y = crossAxisPosition
264
277
)
265
278
} else {
266
279
placeable.place(
267
- x = crossAxisPositions[i] + crossAxis,
280
+ x = when (layoutDirection) {
281
+ LayoutDirection .Ltr ->
282
+ crossAxisPosition
283
+ LayoutDirection .Rtl ->
284
+ crossAxisLayoutSize - crossAxisPosition - crossAxisSize
285
+ },
268
286
y = mainAxisPositions[j]
269
287
)
270
288
}
@@ -274,6 +292,26 @@ private fun Flow(
274
292
}
275
293
}
276
294
295
+ private fun FlowMainAxisAlignment.toHorizontalArrangement () =
296
+ when (this ) {
297
+ FlowMainAxisAlignment .Center -> Arrangement .Center
298
+ FlowMainAxisAlignment .Start -> Arrangement .Start
299
+ FlowMainAxisAlignment .End -> Arrangement .End
300
+ FlowMainAxisAlignment .SpaceEvenly -> Arrangement .SpaceEvenly
301
+ FlowMainAxisAlignment .SpaceBetween -> Arrangement .SpaceBetween
302
+ FlowMainAxisAlignment .SpaceAround -> Arrangement .SpaceAround
303
+ }
304
+
305
+ private fun FlowMainAxisAlignment.toVerticalArrangement () =
306
+ when (this ) {
307
+ FlowMainAxisAlignment .Center -> Arrangement .Center
308
+ FlowMainAxisAlignment .Start -> Arrangement .Top
309
+ FlowMainAxisAlignment .End -> Arrangement .Bottom
310
+ FlowMainAxisAlignment .SpaceEvenly -> Arrangement .SpaceEvenly
311
+ FlowMainAxisAlignment .SpaceBetween -> Arrangement .SpaceBetween
312
+ FlowMainAxisAlignment .SpaceAround -> Arrangement .SpaceAround
313
+ }
314
+
277
315
/* *
278
316
* Used to specify how a layout chooses its own size when multiple behaviors are possible.
279
317
*/
@@ -294,40 +332,38 @@ public enum class SizeMode {
294
332
/* *
295
333
* Used to specify the alignment of a layout's children, in main axis direction.
296
334
*/
297
- public enum class MainAxisAlignment (internal val arrangement : Arrangement .Vertical ) {
298
- // TODO(soboleva) support RTl in Flow
299
- // workaround for now - use Arrangement that equals to previous Arrangement
335
+ public enum class MainAxisAlignment {
300
336
/* *
301
337
* Place children such that they are as close as possible to the middle of the main axis.
302
338
*/
303
- Center ( Arrangement . Center ) ,
339
+ Center ,
304
340
305
341
/* *
306
342
* Place children such that they are as close as possible to the start of the main axis.
307
343
*/
308
- Start ( Arrangement . Top ) ,
344
+ Start ,
309
345
310
346
/* *
311
347
* Place children such that they are as close as possible to the end of the main axis.
312
348
*/
313
- End ( Arrangement . Bottom ) ,
349
+ End ,
314
350
315
351
/* *
316
352
* Place children such that they are spaced evenly across the main axis, including free
317
353
* space before the first child and after the last child.
318
354
*/
319
- SpaceEvenly ( Arrangement . SpaceEvenly ) ,
355
+ SpaceEvenly ,
320
356
321
357
/* *
322
358
* Place children such that they are spaced evenly across the main axis, without free
323
359
* space before the first child or after the last child.
324
360
*/
325
- SpaceBetween ( Arrangement . SpaceBetween ) ,
361
+ SpaceBetween ,
326
362
327
363
/* *
328
364
* Place children such that they are spaced evenly across the main axis, including free
329
365
* space before the first child and after the last child, but half the amount of space
330
366
* existing otherwise between two consecutive children.
331
367
*/
332
- SpaceAround ( Arrangement . SpaceAround ) ;
368
+ SpaceAround ;
333
369
}
0 commit comments