https://kotlinlang.org logo
#compose
Title
# compose
d

dorche

07/08/2022, 11:01 AM
I think I’ve hit a bug in Compose Material - is anyone else experiencing this? Basically
BottomSheetScaffold
is not rendering properly in Previews (only Preview as far as I can see) on
androidx.compose.material:material:1.2.0-beta03
but it does on the previous version -
androidx.compose.material:material:1.2.0-beta02
. More in 🧵
On a brand new project created from the Android Studio template, with the following code:
Copy code
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            BottomSheetStateIssueTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    BottomSheetBug()
                }
            }
        }
    }
}

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun BottomSheetBug() {
    val bottomSheetScaffoldState = rememberBottomSheetScaffoldState(
        bottomSheetState = BottomSheetState(BottomSheetValue.Collapsed)
    )
    val coroutineScope = rememberCoroutineScope()
    BottomSheetScaffold(
        scaffoldState = bottomSheetScaffoldState,
        sheetContent = {
            Box(
                Modifier
                    .fillMaxWidth()
                    .height(200.dp)
                    .background(Color.Cyan)
            ) {
                Text(text = "Hello from sheet")
            }
        },
        sheetPeekHeight = 50.dp,
        contentColor = Color.Red
    ) {
        Box(modifier = Modifier.height(200.dp)) {
            Button(onClick = {
                coroutineScope.launch {
                    if (bottomSheetScaffoldState.bottomSheetState.isCollapsed) {
                        bottomSheetScaffoldState.bottomSheetState.expand()
                    } else {
                        bottomSheetScaffoldState.bottomSheetState.collapse()
                    }
                }
            }) {
                Text(text = "Expand/Collapse Bottom Sheet")
            }
        }
    }
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    BottomSheetStateIssueTheme {
        BottomSheetBug()
    }
}
the Preview shows this on the beta03 version:
When you run it on device or even just “Start Interactive Mode”, it fixes itself, screenshots below for collapsed and expanded state…
The release notes for beta03 indeed mention changes to
BottomSheetScaffold
which I’m guessing broke that https://developer.android.com/jetpack/androidx/releases/compose-material#1.2.0-beta03
This is quite disruptive for us atm because we are using Previews for screenshot testing.. Could someone confirm if they are seeing the same behaviour and I’m not going crazy?
(I made that change. Sorry!)
d

dorche

07/13/2022, 8:16 PM
I don’t think I have a login for that, is that an internal bug tracker?
j

jossiwolf

07/14/2022, 4:49 PM
t

Tristan Hamilton

08/08/2022, 5:15 PM
I seem to have bumped into similar issue - trying to preview an expanded ModalBottomSheetLayout in 1.2.0 and i only see the scrim until i click on interactive mode. Was a ticket created in the end?
d

dorche

08/08/2022, 5:17 PM
Hey no sorry I was on holiday and only coming back this week so I've not raised one yet. Let me know if you end up raising one, if not I'll do it this week and drop a message here
t

Tristan Hamilton

08/08/2022, 5:52 PM
Ah no worries, I wanted to check first to see if there was a root cause in case ive got a different issue - i tried to rollback through a couple of the 1.2 betas/alphas for the view im trying to preview and i still only see the scrim until I click interactive mode 😕 so maybe im doing something wrong. Think id need to try experiment in a simple project to confirm before opening a ticket.
d

dorche

08/08/2022, 5:56 PM
That might be because another library is promoting material to a newer version still, try this (might need tweaking if you use Kotlin DSL)
Copy code
implementation("androidx.compose.material:material") {
        version {
            // todo remove when BottomSheetScaffold Preview Bug is fixed
            strictly '1.2.0-beta02'
        }
    }
That’s the latest version I found to be working fine at the time. This is what I did for our project because we absolutely depend on the previews working fine
t

Tristan Hamilton

08/09/2022, 11:17 AM
ok made a super small example here: https://github.com/Munzey/ModalBottomSheetPreviewBug/blob/master/app/src/main/java/com/example/modalbottomsheetpreviewbug/MainActivity.kt regardless of the compose version (tried
1.0.0
,
1.1.0
,
1.2.0-alpha0X
etc), the preview does not show the expanded bottom sheet, only the scrim has rendered. When you click interactive mode, the modal pops up. This seems different to your issue listed above so I’ll submit a ticket for this case anyway.
j

jossiwolf

08/11/2022, 12:38 PM
Thanks for filing @Tristan Hamilton! FWIW the original issue was about
BottomSheetScaffold
- we reverted the change that surfaced the bug in that. The issue you are seeing has the same root cause though which we are working on fixing, but it will be a bit still. I might mark your issue as duplicate and then you can follow along in the OG bug 🙂
t

Tristan Hamilton

08/11/2022, 12:40 PM
thanks for the follow up!
5 Views