-1

I have 20 input fields in my app. What is the best way to define TextFormField widgets?

For example:

Column(
          children: [
            _buildCariUnvanTextField(unvanController),
            _buildCariUnvanTextField(unvanController),
            _buildCariUnvanTextField(unvanController),
            _buildCariUnvanTextField(unvanController),
            _buildCariUnvanTextField(unvanController),
            _buildCariUnvanTextField(unvanController),
            _buildCariUnvanTextField(unvanController),
            _buildCariUnvanTextField(unvanController),
            _buildCariUnvanTextField(unvanController),
            _buildCariUnvanTextField(unvanController),
          ],
        ),

Should I have 20 separate methods? Is it the correct way to define? Or what should I do? Can anyone explain?

janw
  • 8,758
  • 11
  • 40
  • 62
Alican Kesen
  • 11
  • 1
  • 3

2 Answers2

0

You shouldn't have 20 different methods. You should use ListView.builder code like this:

ListView.builder(
  itemCount: 20,
  itemBuilder: (context, index) {
    return _buildCariUnvanTextField(unvanController);
  },
),

If the widgets are different, I think the only way is to have 20 different methods.

My Car
  • 4,198
  • 5
  • 17
  • 50
  • It's totally my fault that you don't understand the question. Each TextFormField is representing another field. I've duplicated the same widget just as an example. – Alican Kesen Oct 28 '22 at 10:15
  • Hi @AlicanKesen, if the widgets are not the same, I think the only way is to have 20 different methods – My Car Oct 28 '22 at 11:12
  • 1
    I think too. I just wondered is there another way. Thank you – Alican Kesen Oct 28 '22 at 11:17
-1

Avoid using helper methods altogether and instead define and use own defined classes/widgets.

For reference:

https://youtu.be/IOyq-eTRhvo

And another SO question where it has been answered:

What is the difference between functions and classes to create reusable widgets?

Robert Sandberg
  • 6,832
  • 2
  • 12
  • 30