
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
package com.example.myapplication import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.activity.enableEdgeToEdge import androidx.compose.animation.AnimatedVisibility import androidx.compose.animation.core.tween import androidx.compose.animation.fadeIn import androidx.compose.animation.slideInVertically import androidx.compose.foundation.layout.* import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.itemsIndexed import androidx.compose.material3.* import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import com.example.myapplication.ui.theme.MyApplicationTheme import kotlinx.coroutines.delay class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setContent { MyApplicationTheme { MultiplicationApp() } } } } @OptIn(ExperimentalMaterial3Api::class) @Composable fun MultiplicationApp() { var number by remember { mutableStateOf("") } var table by remember { mutableStateOf<List<String>>(emptyList()) } var visibleItems by remember { mutableStateOf(0) } Scaffold( topBar = { TopAppBar( title = { Text("Tablas de multiplicar", fontWeight = FontWeight.Bold) }, colors = TopAppBarDefaults.topAppBarColors( containerColor = MaterialTheme.colorScheme.primaryContainer, titleContentColor = MaterialTheme.colorScheme.onPrimaryContainer ) ) } ) { innerPadding -> Column( modifier = Modifier .padding(innerPadding) .padding(16.dp) .fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally ) { Text( text = "Introduce un número para ver su tabla de multiplicar:", fontSize = 18.sp, modifier = Modifier.padding(bottom = 8.dp) ) OutlinedTextField( value = number, onValueChange = { input -> if (input.all { it.isDigit() }) number = input }, label = { Text("Número") }, singleLine = true, modifier = Modifier.fillMaxWidth() ) Spacer(modifier = Modifier.height(12.dp)) Button( onClick = { val n = number.toIntOrNull() if (n != null) { table = (1..10).map { "$n × $it = ${n * it}" } visibleItems = 0 } else { table = emptyList() } }, modifier = Modifier.fillMaxWidth(), enabled = number.isNotEmpty() ) { Text("Generar tabla") } Spacer(modifier = Modifier.height(16.dp)) if (table.isNotEmpty()) { Text( text = "Tabla del ${number.toInt()}", fontSize = 22.sp, fontWeight = FontWeight.Bold, color = MaterialTheme.colorScheme.primary ) Spacer(modifier = Modifier.height(8.dp)) LazyColumn( modifier = Modifier .fillMaxWidth() .weight(1f), horizontalAlignment = Alignment.CenterHorizontally ) { itemsIndexed(table) { index, item -> // Animar la aparición progresiva de cada línea LaunchedEffect(table) { delay(index * 100L) visibleItems = index + 1 } AnimatedVisibility( visible = index < visibleItems, enter = fadeIn(animationSpec = tween(400)) + slideInVertically(initialOffsetY = { it / 2 }, animationSpec = tween(400)) ) { Card( modifier = Modifier .padding(vertical = 4.dp) .fillMaxWidth(), colors = CardDefaults.cardColors( containerColor = MaterialTheme.colorScheme.surfaceVariant ), elevation = CardDefaults.cardElevation(defaultElevation = 2.dp) ) { Text( text = item, fontSize = 20.sp, modifier = Modifier .padding(12.dp) .align(Alignment.CenterHorizontally) ) } } } } } else { Text( text = "Aún no has generado ninguna tabla.", fontSize = 16.sp, color = MaterialTheme.colorScheme.onSurfaceVariant, modifier = Modifier.padding(top = 16.dp) ) } } } } |

