Home > database >  Table calculation in flutter with one button
Table calculation in flutter with one button

Time:12-28

enter image description here

Here are two rows of products with product name , Load(qty) ,Balance(qty) , amount columns and should be calculate by Amount = (load - balance)*2 and display the amount on that row's amount field it should be work for each rows with only one button called calculated without duplicate vars like i did.

    class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final loadCntrlr1 = TextEditingController();
  final loadCntrlr2 = TextEditingController();

  final balCntrlr1 = TextEditingController();
  final balCntrlr2 = TextEditingController();
  final amount1 = 0;
  final amount2 = 0;

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            FittedBox(
              child: DataTable(columns: const [
                DataColumn(
                  label: Text('Products'),
                ),
                DataColumn(
                  label: Text('Load(Qty)'),
                ),
                DataColumn(
                  label: Text('Bal(Qty)'),
                ),
                DataColumn(
                  label: Text('Amount'),
                ),
              ], rows: [
                DataRow(cells: [
                  const DataCell(
                    Text('Furniture'),
                  ),
                  DataCell(
                    TextFormField(
                      keyboardType: TextInputType.number,
                      controller: loadCntrlr1,
                    ),
                  ),
                  DataCell(
                    TextFormField(
                      keyboardType: TextInputType.number,
                      controller: balCntrlr1,
                    ),
                  ),
                  const DataCell(
                    Text('0'),
                  ),
                ]),
                DataRow(cells: [
                  const DataCell(
                    Text('Dhall'),
                  ),
                  DataCell(
                    TextFormField(
                      keyboardType: TextInputType.number,
                      controller: loadCntrlr2,
                    ),
                  ),
                  DataCell(
                    TextFormField(
                      keyboardType: TextInputType.number,
                      controller: balCntrlr2,
                    ),
                  ),
                  const DataCell(
                    Text('0'),
                  ),
                ]),
              ]),
            ),
            ElevatedButton(onPressed: () {}, child: Text('calculate'))
          ],
        ),
      ),
    );
  }
}

CodePudding user response:

Did you mean like this?

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final products = <Product>[
    Product(name: "Furniture"),
    Product(name: "Dhall"),
  ];

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            FittedBox(
              child: DataTable(
                columns: const [
                  DataColumn(
                    label: Text('Products'),
                  ),
                  DataColumn(
                    label: Text('Load(Qty)'),
                  ),
                  DataColumn(
                    label: Text('Bal(Qty)'),
                  ),
                  DataColumn(
                    label: Text('Amount'),
                  ),
                ],
                rows: products
                    .map(
                      (product) => DataRow(
                        cells: [
                          DataCell(Text(product.name)),
                          DataCell(
                            TextFormField(
                              keyboardType: TextInputType.number,
                              onChanged: (value) {
                                product.load = int.tryParse(value);
                              },
                            ),
                          ),
                          DataCell(
                            TextFormField(
                              keyboardType: TextInputType.number,
                              onChanged: (value) {
                                product.balance = int.tryParse(value);
                              },
                            ),
                          ),
                          DataCell(Text('${product.amount}')),
                        ],
                      ),
                    )
                    .toList(),
              ),
            ),
            ElevatedButton(
                onPressed: () {
                  products.forEach((element) {
                    if (element.load != null && element.balance != null) {
                      element.amount = (element.load! - element.balance!) * 2;
                    }
                  });
                  setState(() {});
                },
                child: Text('calculate'))
          ],
        ),
      ),
    );
  }
}

class Product {
  final String name;
  int? load;
  int? balance;
  int amount;

  Product({
    required this.name,
    this.load,
    this.balance,
    this.amount = 0,
  });
}
  • Related