Dynamic number of items
Dynamic Number of Items in ComposeFlow
In ComposeFlow, certain composables like LazyColumn
, LazyRow
, and LazyVerticalGrid
allow you to generate a dynamic number of child items based on your data sources. This is particularly useful when you want to display lists or grids of data that can change in size, such as:
- Binding to app states that contain lists of a data type.
- Displaying results from external APIs that return JSON responses with variable lengths.
Overview
- Lazy Composables: Special composables that efficiently display large or dynamic datasets by only rendering items currently visible on the screen.
- Data Sources: Lists or collections of data that you bind to lazy composables to generate UI elements dynamically.
- Dynamic Items: The individual UI elements that represent each item in your data source.
Steps to Implement Dynamic Items
Follow these steps to create a dynamic list or grid in your ComposeFlow app:
Step 1: Define a List Type State
-
Create or Import a Data Type:
- Navigate to “Data Type” in the ComposeFlow menu.
- Create a data type, for example, define a data type called
Plant
with properties likename
,description
, andimageUrl
.
-
Create a List State:
- Navigate to the App state in the ComposeFlow menu.
- Add a new state of type
List<Plant>
. - Initialize it with sample data (assuming the initial data is created by a json like found as in Sunflower app).
Step 2: Add a Lazy Composable to Your Screen
-
Open the UI Builder:
- Go to the screen where you want to display the dynamic list or grid.
-
Add a Lazy Composable:
- From the Components Library, drag and drop a
LazyColumn
,LazyRow
, orLazyVerticalGrid
onto your canvas. LazyColumn
: For vertical lists.LazyRow
: For horizontal lists.LazyVerticalGrid
: For grid layouts.
- From the Components Library, drag and drop a
Step 3: Focus on the Lazy Composable
- Click on the lazy composable you just added to select it.
- Ensure it is highlighted in the Canvas Area and that its properties are visible in the Inspector panel.
Step 4: Bind the Data Source
-
Access the Dynamic Items Section:
- In the Inspector panel, locate the “Dynamic Items” section.
-
Add a Data Source:
- Click the ”+” button in the “Dynamic Items” section.
- A dialog will appear prompting you to select a data source.
-
Select the Data Source:
- Choose the list state you created earlier (e.g.,
List<Plant>
). - Confirm your selection to bind the data source to the lazy composable.
- Choose the list state you created earlier (e.g.,
Step 5: Add a Child Composable
-
Add an Item Template:
- Drag and drop a composable (e.g., Card, Row, Column) into the lazy composable.
- This composable will serve as the template for each item in your list.
Note: Only one direct child of the lazy composable can be bound to the dynamic item.
-
Design the Item Layout:
- Inside the item template, add the necessary UI elements to display the data.
- For example, add an Image for
imageUrl
and Text composables forname
andspecies
.
Step 6: Bind Item Properties to Data Fields
-
Select UI Elements:
- Click on each composable inside your item template (e.g., Text, Image).
-
Bind to Dynamic Item Properties:
- In the Inspector panel, find the property you want to bind (e.g., “Text” for a Text composable).
- Click “Set from State” or the binding icon next to the property.
- A dialog will appear showing available states.
-
Select Dynamic Item Properties:
- You’ll see the properties of the data type (e.g.,
Plant.name
,Plant.species
). - Select the appropriate property to bind it to the UI element.
- You’ll see the properties of the data type (e.g.,
Step 7: Configure and Test
-
Adjust Modifiers and Styling:
- Apply any necessary modifiers to style your item template (e.g., padding, background color).
-
Preview the List:
- Use the Preview feature to see how your dynamic list or grid looks with the bound data.
-
Test Interactions:
- If your items are interactive, add actions as needed and test them in the preview or on a device.
Important Notes
-
Single Direct Child Rule:
- The lazy composable can have only one direct child that is bound to the dynamic item.
- This child serves as the template for each item in the data source.
-
Modifiers and Layouts:
- Apply modifiers to the child composable to control spacing, alignment, and styling.
- Use layouts like Row or Column inside the item template to organize content.
-
Data Updates:
- If the data source list updates (e.g., items are added or removed), the UI will automatically reflect these changes due to ComposeFlow’s declarative nature.