feat(prescription): expand canvas to fill bottom space and add cursor-aware newline on bullet insertion

This commit is contained in:
=
2026-09-22 14:15:44 -04:00
parent 654a7f402d
commit 6b3b606fec
2 changed files with 376 additions and 306 deletions
@@ -72,25 +72,44 @@ class _PrescriptionModalViewState extends State<PrescriptionModalView> {
void _insertBullet() { void _insertBullet() {
final text = _prescriptionController.text; final text = _prescriptionController.text;
final bullet = text.isEmpty || text.endsWith('\n') ? '• ' : '\n• '; final selection = _prescriptionController.selection;
int start = selection.start;
if (start < 0 || start > text.length) start = text.length;
final textBefore = text.substring(0, start);
final needNewline = textBefore.isNotEmpty && !textBefore.endsWith('\n');
final bullet = needNewline ? '\n• ' : '• ';
_insertText(bullet); _insertText(bullet);
} }
void _insertNextNumber() { void _insertNextNumber() {
final text = _prescriptionController.text; final text = _prescriptionController.text;
final selection = _prescriptionController.selection;
int start = selection.start;
if (start < 0 || start > text.length) start = text.length;
final matches = RegExp(r'(?:^|\n)\s*(\d+)\.').allMatches(text); final matches = RegExp(r'(?:^|\n)\s*(\d+)\.').allMatches(text);
int nextNumber = 1; int nextNumber = 1;
if (matches.isNotEmpty) { if (matches.isNotEmpty) {
final lastNum = int.tryParse(matches.last.group(1) ?? '0') ?? 0; final lastNum = int.tryParse(matches.last.group(1) ?? '0') ?? 0;
nextNumber = lastNum + 1; nextNumber = lastNum + 1;
} }
final prefix = text.isEmpty || text.endsWith('\n') ? '$nextNumber. ' : '\n$nextNumber. ';
final textBefore = text.substring(0, start);
final needNewline = textBefore.isNotEmpty && !textBefore.endsWith('\n');
final prefix = needNewline ? '\n$nextNumber. ' : '$nextNumber. ';
_insertText(prefix); _insertText(prefix);
} }
void _insertSubIndication() { void _insertSubIndication() {
final text = _prescriptionController.text; final text = _prescriptionController.text;
final prefix = text.isEmpty || text.endsWith('\n') ? ' - ' : '\n - '; final selection = _prescriptionController.selection;
int start = selection.start;
if (start < 0 || start > text.length) start = text.length;
final textBefore = text.substring(0, start);
final needNewline = textBefore.isNotEmpty && !textBefore.endsWith('\n');
final prefix = needNewline ? '\n - ' : ' - ';
_insertText(prefix); _insertText(prefix);
} }
@@ -180,12 +199,15 @@ class _PrescriptionModalViewState extends State<PrescriptionModalView> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final formattedDate = DateFormat('dd/MM/yyyy').format(DateTime.now()); final formattedDate = DateFormat('dd/MM/yyyy').format(DateTime.now());
final mediaQuery = MediaQuery.sizeOf(context);
final modalHeight = (mediaQuery.height * 0.90).clamp(650.0, 780.0);
final modalWidth = (mediaQuery.width * 0.90).clamp(840.0, 920.0);
return Observer( return Observer(
builder: (_) { builder: (_) {
return MedicalModalDialog( return MedicalModalDialog(
maxWidth: 820, maxWidth: modalWidth,
maxHeight: 650, maxHeight: modalHeight,
title: 'Emisión de Receta Médica', title: 'Emisión de Receta Médica',
titleIcon: Container( titleIcon: Container(
padding: const EdgeInsets.all(8), padding: const EdgeInsets.all(8),
@@ -292,164 +314,11 @@ class _PrescriptionModalViewState extends State<PrescriptionModalView> {
], ],
), ),
), ),
child: SingleChildScrollView( child: LayoutBuilder(
padding: const EdgeInsets.all(20), builder: (context, constraints) {
child: Column( final isCompact = constraints.maxHeight < 420;
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// Membrete Informativo del Médico (Vista preliminar institucional)
Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: const Color(0xFFF8FAFC),
borderRadius: BorderRadius.circular(12),
border: Border.all(color: const Color(0xFFE2E8F0)),
),
child: Column(
children: [
Text(
_configStore.nombreDoctor.isNotEmpty
? _configStore.nombreDoctor
: 'Dr. Luis A. Castro Sosa',
textAlign: TextAlign.center,
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.w700,
color: AppColors.textPrimary,
letterSpacing: -0.3,
),
),
if (_configStore.especialidad.isNotEmpty) ...[
const SizedBox(height: 2),
Text(
_configStore.especialidad.toUpperCase(),
textAlign: TextAlign.center,
style: const TextStyle(
fontSize: 11.5,
fontWeight: FontWeight.w600,
color: AppColors.primary,
letterSpacing: 0.5,
),
),
],
if (_configStore.descripEspecialidad.isNotEmpty) ...[
const SizedBox(height: 2),
Text(
_configStore.descripEspecialidad.toUpperCase(),
textAlign: TextAlign.center,
style: const TextStyle(
fontSize: 10,
color: AppColors.textMuted,
),
),
],
const SizedBox(height: 4),
Text(
'Fecha: $formattedDate',
style: const TextStyle(
fontSize: 11,
fontWeight: FontWeight.w500,
color: AppColors.textSecondary,
),
),
],
),
),
const SizedBox(height: 16),
// Campo Paciente final editorWidget = Container(
Row(
children: [
Container(
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 8),
decoration: BoxDecoration(
color: const Color(0xFF00A65A).withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(8),
),
child: const Text(
'Rx',
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w800,
color: Color(0xFF00A65A),
),
),
),
const SizedBox(width: 12),
Expanded(
child: TextField(
controller: _patientController,
decoration: InputDecoration(
labelText: 'Paciente',
hintText: 'Nombre del paciente',
isDense: true,
floatingLabelBehavior: FloatingLabelBehavior.always,
filled: true,
fillColor: Colors.white,
contentPadding: const EdgeInsets.symmetric(horizontal: 12, vertical: 12),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: const BorderSide(color: Color(0xFFE2E8F0)),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: const BorderSide(color: Color(0xFFE2E8F0)),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: const BorderSide(color: AppColors.primary, width: 1.5),
),
),
style: const TextStyle(fontSize: 13, fontWeight: FontWeight.w600),
),
),
],
),
const SizedBox(height: 16),
// Sección de Medicamentos y Redacción Inteligente
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Text(
'Medicamentos e Indicaciones:',
style: TextStyle(
fontSize: 13,
fontWeight: FontWeight.w700,
color: AppColors.textPrimary,
),
),
ValueListenableBuilder<TextEditingValue>(
valueListenable: _prescriptionController,
builder: (context, val, _) {
if (val.text.isEmpty) return const SizedBox.shrink();
return InkWell(
onTap: () {
_prescriptionController.clear();
},
borderRadius: BorderRadius.circular(4),
child: const Padding(
padding: EdgeInsets.symmetric(horizontal: 6, vertical: 2),
child: Row(
children: [
Icon(Icons.clear_all_rounded, size: 14, color: AppColors.textMuted),
SizedBox(width: 4),
Text(
'Limpiar lienzo',
style: TextStyle(fontSize: 11, color: AppColors.textMuted, fontWeight: FontWeight.w500),
),
],
),
),
);
},
),
],
),
const SizedBox(height: 8),
// Editor de Receta con Toolbar Inteligente Integrada
Container(
decoration: BoxDecoration( decoration: BoxDecoration(
color: Colors.white, color: Colors.white,
borderRadius: BorderRadius.circular(10), borderRadius: BorderRadius.circular(10),
@@ -601,11 +470,29 @@ class _PrescriptionModalViewState extends State<PrescriptionModalView> {
), ),
// Lienzo de prescripción // Lienzo de prescripción
if (isCompact)
TextField( TextField(
controller: _prescriptionController, controller: _prescriptionController,
focusNode: _prescriptionFocusNode, focusNode: _prescriptionFocusNode,
maxLines: 12, maxLines: 10,
minLines: 7, minLines: 5,
style: const TextStyle(fontSize: 13.5, height: 1.5, color: AppColors.textPrimary),
decoration: const InputDecoration(
hintText: '1. Ciprofloxacina 500mg (14 tabletas)\n - Tomar 1 tableta cada 12 horas por 7 días con abundante agua.\n\n2. Tamsulosina 0.4mg (30 cápsulas)\n - Tomar 1 cápsula cada 24 horas después de la cena.',
hintStyle: TextStyle(color: Color(0xFF94A3B8), fontSize: 12.5),
border: InputBorder.none,
contentPadding: EdgeInsets.all(14),
),
)
else
Expanded(
child: TextField(
controller: _prescriptionController,
focusNode: _prescriptionFocusNode,
maxLines: null,
minLines: null,
expands: true,
textAlignVertical: TextAlignVertical.top,
style: const TextStyle(fontSize: 13.5, height: 1.5, color: AppColors.textPrimary), style: const TextStyle(fontSize: 13.5, height: 1.5, color: AppColors.textPrimary),
decoration: const InputDecoration( decoration: const InputDecoration(
hintText: '1. Ciprofloxacina 500mg (14 tabletas)\n - Tomar 1 tableta cada 12 horas por 7 días con abundante agua.\n\n2. Tamsulosina 0.4mg (30 cápsulas)\n - Tomar 1 cápsula cada 24 horas después de la cena.', hintText: '1. Ciprofloxacina 500mg (14 tabletas)\n - Tomar 1 tableta cada 12 horas por 7 días con abundante agua.\n\n2. Tamsulosina 0.4mg (30 cápsulas)\n - Tomar 1 cápsula cada 24 horas después de la cena.',
@@ -614,12 +501,183 @@ class _PrescriptionModalViewState extends State<PrescriptionModalView> {
contentPadding: EdgeInsets.all(14), contentPadding: EdgeInsets.all(14),
), ),
), ),
),
], ],
), ),
);
final bodyContent = Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// Membrete Informativo del Médico (Vista preliminar institucional)
Container(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
decoration: BoxDecoration(
color: const Color(0xFFF8FAFC),
borderRadius: BorderRadius.circular(10),
border: Border.all(color: const Color(0xFFE2E8F0)),
),
child: Column(
children: [
Text(
_configStore.nombreDoctor.isNotEmpty
? _configStore.nombreDoctor
: 'Dr. Luis A. Castro Sosa',
textAlign: TextAlign.center,
style: const TextStyle(
fontSize: 17,
fontWeight: FontWeight.w700,
color: AppColors.textPrimary,
letterSpacing: -0.3,
),
),
if (_configStore.especialidad.isNotEmpty) ...[
const SizedBox(height: 2),
Text(
_configStore.especialidad.toUpperCase(),
textAlign: TextAlign.center,
style: const TextStyle(
fontSize: 11,
fontWeight: FontWeight.w600,
color: AppColors.primary,
letterSpacing: 0.5,
),
),
],
if (_configStore.descripEspecialidad.isNotEmpty) ...[
const SizedBox(height: 1),
Text(
_configStore.descripEspecialidad.toUpperCase(),
textAlign: TextAlign.center,
style: const TextStyle(
fontSize: 9.5,
color: AppColors.textMuted,
),
),
],
const SizedBox(height: 3),
Text(
'Fecha: $formattedDate',
style: const TextStyle(
fontSize: 10.5,
fontWeight: FontWeight.w500,
color: AppColors.textSecondary,
),
), ),
], ],
), ),
), ),
const SizedBox(height: 12),
// Campo Paciente
Row(
children: [
Container(
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 8),
decoration: BoxDecoration(
color: const Color(0xFF00A65A).withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(8),
),
child: const Text(
'Rx',
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w800,
color: Color(0xFF00A65A),
),
),
),
const SizedBox(width: 12),
Expanded(
child: TextField(
controller: _patientController,
decoration: InputDecoration(
labelText: 'Paciente',
hintText: 'Nombre del paciente',
isDense: true,
floatingLabelBehavior: FloatingLabelBehavior.always,
filled: true,
fillColor: Colors.white,
contentPadding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: const BorderSide(color: Color(0xFFE2E8F0)),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: const BorderSide(color: Color(0xFFE2E8F0)),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: const BorderSide(color: AppColors.primary, width: 1.5),
),
),
style: const TextStyle(fontSize: 13, fontWeight: FontWeight.w600),
),
),
],
),
const SizedBox(height: 12),
// Sección de Medicamentos y Redacción Inteligente
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Text(
'Medicamentos e Indicaciones:',
style: TextStyle(
fontSize: 13,
fontWeight: FontWeight.w700,
color: AppColors.textPrimary,
),
),
ValueListenableBuilder<TextEditingValue>(
valueListenable: _prescriptionController,
builder: (context, val, _) {
if (val.text.isEmpty) return const SizedBox.shrink();
return InkWell(
onTap: () {
_prescriptionController.clear();
},
borderRadius: BorderRadius.circular(4),
child: const Padding(
padding: EdgeInsets.symmetric(horizontal: 6, vertical: 2),
child: Row(
children: [
Icon(Icons.clear_all_rounded, size: 14, color: AppColors.textMuted),
SizedBox(width: 4),
Text(
'Limpiar lienzo',
style: TextStyle(fontSize: 11, color: AppColors.textMuted, fontWeight: FontWeight.w500),
),
],
),
),
);
},
),
],
),
const SizedBox(height: 8),
// Editor de Receta (Expanded para rellenar toda la altura o scrollable en pantallas pequeñas)
if (isCompact) editorWidget else Expanded(child: editorWidget),
],
);
if (isCompact) {
return SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: bodyContent,
);
}
return Padding(
padding: const EdgeInsets.fromLTRB(20, 14, 20, 16),
child: bodyContent,
);
},
),
); );
}, },
); );
+13 -1
View File
@@ -79,7 +79,7 @@ void main() {
expect(find.text('ESC para salir'), findsOneWidget); expect(find.text('ESC para salir'), findsOneWidget);
}); });
testWidgets('inserts bullet when clicking viñeta button', (tester) async { testWidgets('inserts bullet when clicking viñeta button and jumps to new line if text has content', (tester) async {
final configStore = ConfigStore(); final configStore = ConfigStore();
await tester.pumpWidget( await tester.pumpWidget(
MaterialApp( MaterialApp(
@@ -100,6 +100,18 @@ void main() {
await tester.pump(); await tester.pump();
expect(find.text('• '), findsOneWidget); expect(find.text('• '), findsOneWidget);
// Now enter text at end of line and test auto-newline
final prescriptionField = find.byWidgetPredicate(
(w) => w is TextField && (w.decoration?.hintText?.contains('Ciprofloxacina') ?? false),
);
await tester.enterText(prescriptionField, '1. Ciprofloxacina 500mg');
await tester.pump();
await tester.tap(bulletBtn);
await tester.pump();
expect(find.text('1. Ciprofloxacina 500mg\n• '), findsOneWidget);
}); });
testWidgets('inserts sequential numbers and sub-indication dosis', (tester) async { testWidgets('inserts sequential numbers and sub-indication dosis', (tester) async {