How can I create reusable modifiers without android compose lint rules throwing a fit?
I don't want to have to copy/paste the same modifiers for every screen within my app, I would rather just create an extension function I can call like this,
Box(modifier = Modifier.defaultFillScreen())
But that extension function, shown below, keeps throwing lint errors.
@Composable
fun Modifier.defaultFillScreen() = this.then(Modifier
.fillMaxWidth()
.navigationBarsWithImePadding()
.verticalScroll(rememberScrollState())
.padding(dimensionResource(id = R.dimen.standard_padding)))
Gives me the following lint error:
ComposableModifierFactory: Modifier factory functions should not be marked as @Composable, and should use composed instead
When I make that change I then get a new lint error:
fun Modifier.defaultFillScreen() = composed { this.then(Modifier
.fillMaxWidth()
.navigationBarsWithImePadding()
.verticalScroll(rememberScrollState())
.padding(dimensionResource(id = R.dimen.standard_padding))) }
UnnecessaryComposedModifier: Unnecessary use of Modifier.composed
How can I create a reusable modifier without compose complaining about it? Writing the same 5 lines of modifier code for every screen is not an acceptable answer.
dependencies:
'androidx.activity:activity-compose:1.3.1',
'androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha07',
'androidx.compose.material:material:1.0.5',
'androidx.navigation:navigation-compose:2.4.0-alpha06',
'androidx.compose.ui:ui:1.0.5',
'androidx.compose.ui:ui-tooling:1.0.5'
Android studio:
Android Studio Arctic Fox | 2020.3.1 Build #AI-203.7717.56.2031.7583922, built on July 26, 2021
I don't see the same warning with compose 1.2.0-alpha and Android Studio Bubblebee, it used to appear when i use
Modifier.composedwithoutstate.Purpose of Modifier.composed is having stateful modifiers which you use with
remember,LaunchedEffect. When you don't have a state associated with your Modifier you should you Modifier.then insteadThis is just a sample composed example. If you change from remember you will see that at each recomposition random color will change.
And without composed it will give error
@Composable invocations can only happen from the context of a @Composable functionif you use remember like the snippet below.