0

I want to pass quantity from listview using a textfield in listtile.

        padding: EdgeInsets.all(8.0),
        itemCount: allProducts.length,
        itemBuilder: (context, index) {
          return Consumer<CartModel>(builder: (context, cart, child) {
            return Card(
              color: Colors.white,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Padding(
                    padding: const EdgeInsets.only(left: 20),
                    child: Text(
                      allProducts[index].title,
                      style: TextStyle(fontWeight: FontWeight.bold),
                    ),
                  ),
                  Container(
                    width: 30,
                    child: TextField(
                      controller: TextEditingController(),
                      decoration: InputDecoration(
                        hintText: 'Quantity'
                      ),
                    ),
                  ),
                  // Text("\$" + allProducts[index].price.toString()),
                  RaisedButton(
                    color: Colors.blue[200],
                    child: Text('Add'),
                    onPressed: () {
                      cart.addProduct(allProducts[index]);
                    },
                  ),
                ],
              ),
            );
          });
        },
      ),

But when I define controller for textfield, it reflects on all of the fields of the listview. Is there any way I can enter custom quantity on individual textfields of the list and pass them to cart?

Zero Live
  • 1,653
  • 5
  • 22
  • 44

4 Answers4

0

try Moving whole Row code into another widget and take a product as input for that new widget.

Now when creating a row pass something unique in value from your products as a key.

Harshvardhan Joshi
  • 2,855
  • 2
  • 18
  • 31
0

Yes, one of the cleanest ways to solve this is to extract the tile into a separate StatefulWidget. This widget can then have its own controller as a state, and the button can access its text seamlessly. You would also gain in readability.

MickaelHrndz
  • 3,604
  • 1
  • 13
  • 23
0

You need the define your TextEditingController outside of your widget into a variable and pass that variable to the controller property of your TextField:

TextEditingController _textEditingController = TextEditingController();

TextField(
  controller: _textEditingController,
  decoration: InputDecoration(
    hintText: 'Quantity'
  ),
),

Once you have that, you can access and manipulate the contents of that specific TextField.

halfer
  • 19,824
  • 17
  • 99
  • 186
J. S.
  • 8,905
  • 2
  • 34
  • 44
  • Please do not ask for votes here. It is OK to remind folks about how the voting system works, but asking someone to upvote/accept your material in particular is discouraged. If you work is useful, it will get upvoted quite organically. – halfer Jan 02 '20 at 20:43
  • You're right and I understand that. Unfortunately the amount of people who get correct answers, comment about it but never mark answers as correct leads to some frustration. – J. S. Jan 02 '20 at 21:08
  • Yes, I agree with that. One approach that is OK is to add a comment reminder underneath the question, asking if they have seen all the answers, or whether they are still stuck. Personally I tend to avoid answering questions from new accounts, or where there was significant requirement for clarification or question repair - in those cases, the question may be a request for free work, and the question is asked with no desire to be part of the (voting) community. – halfer Jan 02 '20 at 21:24
  • Yes, exactly. I have made a lot of comments advising new users that they will have a better chance of getting quality answers if they follow the "how to ask" guidelines. But it does often get ignored or people get annoyed and tell you off, as happened to me days ago. A moderator removed the nasty comment. – J. S. Jan 02 '20 at 21:34
  • 1
    Wise words. Cheers! – J. S. Jan 02 '20 at 21:57
0

Use key property to distinguish textfields. Here is a example: Using Keys in Flutter

KumarSunil17
  • 224
  • 2
  • 6