Use dynamic TextField in ListViewBuilder in Flutter. You have several options depending how you architecture your application or where you have your central state.
I propose you here a solution that updates a local map variable. Alternatively, you could send an event/stream to the place your store is located.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | import 'package:flutter/material.dart'; void main() => runApp(new MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: MyHomePage(), ); } } class MyHomePage extends StatefulWidget { @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { List<Item> itemList = [ Item("ID1", "First product"), Item("ID2", "Second product"), ]; Map<String, int> quantities = {}; void takeNumber(String text, String itemId) { try { int number = int.parse(text); quantities[itemId] = number; print(quantities); } on FormatException {} } Widget singleItemList(int index) { Item item = itemList[index]; return Container( decoration: BoxDecoration( color: Colors.white, ), child: Row( children: [ Expanded(flex: 1, child: Text("${index + 1}")), Expanded( flex: 3, child: TextField( keyboardType: TextInputType.number, onChanged: (text) { takeNumber(text, item.id); }, decoration: InputDecoration( labelText: "Qty", ), ), ), ], ), ); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text("Demo")), body: Center( child: ListView.builder( shrinkWrap: true, itemCount: itemList.length, itemBuilder: (context, index) { if (itemList.isEmpty) { return CircularProgressIndicator(); } else { return singleItemList(index); } }), ), ); } } class Item { final String id; final String name; Item(this.id, this.name); } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.