TextFormField cursor did not move when changes occur

Viewed 86

I have a screen that shows a textformfield, I want to retrieve the value that the user has inputted. But when I made a change the cursor didn't move, so it stays at position 0. How do I solve this problem?

class ScreenFormArtikel extends StatefulWidget {
  final String mode;

  ScreenFormArtikel(this.mode);

  @override
  _ScreenFormArtikelState createState() => _ScreenFormArtikelState();
}

class _ScreenFormArtikelState extends State<ScreenFormArtikel> {
  var _key = GlobalKey<FormState>();
  var _titleController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Article'),
      ),
      body: Container(
        child: Form(
          key: _key,
          child: Column(
            children: [
              Consumer<ProviderArtikel>(
                builder: (context, artikel, child) {
                  return TextFormField(
                    decoration: InputDecoration(
                        labelText: 'Title',
                        floatingLabelBehavior: FloatingLabelBehavior.always),
                    controller: _titleController,
                    validator: (value) {
                      if (value.isEmpty) {
                        return "Please fill this field";
                      }
                      return null;
                    },
                    onChanged: (value) {
                      _titleController.text = value;
                    },
                  );
                },
              ),
              RaisedButton(onPressed: () {}),
            ],
          ),
        ),
      ),
    );
  }
}

I have attached the sample video below click here

1 Answers

Commenting the onChanged will do the work.

TextFormField(
            decoration: InputDecoration(
                labelText: 'Title',
                floatingLabelBehavior: FloatingLabelBehavior.always),
            controller: _titleController,
            validator: (value) {
              if (value.isEmpty) {
                return "Please fill this field";
              }
              return null;
            },
            // onChanged: (value) {
            //   _titleController.text = value;
            // },
          ),

Actually the _titleController.text is setting the text onChange, that's why cursor is moving to the first place.

And you don't have to explicitly do the onTextChange thing.

Related