ModernMemoires/lib/views/write_page/widget/why_widget.dart

61 lines
1.7 KiB
Dart
Raw Normal View History

2023-12-17 23:00:31 +01:00
import 'package:flutter/material.dart';
class WhyWidget extends StatefulWidget {
@override
_WhyWidgetState createState() => _WhyWidgetState();
}
class _WhyWidgetState extends State<WhyWidget> {
final TextEditingController _controller =
TextEditingController(text: "why? ");
final FocusNode _focusNode = FocusNode();
@override
void initState() {
super.initState();
_focusNode.addListener(() {
if (!_focusNode.hasFocus) {
setState(() {
// Remove cursor when focus is lost
_controller.text = _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: _controller,
focusNode: _focusNode,
keyboardType: TextInputType.multiline,
maxLines: null, // Allow for unlimited lines
cursorColor: Colors.transparent, // Hide the default cursor
style: TextStyle(color: Colors.black, fontSize: 18),
decoration: InputDecoration(
border: InputBorder.none, // Make the TextField borderless
),
onChanged: (value) {
// Ensure "why? " is always at the start
if (!value.startsWith('why? ')) {
_controller.text = 'why? ' + value;
_controller.selection = TextSelection.fromPosition(
TextPosition(offset: _controller.text.length));
}
},
);
}
@override
void dispose() {
_controller.dispose();
_focusNode.dispose();
super.dispose();
}
}