muzzle-velocity/lib/codefield.dart
randogoth 14d6b3c44d always place cursor at end of file when switching to edit mode
Removes the unreliable viewport-centre cursor heuristic. Switching to
edit mode now always positions the cursor at text.length and scrolls to
the bottom, whether arriving from a regular tap or a long-press. The
long-press scroll-to-end on initial load (view mode) is unchanged.

Cleans up _cursorAtEnd flag, _editorKey / GlobalKey, _getOffsetAtViewportCenter,
and the public WrappedCodeFieldState / findRenderEditable that were only
needed for the removed feature.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-19 19:01:21 +03:00

74 lines
2 KiB
Dart
Executable file

import 'package:flutter/material.dart';
import 'package:flutter_code_editor/flutter_code_editor.dart';
class WrappedCodeField extends StatefulWidget {
final CodeController controller;
final bool expands;
final bool wrap;
final TextStyle? textStyle;
final Color? cursorColor;
final EdgeInsets padding;
final bool readOnly;
final FocusNode? focusNode;
const WrappedCodeField({
super.key,
required this.controller,
this.expands = false,
this.wrap = true, // ✅ Enable wrapping by default
this.textStyle,
this.cursorColor,
this.padding = EdgeInsets.zero,
this.readOnly = false,
this.focusNode,
});
@override
State<WrappedCodeField> createState() => _WrappedCodeFieldState();
}
class _WrappedCodeFieldState extends State<WrappedCodeField> {
late FocusNode _focusNode;
@override
void initState() {
super.initState();
_focusNode = widget.focusNode ?? FocusNode();
_focusNode.attach(context, onKeyEvent: _onKeyEvent);
}
KeyEventResult _onKeyEvent(FocusNode node, KeyEvent event) {
return widget.controller.onKey(event);
}
@override
void dispose() {
if (widget.focusNode == null) {
_focusNode.dispose();
}
super.dispose();
}
@override
Widget build(BuildContext context) {
return Container(
padding: widget.padding,
child: TextField(
focusNode: _focusNode,
controller: widget.controller,
expands: widget.expands,
minLines: widget.expands ? null : 1,
maxLines: widget.expands ? null : null, // Allows text to grow dynamically
scrollPhysics: widget.wrap ? const NeverScrollableScrollPhysics() : null,
style: widget.textStyle ?? const TextStyle(fontSize: 16),
cursorColor: widget.cursorColor ?? Colors.white,
decoration: const InputDecoration(
isCollapsed: true,
border: InputBorder.none,
contentPadding: EdgeInsets.symmetric(vertical: 16),
),
readOnly: widget.readOnly,
),
);
}
}