0

I have above 10 TextFormFields so i created widget that i could not type this same code to every of them

class TextFieldWidget extends StatelessWidget {
  const TextFieldWidget(
      {super.key, required this.countryshortcut, required this.controllername});

  final String countryshortcut;
  final String controllername;

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: 60.0,
      height: 60,
      child: TextFormField(
        decoration: InputDecoration(
          hintText: countryshortcut,
          counterText: "",
        ),
        maxLength: 2,
        controller: controllername,
        keyboardType: TextInputType.number,
        inputFormatters: [FilteringTextInputFormatter.digitsOnly],
      ),
    );
  }
}

as you see i need controller and here i get an error

The argument type 'String' can't be assigned to the parameter type 'TextEditingController?'.

I tried solutions from other topics, for example trying to change type of variable from String or changing 'controllername' to 'controllername.text' (the getter text isn't defined) but it didn't work.

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
Adam
  • 33
  • 5

4 Answers4

2

While you like to use controller pass TextEditingController

class TextFieldWidget extends StatelessWidget {
  const TextFieldWidget({
    super.key,
    required this.countryshortcut,
    required this.controller,
  });

  final String countryshortcut;
  final TextEditingController controller;

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: 60.0,
      height: 60,
      child: TextFormField(
        decoration: InputDecoration(
          hintText: countryshortcut,
          counterText: "",
        ),
        maxLength: 2,
        controller: controller,
        keyboardType: TextInputType.number,
        inputFormatters: [FilteringTextInputFormatter.digitsOnly],
      ),
    );
  }
}

If you like to pass initial value TextEditingController.fromValue(

 final TextEditingController controller =
        TextEditingController.fromValue(TextEditingValue(text: "initValue"));
 TextFieldWidget(
  controller: controller,
  countryshortcut: "",
),
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
  • Thank you very much, as i see it works very good :) By the way i found your answer for another question few days ago - https://stackoverflow.com/a/73425333/19789103 - i can't write you there because my points number is too low. I tried to do this same with also third column of numbers but it didn't work for me well. Is it possible to do it with not only two, but three different columns? – Adam Aug 24 '22 at 14:50
  • Sorry I am not getting, the link you've attached about sorting number, I think you can create a new question providing sample snippet – Md. Yeasin Sheikh Aug 24 '22 at 14:54
  • Yes the problem is i created new question with my sorting problem but it was closed because it was a duplicate of a topic from link above. – Adam Aug 24 '22 at 15:02
  • can you share the duplicate question link – Md. Yeasin Sheikh Aug 24 '22 at 17:11
  • It is here. https://stackoverflow.com/questions/73438970/map-with-list-as-values-how-to-sort-it-by-specific-list-item Then i rearranged this map a little that it contains not lists, but maps like in that example above – Adam Aug 25 '22 at 01:04
  • map cant be sorted, you can try `SplayTreeMap` – Md. Yeasin Sheikh Aug 25 '22 at 04:21
  • Thank you I will try check it, also is it possible to use 'toList' method if i will have map like this? https://stackoverflow.com/questions/73425267/flutter-sort-map-by-two-values/73425333#73425333 but with also 3 keys and 3 values per map? Like your solution in this example – Adam Aug 25 '22 at 13:19
  • I think creating a model class then create a list sorting by value will be a good option for your case, You can also check [sorting map](https://stackoverflow.com/q/18244545/10157127) – Md. Yeasin Sheikh Aug 25 '22 at 13:33
0

controller is of type TextEditController().
_textController.text can be used as a getter.

TextEditingController _textController = TextEditingController();

    return Scaffold(
      body: SafeArea(
        child: SizedBox(
          width: 60.0,
          height: 60.0,
          child: TextFormField(
            decoration: InputDecoration(
              hintText: "hint",
              counterText: "",
            ),
            maxLength: 2,
            controller: _textController,
            keyboardType: TextInputType.number,
            inputFormatters: [FilteringTextInputFormatter.digitsOnly],
          ),
        ),
      ),
    );
Jungwon
  • 1,038
  • 7
  • 20
0
class TextFieldWidget extends StatelessWidget {
  TextFieldWidget(
      {super.key, required this.countryshortcut, required this.controllername}) {
**controller.text = controllerame;**
};

  final String countryshortcut;
  final String controllername;

  final **controller** = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: 60.0,
      height: 60,
      child: TextFormField(
        decoration: InputDecoration(
          hintText: countryshortcut,
          counterText: "",
        ),
        maxLength: 2,
        controller: **controller**,
        keyboardType: TextInputType.number,
        inputFormatters: [FilteringTextInputFormatter.digitsOnly],
      ),
    );
  }
}
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
sahilatahar
  • 568
  • 2
  • 13
0
final String countryshortcut;
final String controllername; // you don't need it
final TextEditingController controller; // Use this
//Run as controller.text when using.
//For example Text(controller.text)
Alakba
  • 178
  • 10
  • 1
    This answer was reviewed in the [Low Quality Queue](https://stackoverflow.com/help/review-low-quality). Here are some guidelines for [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Code only answers are **not considered good answers**, and are likely to be downvoted and/or deleted because they are **less useful** to a community of learners. It's only obvious to you. Explain what it does, and how it's different / **better** than existing answers. [From Review](https://stackoverflow.com/review/low-quality-posts/32557923) – Trenton McKinney Aug 26 '22 at 23:33