2023-12-17 23:00:31 +01:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
class MoodTextAreaWidget extends StatefulWidget {
|
|
|
|
final String initialText;
|
|
|
|
final bool isDisabled;
|
|
|
|
final bool hasPrefix;
|
|
|
|
final bool autoFocus;
|
|
|
|
final bool forceBlinkingCursor;
|
2024-01-08 19:34:48 +01:00
|
|
|
final TextEditingController? controller;
|
2023-12-17 23:00:31 +01:00
|
|
|
|
|
|
|
MoodTextAreaWidget({
|
|
|
|
Key? key,
|
|
|
|
this.initialText = '',
|
|
|
|
this.isDisabled = false,
|
|
|
|
this.hasPrefix = false,
|
|
|
|
this.autoFocus = false,
|
|
|
|
this.forceBlinkingCursor = false,
|
2024-01-08 19:34:48 +01:00
|
|
|
this.controller,
|
2023-12-17 23:00:31 +01:00
|
|
|
}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
_MoodTextAreaWidgetState createState() => _MoodTextAreaWidgetState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _MoodTextAreaWidgetState extends State<MoodTextAreaWidget> {
|
|
|
|
late TextEditingController _controller;
|
|
|
|
final FocusNode _focusNode = FocusNode();
|
|
|
|
late bool _isEditingEnabled;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2024-01-08 19:34:48 +01:00
|
|
|
//String initialText = widget.hasPrefix ? 'why? ' + widget.initialText : widget.initialText;
|
|
|
|
_controller = widget.controller ?? TextEditingController(text: widget.hasPrefix ? 'why? ${widget.initialText}' : widget.initialText);
|
2023-12-17 23:00:31 +01:00
|
|
|
_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) {
|
2024-01-08 19:34:48 +01:00
|
|
|
print("initialtext######${widget.initialText}");
|
|
|
|
if (!_isEditingEnabled) _controller.text = widget.initialText;
|
2023-12-17 23:00:31 +01:00
|
|
|
return TextField(
|
|
|
|
controller: _controller,
|
|
|
|
focusNode: _focusNode,
|
|
|
|
keyboardType: TextInputType.multiline,
|
|
|
|
maxLines: null,
|
|
|
|
enabled: _isEditingEnabled,
|
2024-01-08 19:34:48 +01:00
|
|
|
cursorColor: widget.forceBlinkingCursor ? Colors.black : Colors.transparent,
|
|
|
|
style: const TextStyle(color: Colors.black, fontSize: 18),
|
|
|
|
decoration: const InputDecoration(
|
2023-12-17 23:00:31 +01:00
|
|
|
border: InputBorder.none,
|
|
|
|
),
|
|
|
|
onChanged: (value) {
|
2024-01-08 19:34:48 +01:00
|
|
|
if (widget.hasPrefix && !value.startsWith('warum? ')) {
|
|
|
|
_controller.text = _isEditingEnabled ? 'warum? $value' : value;
|
2023-12-17 23:00:31 +01:00
|
|
|
_controller.selection = TextSelection.fromPosition(
|
|
|
|
TextPosition(offset: _controller.text.length),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
2024-01-08 19:34:48 +01:00
|
|
|
if (widget.controller == null) {
|
|
|
|
_controller.dispose();
|
|
|
|
}
|
2023-12-17 23:00:31 +01:00
|
|
|
_focusNode.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
}
|