import 'package:flutter/material.dart'; class MoodTextAreaWidget extends StatefulWidget { final String initialText; final bool isDisabled; final bool hasPrefix; final bool autoFocus; final bool forceBlinkingCursor; final TextEditingController? controller; const MoodTextAreaWidget({ Key? key, this.initialText = '', this.isDisabled = false, this.hasPrefix = false, this.autoFocus = false, this.forceBlinkingCursor = false, this.controller, }) : super(key: key); @override State createState() => _MoodTextAreaWidget(); } class _MoodTextAreaWidget extends State { late TextEditingController _controller; final FocusNode _focusNode = FocusNode(); late bool _isEditingEnabled; @override void initState() { super.initState(); //String initialText = widget.hasPrefix ? 'why? ' + widget.initialText : widget.initialText; _controller = widget.controller ?? TextEditingController(text: widget.hasPrefix ? 'why? ${widget.initialText}' : widget.initialText); _isEditingEnabled = !widget.isDisabled; _focusNode.addListener(() { if (!_focusNode.hasFocus) { setState(() { // Remove cursor when focus is lost if (!widget.forceBlinkingCursor) { _controller.text = _controller.text.replaceAll('|', ''); } }); } }); if (widget.autoFocus) { WidgetsBinding.instance.addPostFrameCallback((_) { if (_isEditingEnabled) { _focusNode.requestFocus(); } }); } } void enableEditing() { setState(() { _isEditingEnabled = true; _focusNode.requestFocus(); }); } void disableEditing() { setState(() { _isEditingEnabled = false; _focusNode.unfocus(); }); } @override Widget build(BuildContext context) { if (!_isEditingEnabled) _controller.text = widget.initialText; return TextField( controller: _controller, focusNode: _focusNode, keyboardType: TextInputType.multiline, maxLines: null, enabled: _isEditingEnabled, cursorColor: widget.forceBlinkingCursor ? Colors.black : Colors.transparent, style: const TextStyle(color: Colors.black, fontSize: 18), decoration: const InputDecoration( border: InputBorder.none, ), onChanged: (value) { if (widget.hasPrefix && !value.startsWith('warum? ')) { _controller.text = _isEditingEnabled ? 'warum? $value' : value; _controller.selection = TextSelection.fromPosition( TextPosition(offset: _controller.text.length), ); } }, ); } @override void dispose() { if (widget.controller == null) { _controller.dispose(); } _focusNode.dispose(); super.dispose(); } }