0

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. 
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Stefan
  • 69
  • 6

3 Answers3

2

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,
        ),
      ),
    );
  }
}
MendelG
  • 14,885
  • 4
  • 25
  • 52
  • yes yes is work .... I would like to ask when should I use const? When you make an object that does not change? – Stefan Mar 07 '22 at 22:10
  • @Stefan `const` is for _compile_ time, `final` is for run time. See [this](https://stackoverflow.com/questions/50431055/what-is-the-difference-between-the-const-and-final-keywords-in-dart) answer – MendelG Mar 07 '22 at 22:11
1

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, 
    ),
  ),
);
mkobuolys
  • 4,499
  • 1
  • 11
  • 27
  • yes yes is work .... I would like to ask when should I use const? When you make an object that does not change? – Stefan Mar 07 '22 at 22:09
  • Check this in the [Dart language tour](https://dart.dev/guides/language/language-tour#final-and-const). – mkobuolys Mar 07 '22 at 22:12
0

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: const OutlineInputBorder(),
          labelText: textLabelHint,
        ),
      ),
    );
  }
}
My Car
  • 4,198
  • 5
  • 17
  • 50