Home > Mobile >  Create diynamic textfromfild labelText
Create diynamic textfromfild labelText

Time:03-08

I'm trying to create a new TextInput widget for customizing TextFormField but i can't customize labelText. I need to send in constructor labelText for my TextFormField and show this String.

class TextInput extends StatelessWidget {


 final TextEditingController textControler;
 final String textLabelHint;
 const TextInput({Key? key, required this.textControler,required this.textLabelHint}) : super(key: key);

@override
Widget build(BuildContext context) {
  return Padding(
    padding: const EdgeInsets.only(top: 10.0),
    child: TextFormField(
    controller: textControler,
    decoration: const InputDecoration(
      border: OutlineInputBorder(),
      labelText: textLabelHint, 
    ),
  ),
);
  }
}

But I have problem:

labelText: textLabelHint, //Invalid constant value. 

CodePudding user response:

This error is there because textLabelHint is a final class property (not const) that could change based on the constructor value. However, where you try to pass this value is InputDecoration which you have marked as const. Thus, the error indicates that.

To resolve the issue, remove the const keyword before the InputDecoration:

class TextInput extends StatelessWidget {
 final TextEditingController textControler;
 final String textLabelHint;
 const TextInput({Key? key, required this.textControler,required this.textLabelHint}) : super(key: key);

@override
Widget build(BuildContext context) {
  return Padding(
    padding: const EdgeInsets.only(top: 10.0),
    child: TextFormField(
    controller: textControler,
    decoration: InputDecoration( // <-- Notice the removed const
      border: OutlineInputBorder(),
      labelText: textLabelHint, 
    ),
  ),
);

CodePudding user response:

You ned to remove const from decoration: const InputDecoration(...), since textLabelHint isn't a const value:

class TextInput extends StatelessWidget {
  final TextEditingController textControler;
  final String textLabelHint;
  const TextInput(
      {Key? key, required this.textControler, required this.textLabelHint})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(top: 10.0),
      child: TextFormField(
        controller: textControler,
        decoration: InputDecoration(
          border: OutlineInputBorder(),
          labelText: textLabelHint,
        ),
      ),
    );
  }
}

CodePudding user response:

Try this:

class TextInput extends StatelessWidget {


 final TextEditingController textControler;
 final String textLabelHint;
 const TextInput({Key? key, required this.textControler,required this.textLabelHint}) : super(key: key);

@override
Widget build(BuildContext context) {
  return Padding(
    padding: const EdgeInsets.only(top: 10.0),
    child: TextFormField(
    controller: textControler,
    decoration: InputDecoration(
      border: OutlineInputBorder(),
      labelText: textLabelHint, 
    ),
  ),
);
  }
}
  • Related