71 lines
2.0 KiB
Dart
71 lines
2.0 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
class WhyWidget extends StatefulWidget {
|
|
final TextEditingController controller;
|
|
final bool prependWhy;
|
|
|
|
const WhyWidget({
|
|
Key? key,
|
|
required this.controller,
|
|
this.prependWhy = true,
|
|
}) : super(key: key);
|
|
|
|
@override
|
|
State<WhyWidget> createState() => _WhyWidget();
|
|
}
|
|
|
|
class _WhyWidget 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(
|
|
cursorOpacityAnimates: true,
|
|
scrollPhysics: const BouncingScrollPhysics(decelerationRate: ScrollDecelerationRate.normal),
|
|
|
|
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();
|
|
}
|
|
}
|