Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.scaffolddemo
- import android.os.Bundle
- import androidx.activity.ComponentActivity
- import androidx.activity.compose.setContent
- import androidx.activity.enableEdgeToEdge
- import androidx.compose.foundation.layout.Arrangement
- import androidx.compose.foundation.layout.Row
- import androidx.compose.foundation.layout.fillMaxSize
- import androidx.compose.foundation.layout.fillMaxWidth
- import androidx.compose.foundation.layout.padding
- import androidx.compose.foundation.lazy.LazyColumn
- import androidx.compose.foundation.lazy.items
- import androidx.compose.material.icons.Icons
- import androidx.compose.material.icons.filled.AccountBox
- import androidx.compose.material.icons.filled.Add
- import androidx.compose.material.icons.filled.Delete
- import androidx.compose.material.icons.filled.Refresh
- import androidx.compose.material3.Card
- import androidx.compose.material3.CardDefaults
- import androidx.compose.material3.ExperimentalMaterial3Api
- import androidx.compose.material3.FloatingActionButton
- import androidx.compose.material3.Icon
- import androidx.compose.material3.IconButton
- import androidx.compose.material3.MaterialTheme
- import androidx.compose.material3.Scaffold
- import androidx.compose.material3.Surface
- import androidx.compose.material3.Text
- import androidx.compose.material3.TopAppBar
- import androidx.compose.material3.TopAppBarDefaults
- import androidx.compose.runtime.Composable
- import androidx.compose.runtime.getValue
- import androidx.compose.runtime.mutableStateListOf
- import androidx.compose.runtime.mutableStateOf
- import androidx.compose.runtime.remember
- import androidx.compose.runtime.setValue
- import androidx.compose.ui.Alignment
- import androidx.compose.ui.Modifier
- import androidx.compose.ui.text.font.FontWeight
- import androidx.compose.ui.tooling.preview.Preview
- import androidx.compose.ui.unit.dp
- import com.example.scaffolddemo.ui.theme.ScaffoldDemoTheme
- class MainActivity : ComponentActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- enableEdgeToEdge()
- setContent {
- TaskManagerApp()
- }
- }
- }
- @OptIn(ExperimentalMaterial3Api::class)
- @Composable
- fun TaskManagerApp() {
- var taskCounter by remember { mutableStateOf(1) }
- var taskList = remember { mutableStateListOf<String>() }
- Scaffold(
- topBar = { TaskAppBar() },
- floatingActionButton = {
- FloatingActionButton(
- onClick = {
- taskList.add("Task $taskCounter")
- taskCounter++
- },
- containerColor = MaterialTheme.colorScheme.primary
- ) {
- Icon(imageVector = Icons.Filled.Add, contentDescription = "Add Task")
- }
- },
- content = { paddingValues ->
- Surface(
- modifier = Modifier
- .fillMaxSize()
- .padding(paddingValues)
- .padding(16.dp)
- ) {
- TaskList(taskList) { task -> taskList.remove(task)}
- }
- }
- )
- }
- @OptIn(ExperimentalMaterial3Api::class)
- @Composable
- fun TaskAppBar() {
- TopAppBar(
- title = { Text(text = "Task Manager", fontWeight = FontWeight.Bold) },
- actions = {
- IconButton(onClick = { }) {
- Icon(
- imageVector = Icons.Filled.Refresh,
- contentDescription = "Clear All Tasks"
- )
- }
- IconButton(onClick = { }) {
- Icon(
- imageVector = Icons.Filled.AccountBox,
- contentDescription = "Clear All Tasks"
- )
- }
- },
- colors = TopAppBarDefaults.topAppBarColors(MaterialTheme.colorScheme.primaryContainer)
- )
- }
- @Composable
- fun TaskList(tasks: List<String>, onDelete: (String) -> Unit) {
- LazyColumn {
- items(tasks) {
- task -> TaskItem(task, onDelete)
- }
- }
- }
- @Composable
- fun TaskItem(task: String, onDelete: (String) -> Unit) {
- Card(
- modifier = Modifier
- .fillMaxSize()
- .padding(8.dp),
- elevation = CardDefaults.cardElevation(defaultElevation = 4.dp)
- ) {
- Row(
- modifier = Modifier
- .fillMaxWidth()
- .padding(16.dp),
- verticalAlignment = Alignment.CenterVertically,
- horizontalArrangement = Arrangement.SpaceBetween
- ) {
- Text(text = task, fontWeight = FontWeight.Medium)
- IconButton(onClick = { onDelete(task) }) {
- Icon(imageVector = Icons.Filled.Delete, contentDescription = "Delete Task")
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement