Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/core/utils/utilities.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ extension RotateList<T> on List<T> {
List<T> rotate(int count) => count > 0 ? (sublist(count)..addAll(sublist(0, count))) : (sublist(length + count)..addAll(sublist(0, length + count)));
}

/// Esta extensión permite insertar un elemento entre cada elemento de la lista, excepto al final.
/// Ejemplo:
/// ```dart
/// List<String> lista = ["a", "b", "c"];
/// print(lista.intersperse("-")); // ["a", "-", "b", "-", "c"]
/// ```
extension IntersperseList<T> on List<T> {
List<T> intersperse(T element) => length > 1 ? expand((e) sync* {
yield e;
if (e != last) yield element;
}).toList() : this;
}

/// Capitaliza el texto entregado
/// Ejemplo:
/// ```dart
Expand Down
137 changes: 59 additions & 78 deletions lib/screens/notas/widgets/notas.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import "package:collection/collection.dart";
import "package:flutter/material.dart";
import "package:flutter/services.dart";
import "package:get/get.dart";
import "package:miutem/core/models/evaluacion/evaluacion.dart";
import "package:miutem/core/services/controllers/notas_controller.dart";
import "package:miutem/core/utils/utils.dart";
import "package:miutem/screens/notas/widgets/notas/fila_nota.dart";
import "package:miutem/styles/styles.dart";

class Notas extends StatelessWidget {

final bool canAddNotas;
final NotasController notasController;

Expand All @@ -20,89 +20,70 @@ class Notas extends StatelessWidget {
@override
Widget build(BuildContext context) => Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
children: [
Text("Notas", style: Theme.of(context).textTheme.bodyMedium),
const SizedBox(height: 12),
SizedBox(
width: double.infinity,
child: Card(
color: Theme.of(context).scaffoldBackgroundColor,
margin: EdgeInsets.zero,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10), side: BorderSide(color: AppTheme.lightGrey)),
child: Padding(
padding: const EdgeInsets.all(20),
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Obx(() => GridView(
shrinkWrap: true,
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
childAspectRatio: 3,
mainAxisSpacing: 12,
crossAxisSpacing: 12,
),
Space.small,
Card(
margin: EdgeInsets.zero,
color: Theme.of(context).scaffoldBackgroundColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
side: BorderSide(color: AppTheme.lightGrey),
),
child: Padding(
padding: const EdgeInsets.all(20),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
// Usamos Obx solo para la parte que cambia
Obx(
() => Column(
children: [
const Center(child: Text("Notas")),
const Center(child: Text("Porcentaje")),
const SizedBox.shrink(),
for (int i = 0; i < notasController.percentageTextFieldControllers.length; i++) ...[
_buildTextField(context: context, enabled: true, controller: notasController.gradeTextFieldControllers[i], textInputAction: TextInputAction.next, hintText: formatoNota(notasController.suggestedGrade), formatters: [notaInputFormatter], onChanged: (value) {
final grade = notasController.partialGrades[i];
grade.nota = double.tryParse(value.replaceAll(",", "."));
notasController.updateGradeAt(i, grade);
}),
_buildTextField(context: context,enabled: true, controller: notasController.percentageTextFieldControllers[i], textInputAction: TextInputAction.done, hintText: notasController.suggestedPercentage?.toStringAsFixed(0) ?? "--", onChanged: (value) {
final grade = notasController.partialGrades[i];
grade.porcentaje = double.tryParse(value.replaceAll(",", ".")) ?? 0;
notasController.updateGradeAt(i, grade);
}),
IconButton(onPressed: () => notasController.removeGradeAt(i), icon: const Icon(AppIcons.delete, size: 20)),
],
const SizedBox.shrink(),
SizedBox.expand(child: Center(child: FilledButton.tonalIcon(icon: const Icon(AppIcons.add), label: const Text("Nota"), onPressed: () {
if(!canAddNotas) {
showErrorSnackbar(context, "Las notas están cargando... Intenta más tarde.");
return;
}
// Encabezados manuales para evitar el GridView rígido
const Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(child: Center(child: Text("Notas"))),
HorizontalSpace.small,
Expanded(child: Center(child: Text("Porcentaje"))),
HorizontalSpace.extraExtraLarge,
],
),
HorizontalSpace.small,

// Filas de Notas
...notasController.percentageTextFieldControllers
.mapIndexed<Widget>(
(i, controller) => FilaNota(
notasController: notasController,
index: i,
),
)
.toList()
.intersperse(Space.small),

notasController.addGrade(IEvaluacion());
}))),
const SizedBox.shrink(),
Space.extraSmall,
FilledButton.tonalIcon(
onPressed: () {
if (!canAddNotas) {
showErrorSnackbar(
context,
"Las notas están cargando... Intenta más tarde.",
);
return;
}
notasController.addGrade(IEvaluacion());
},
icon: const Icon(AppIcons.add),
label: const Text("Agregar Nota"),
),
],
)),
],
),
),
),
],
),
),
),
],
);

Widget _buildTextField({
required BuildContext context,
required bool enabled,
required TextEditingController controller,
required TextInputAction textInputAction,
required String? hintText,
Function(String)? onChanged,
List<TextInputFormatter>? formatters,
}) => TextField(
enabled: enabled,
controller: controller,
style: Theme.of(context).textTheme.bodyMedium,
decoration: InputDecoration(
hintText: hintText ?? "--",
filled: true,
),
textAlign: TextAlign.center,
textAlignVertical: TextAlignVertical.center,
onChanged: onChanged,
textInputAction: textInputAction,
inputFormatters: formatters,

);
}
}
78 changes: 78 additions & 0 deletions lib/screens/notas/widgets/notas/fila_nota.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import "package:flutter/material.dart";
import "package:flutter/services.dart";
import "package:miutem/core/services/controllers/notas_controller.dart";
import "package:miutem/core/utils/utils.dart";
import "package:miutem/styles/styles.dart";

class FilaNota extends StatelessWidget {
final NotasController notasController;
final int index;

const FilaNota({
super.key,
required this.notasController,
required this.index,
});

@override
Widget build(BuildContext context) => Row(
children: [
Expanded(
child: _buildTextField(
context: context,
enabled: true,
controller: notasController.gradeTextFieldControllers[index],
textInputAction: TextInputAction.next,
hintText: formatoNota(notasController.suggestedGrade),
formatters: [notaInputFormatter],
onChanged: (value) {
final grade = notasController.partialGrades[index];
grade.nota = double.tryParse(value.replaceAll(",", "."));
notasController.updateGradeAt(index, grade);
},
),
),
HorizontalSpace.small,
Expanded(
child: _buildTextField(
context: context,
enabled: true,
controller: notasController.percentageTextFieldControllers[index],
textInputAction: TextInputAction.done,
hintText:
notasController.suggestedPercentage?.toStringAsFixed(0) ?? "--",
onChanged: (value) {
final grade = notasController.partialGrades[index];
grade.porcentaje = double.tryParse(value.replaceAll(",", ".")) ?? 0;
notasController.updateGradeAt(index, grade);
},
),
),
IconButton(
onPressed: () => notasController.removeGradeAt(index),
icon: const Icon(AppIcons.delete, size: 20),
color: Theme.of(context).textTheme.bodyMedium?.color,
),
],
);

Widget _buildTextField({
required BuildContext context,
required bool enabled,
required TextEditingController controller,
required TextInputAction textInputAction,
required String? hintText,
Function(String)? onChanged,
List<TextInputFormatter>? formatters,
}) => TextField(
enabled: enabled,
controller: controller,
style: Theme.of(context).textTheme.bodyMedium,
decoration: InputDecoration(hintText: hintText ?? "--", filled: true),
textAlign: TextAlign.center,
textAlignVertical: TextAlignVertical.center,
onChanged: onChanged,
textInputAction: textInputAction,
inputFormatters: formatters,
);
}
2 changes: 2 additions & 0 deletions lib/styles/theme/space.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class Space {
}

class HorizontalSpace {
/// 48 PX para separar secciones muy distintas o elementos muy importantes
static const Widget extraExtraLarge = SizedBox(width: 48);
/// 28 PX para separar secciones muy distintas o elementos muy importantes
static const Widget extraLarge = SizedBox(width: 28);
/// 20 PX para separar elementos muy distintos o secciones
Expand Down
Loading