I am facing an issue with integrating the flutter_quill: ^9.5.3 library in my Flutter application. I receive a string containing HTML tags from my backend (BO) after mapping the data. My goal is to display this HTML content correctly and allow the user to edit it, then send the modified string back to the server.
Here is the code I am currently using:
class DescriptionHTMLEditText extends StatefulWidget {
final String title;
final String text;
final Function(String) onChanged;
final bool isEditable;
const DescriptionHTMLEditText({
super.key,
required this.title,
required this.text,
required this.onChanged,
required this.isEditable,
});
@override
State<DescriptionHTMLEditText> createState() =>
_DescriptionHTMLEditTextState();
}
class _DescriptionHTMLEditTextState extends State<DescriptionHTMLEditText> {
bool editing = true;
final textController = TextEditingController();
@override
void initState() {
super.initState();
textController.text = widget.text;
}
@override
void dispose() {
textController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
key: const Key('description-row-readonly'),
children: [
Text(
widget.title,
style: Theme.of(context).textTheme.titleSmall,
),
if (editing && widget.isEditable)
TextButton(
onPressed: () {
setState(() {
editing = !editing;
});
},
child: Text("Modify"),
)
],
),
AnimatedSwitcher(
duration: const Duration(milliseconds: 250),
child: !editing
? Column(
key: const Key('description-row-editable'),
children: [
TextFormField(
controller: textController,
minLines: 3,
maxLines: 60,
),
const SizedBox(height: 4),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
ElevatedButton(
onPressed: () {
setState(() {
editing = !editing;
});
},
child: Text("Cancel"),
),
ElevatedButton(
onPressed: () async {
setState(() {
editing = !editing;
});
widget.onChanged(textController.text);
},
child: Text("Save"),
),
],
),
],
)
: Column(
children: [
Text(widget.text.isEmpty
? "No description available"
: widget.text),
],
),
),
],
);
}
}
Currently, this code allows displaying the received text and modifying it through a text form field. However, it does not render the HTML properly, nor does it allow HTML content editing in a user-friendly way.
I am seeking advice on how to:
Display the HTML content properly formatted.
Enable editing of this HTML content.
Send back the modified text in HTML format.
I have explored a few options but haven’t found a concrete solution yet. I understand that flutter_quill is a powerful library for rich text editing, but I am not sure how to integrate it for this specific use case.
Has anyone successfully implemented a similar feature with flutter_quill or any other approach? I would greatly appreciate any examples, suggestions, or guidance on how to achieve this.
Thank you in advance for your help!