Advertisement
Cnvmendoza

ScaffoldDemo

Mar 12th, 2025 (edited)
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.73 KB | None | 0 0
  1. package com.example.scaffolddemo
  2.  
  3. import android.os.Bundle
  4. import androidx.activity.ComponentActivity
  5. import androidx.activity.compose.setContent
  6. import androidx.activity.enableEdgeToEdge
  7. import androidx.compose.foundation.layout.Arrangement
  8. import androidx.compose.foundation.layout.Row
  9. import androidx.compose.foundation.layout.fillMaxSize
  10. import androidx.compose.foundation.layout.fillMaxWidth
  11. import androidx.compose.foundation.layout.padding
  12. import androidx.compose.foundation.lazy.LazyColumn
  13. import androidx.compose.foundation.lazy.items
  14. import androidx.compose.material.icons.Icons
  15. import androidx.compose.material.icons.filled.AccountBox
  16. import androidx.compose.material.icons.filled.Add
  17. import androidx.compose.material.icons.filled.Delete
  18. import androidx.compose.material.icons.filled.Refresh
  19. import androidx.compose.material3.Card
  20. import androidx.compose.material3.CardDefaults
  21. import androidx.compose.material3.ExperimentalMaterial3Api
  22. import androidx.compose.material3.FloatingActionButton
  23. import androidx.compose.material3.Icon
  24. import androidx.compose.material3.IconButton
  25. import androidx.compose.material3.MaterialTheme
  26. import androidx.compose.material3.Scaffold
  27. import androidx.compose.material3.Surface
  28. import androidx.compose.material3.Text
  29. import androidx.compose.material3.TopAppBar
  30. import androidx.compose.material3.TopAppBarDefaults
  31. import androidx.compose.runtime.Composable
  32. import androidx.compose.runtime.getValue
  33. import androidx.compose.runtime.mutableStateListOf
  34. import androidx.compose.runtime.mutableStateOf
  35. import androidx.compose.runtime.remember
  36. import androidx.compose.runtime.setValue
  37. import androidx.compose.ui.Alignment
  38. import androidx.compose.ui.Modifier
  39. import androidx.compose.ui.text.font.FontWeight
  40. import androidx.compose.ui.tooling.preview.Preview
  41. import androidx.compose.ui.unit.dp
  42. import com.example.scaffolddemo.ui.theme.ScaffoldDemoTheme
  43.  
  44. class MainActivity : ComponentActivity() {
  45.     override fun onCreate(savedInstanceState: Bundle?) {
  46.         super.onCreate(savedInstanceState)
  47.         enableEdgeToEdge()
  48.         setContent {
  49.             TaskManagerApp()
  50.         }
  51.     }
  52. }
  53.  
  54. @OptIn(ExperimentalMaterial3Api::class)
  55. @Composable
  56. fun TaskManagerApp() {
  57.     var taskCounter by remember { mutableStateOf(1) }
  58.     var taskList = remember { mutableStateListOf<String>() }
  59.  
  60.     Scaffold(
  61.         topBar = { TaskAppBar() },
  62.         floatingActionButton = {
  63.             FloatingActionButton(
  64.                 onClick = {
  65.                     taskList.add("Task $taskCounter")
  66.                     taskCounter++
  67.                 },
  68.                 containerColor = MaterialTheme.colorScheme.primary
  69.             ) {
  70.                 Icon(imageVector = Icons.Filled.Add, contentDescription = "Add Task")
  71.             }
  72.         },
  73.         content = { paddingValues ->
  74.             Surface(
  75.                 modifier = Modifier
  76.                     .fillMaxSize()
  77.                     .padding(paddingValues)
  78.                     .padding(16.dp)
  79.             ) {
  80.                 TaskList(taskList) { task -> taskList.remove(task)}
  81.             }
  82.         }
  83.     )
  84. }
  85.  
  86. @OptIn(ExperimentalMaterial3Api::class)
  87. @Composable
  88. fun TaskAppBar() {
  89.     TopAppBar(
  90.         title = { Text(text = "Task Manager", fontWeight = FontWeight.Bold) },
  91.         actions = {
  92.             IconButton(onClick = { }) {
  93.                 Icon(
  94.                     imageVector = Icons.Filled.Refresh,
  95.                     contentDescription = "Clear All Tasks"
  96.                 )
  97.             }
  98.             IconButton(onClick = { }) {
  99.                 Icon(
  100.                     imageVector = Icons.Filled.AccountBox,
  101.                     contentDescription = "Clear All Tasks"
  102.                 )
  103.             }
  104.         },
  105.         colors = TopAppBarDefaults.topAppBarColors(MaterialTheme.colorScheme.primaryContainer)
  106.     )
  107. }
  108.  
  109. @Composable
  110. fun TaskList(tasks: List<String>, onDelete: (String) -> Unit) {
  111.     LazyColumn {
  112.         items(tasks) {
  113.             task -> TaskItem(task, onDelete)
  114.         }
  115.     }
  116. }
  117.  
  118. @Composable
  119. fun TaskItem(task: String, onDelete: (String) -> Unit) {
  120.     Card(
  121.         modifier = Modifier
  122.             .fillMaxSize()
  123.             .padding(8.dp),
  124.         elevation = CardDefaults.cardElevation(defaultElevation = 4.dp)
  125.     ) {
  126.        Row(
  127.            modifier = Modifier
  128.                .fillMaxWidth()
  129.                .padding(16.dp),
  130.            verticalAlignment = Alignment.CenterVertically,
  131.            horizontalArrangement = Arrangement.SpaceBetween
  132.        ) {
  133.             Text(text = task, fontWeight = FontWeight.Medium)
  134.            IconButton(onClick = { onDelete(task) }) {
  135.                Icon(imageVector = Icons.Filled.Delete, contentDescription = "Delete Task")
  136.            }
  137.        }
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement