PPB F - Tugas 6 Image Scroll

Nama : Ahmad Ibnu Malik Rahman
Kelas  : PPB F
NRP   : 5025201232


Tugas 6 (Image Scroll)

Membuat daftar yang dapat digulir di aplikasi menggunakan Jetpack Compose.

  1. package com.example.affirmations
  2. import android.os.Bundle
  3. import androidx.activity.ComponentActivity
  4. import androidx.activity.compose.setContent
  5. import androidx.compose.foundation.Image
  6. import androidx.compose.foundation.layout.Column
  7. import androidx.compose.foundation.layout.fillMaxSize
  8. import androidx.compose.foundation.layout.fillMaxWidth
  9. import androidx.compose.foundation.layout.height
  10. import androidx.compose.foundation.layout.padding
  11. import androidx.compose.foundation.lazy.LazyColumn
  12. import androidx.compose.foundation.lazy.items
  13. import androidx.compose.material3.Card
  14. import androidx.compose.material3.MaterialTheme
  15. import androidx.compose.material3.Surface
  16. import androidx.compose.material3.Text
  17. import androidx.compose.runtime.Composable
  18. import androidx.compose.ui.Modifier
  19. import androidx.compose.ui.layout.ContentScale
  20. import androidx.compose.ui.platform.LocalContext
  21. import androidx.compose.ui.res.painterResource
  22. import androidx.compose.ui.res.stringResource
  23. import androidx.compose.ui.tooling.preview.Preview
  24. import androidx.compose.ui.unit.dp
  25. import com.example.affirmations.data.Datasource
  26. import com.example.affirmations.model.Affirmation
  27. import com.example.affirmations.ui.theme.AffirmationsTheme
  28. class MainActivity : ComponentActivity() {
  29. override fun onCreate(savedInstanceState: Bundle?) {
  30. super.onCreate(savedInstanceState)
  31. setContent {
  32. AffirmationsTheme {
  33. // A surface container using the 'background' color from the theme
  34. Surface(
  35. modifier = Modifier.fillMaxSize(),
  36. color = MaterialTheme.colorScheme.background
  37. ) {
  38. AffirmationsApp()
  39. }
  40. }
  41. }
  42. }
  43. }
  44. @Composable
  45. fun AffirmationsApp() {
  46. AffirmationList(affirmationList = Datasource().loadAffirmations())
  47. }
  48. @Composable
  49. fun AffirmationCard(affirmation: Affirmation, modifier: Modifier = Modifier){
  50. Card(modifier = modifier) {
  51. Column {
  52. Image(painter = painterResource(affirmation.imageResourceId),
  53. contentDescription = stringResource(affirmation.stringResourceId),
  54. modifier = Modifier
  55. .fillMaxWidth()
  56. .height(194.dp),
  57. contentScale = ContentScale.Crop
  58. )
  59. Text(
  60. text = LocalContext.current.getString(affirmation.stringResourceId),
  61. modifier = Modifier.padding(16.dp),
  62. style = MaterialTheme.typography.headlineSmall)
  63. }
  64. }
  65. }
  66. @Preview(
  67. showSystemUi = true,
  68. showBackground = true
  69. )
  70. @Composable
  71. private fun AffirmationCardPreview() {
  72. AffirmationCard(affirmation = Affirmation(R.string.affirmation1, R.drawable.image1))
  73. }
  74. @Composable
  75. fun AffirmationList(affirmationList: List<Affirmation>, modifier: Modifier = Modifier){
  76. LazyColumn (modifier = modifier){
  77. items(affirmationList){affirmation ->
  78. AffirmationCard(affirmation,
  79. modifier = Modifier.padding(16.dp),
  80. )
  81. }
  82. }
  83. }

Komentar

Postingan Populer