Closed Gamadril closed 4 years ago
To programmatically create Android views, we have to manually generate view IDs. The current generating algorithm uses java class name of a widget Id objects (you can see this here: https://github.com/icerockdev/moko-widgets/blob/master/widgets/src/androidMain/kotlin/dev/icerock/moko/widgets/utils/IdExt.kt#L15). This means that each widget Id object must have a unique class name. In your implementation, input widgets have different Id objects, but they have the same class name, so exception happens. So, in my opinion, your version of dynamic widget creation is not quite correct.
One way to create a screen with the dynamic list of widgets is to use moko-units library with ListWidget
. In other words, do not create a linear widget inside the scroll with dynamic content, but create a list of widgets using ListWidget
and objects of theWidgetsTableUnitItem
class as list items. You can find some example here: https://github.com/icerockdev/moko-widgets/blob/master/sample/mpp-library/src/commonMain/kotlin/dev/icerock/moko/widgets/sample/CollectionScreen.kt
Thanks for the clarification. However it seems moko-units
is for displaying items inside the RecyclerView/UITableView/UICollectionView
- so items that are of the same structure.
My goal is to create different types of widgets inside a top - down container. They might have different styles, size and position.
So let's say I have the description of the UI (JSON, XML or whatever else) from which I want to create the screens using moko-widgets. The ID in that case is always just a String.
i think we can add specific implementation of ID with string id, not class name...i mark issue as enhacement
runtime ids can be created with autogenerated functions like:
inputWidgetId(uniqueId = "myUniqueId")
buttonWidgetId(uniqueId = "myUniqueId")
@Gamadril you can test it now on dev repo - https://bintray.com/beta/#/icerockdev/moko-dev/moko-widgets/bd962b3?tab=overview
I updated my project based on the recent commit of moko-widgets-template, so using 0.1.0-dev-18 now. And my code previously working with -dev12 throws an exception on Android run now.
I stripped down the test case to minimum for reproducing the error:
package org.example.mpp
import dev.icerock.moko.fields.FormField import dev.icerock.moko.fields.liveBlock import dev.icerock.moko.resources.desc.StringDesc import dev.icerock.moko.resources.desc.desc import dev.icerock.moko.widgets.* import dev.icerock.moko.widgets.core.Theme import dev.icerock.moko.widgets.core.Widget import dev.icerock.moko.widgets.screen.Args import dev.icerock.moko.widgets.screen.WidgetScreen import dev.icerock.moko.widgets.style.view.SizeSpec import dev.icerock.moko.widgets.style.view.WidgetSize import org.example.library.MR
class MainScreen( private val theme: Theme ) : WidgetScreen() {
}