68 lines
1.9 KiB
Dart
68 lines
1.9 KiB
Dart
|
import 'package:flutter/material.dart';
|
||
|
|
||
|
class WhyWidget extends StatefulWidget {
|
||
|
final TextEditingController controller;
|
||
|
final bool prependWhy;
|
||
|
|
||
|
WhyWidget({
|
||
|
Key? key,
|
||
|
required this.controller,
|
||
|
this.prependWhy = true,
|
||
|
}) : super(key: key);
|
||
|
|
||
|
@override
|
||
|
_WhyWidgetState createState() => _WhyWidgetState();
|
||
|
}
|
||
|
|
||
|
class _WhyWidgetState extends State<WhyWidget> {
|
||
|
//final TextEditingController _controller = TextEditingController(text: "warum? ");
|
||
|
final FocusNode _focusNode = FocusNode();
|
||
|
|
||
|
@override
|
||
|
void initState() {
|
||
|
super.initState();
|
||
|
_focusNode.addListener(() {
|
||
|
if (!_focusNode.hasFocus) {
|
||
|
setState(() {
|
||
|
// Remove cursor when focus is lost
|
||
|
widget.controller.text = widget.controller.text.replaceAll('|', '');
|
||
|
});
|
||
|
}
|
||
|
});
|
||
|
|
||
|
// Automatically focus the TextField and open the keyboard when the widget builds
|
||
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||
|
_focusNode.requestFocus();
|
||
|
});
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
return TextField(
|
||
|
controller: widget.controller,
|
||
|
focusNode: _focusNode,
|
||
|
keyboardType: TextInputType.multiline,
|
||
|
maxLines: null, // Allow for unlimited lines
|
||
|
cursorColor: Colors.transparent, // Hide the default cursor
|
||
|
style: const TextStyle(color: Colors.black, fontSize: 18),
|
||
|
decoration: const InputDecoration(
|
||
|
border: InputBorder.none, // Make the TextField borderless
|
||
|
),
|
||
|
onChanged: (value) {
|
||
|
// Ensure "why? " is always at the start
|
||
|
if (widget.prependWhy && !value.startsWith('warum? ')) {
|
||
|
widget.controller.text = 'warum? $value';
|
||
|
widget.controller.selection = TextSelection.fromPosition(TextPosition(offset: widget.controller.text.length));
|
||
|
}
|
||
|
},
|
||
|
);
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
void dispose() {
|
||
|
widget.controller.dispose();
|
||
|
_focusNode.dispose();
|
||
|
super.dispose();
|
||
|
}
|
||
|
}
|