Reputation: 7012
How to show material time picker using jet compose?
implementation 'androidx.compose.material3:material3:1.0.0-alpha02'
implementation 'com.google.android.material:material:1.6.0-alpha01'
I got an error message Unresolved reference: supportFragmentManager
val context = LocalContext.current
//Creating a MaterialDatePicker.Builder instance that gives us a single date selector.
val builder : MaterialDatePicker.Builder<*> = MaterialDatePicker.Builder.datePicker() // 1
//Building-up the MaterialDatePicker by calling a builder.build function
val picker : MaterialDatePicker<*> = builder.build() // 2
//Display the Material Date Picker.
picker.show(context.supportFragmentManager, picker.toString()) // 3
Upvotes: 1
Views: 1907
Reputation: 282
.supportFragmentManager is part of AppCompatActivity and not Context itself. You need to cast LocalContext.current to the Activity your Composable is hosted in. Please note that your Activity might extend ComponentActivity and not AppCompatActivity, you will need to change that.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
...
}
}
}
val supportFragmentManager = (LocalContext.current as MainActivity).supportFragmentManager
Upvotes: 3