Files
zaloopipe/frontend/lib/widgets/prompt_editor.dart
T

98 lines
2.5 KiB
Dart

import 'package:flutter/material.dart';
import '../api/models.dart';
class PromptEditor extends StatefulWidget {
final Prompt prompt;
final ValueChanged<Prompt> onChanged;
final VoidCallback? onDelete;
final bool showDelete;
const PromptEditor({
super.key,
required this.prompt,
required this.onChanged,
this.onDelete,
this.showDelete = true,
});
@override
State<PromptEditor> createState() => _PromptEditorState();
}
class _PromptEditorState extends State<PromptEditor> {
late TextEditingController textController;
late TextEditingController titleController;
int order = 0;
@override
void initState() {
super.initState();
textController = TextEditingController(text: widget.prompt.text);
titleController = TextEditingController(text: widget.prompt.title);
order = widget.prompt.order;
}
@override
void dispose() {
textController.dispose();
titleController.dispose();
super.dispose();
}
void _notifyChanged() {
widget.onChanged(
widget.prompt.copyWith(
text: textController.text,
title: titleController.text,
order: order,
),
);
}
@override
Widget build(BuildContext context) {
return Card(
margin: const EdgeInsets.only(bottom: 12),
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Chip(label: Text('Step ${order + 1}')),
const Spacer(),
if (widget.showDelete && widget.onDelete != null)
IconButton(
icon: const Icon(Icons.close, size: 20),
onPressed: widget.onDelete,
tooltip: 'Remove prompt',
),
],
),
TextField(
controller: titleController,
decoration: const InputDecoration(
labelText: 'Title',
border: OutlineInputBorder(),
isDense: true,
),
onChanged: (_) => _notifyChanged(),
),
const SizedBox(height: 8),
TextField(
controller: textController,
decoration: const InputDecoration(
labelText: 'Prompt Text',
border: OutlineInputBorder(),
),
maxLines: 4,
onChanged: (_) => _notifyChanged(),
),
],
),
),
);
}
}