Flutter textformfield cursor moving to front of the text

Viewed 12

I am using a textformfield in my application. The cursor keeps moving to the left most side of the text. To solve this issue I added a listener:

  final TextEditingController _controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    _controller.text = widget.localLayoutItem.cx.toInt().toString();
    _controller.addListener(() {
      _controller.value = _controller.value.copyWith(
        text: _controller.text,
        selection: TextSelection.fromPosition(TextPosition(
          offset: _controller.text.length,
        )),
        composing: TextRange.empty,
      );
    });
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        TextFormField(
            controller: _controller,
            decoration: const InputDecoration(
              border: InputBorder.none,
              contentPadding: EdgeInsets.only(left: 10),
              iconColor: Colors.grey,
            ),
            onChanged: (value) {
              FocusedItemModel.of(context).redraw();
              widget.localLayoutItem.cx = double.parse(value);
           },
        ),
      ],
    );
  }

The textfield value is directly connected to the widgets x-axis value so if the widget moves I need to update the textfield value as well and vice-versa.

Problem: After adding the listener the cursor is staying on the right most but I lost functionality like using the arrow key to a desired character or deleting all characters.

how to solve this issue?

0 Answers
Related