ModernMemoires/lib/utils/widgets/why_widget.dart

68 lines
1.9 KiB
Dart
Raw Normal View History

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