Why is my flutter TextField initialState not working

156 views Asked by At

Here is the code on how i call my custom Widget called BodyInput

BodyInput(
              label: 'Name',
              readOnly: readOnly,
              initialValue: name,
              placeholder: name,
              handleChange: (value) {
                print(value);
              },
            ),

both placeholder and initialState has the same value but for some reason instead of displaying the initialState my TextField is displaying the placeholder. results

and here is the BodyInput itself *for some reason it wont let me copy paste the code so here is an image instead i'm really sorry for the trouble, for some reason when i paste the code only first line is copied as code the rest is normal text

1

There are 1 answers

0
Its YaaBoy On

Well turnsout my widget.initialState on initState value was null :/, so i had to set the _controller value in the Widget and my code become like this

Widget build(BuildContext context) {
//Had to move this here instead of in the initState method.
_controller = new TextEditingController(text: widget.initialValue);
return Column(
  crossAxisAlignment: CrossAxisAlignment.start,
  children: [
    Text(widget.label),
    TextField(
      onChanged: (value) {
        widget.handleChange(value);
      },
      controller: _controller,
      readOnly: widget.readOnly,
      decoration: InputDecoration(
          hintText: widget.placeholder, contentPadding: EdgeInsets.all(2)),
    ),
  ],
);

}

please let me know if there is a better answer