Home > Back-end >  Automatically Add Two Numbers In Textfield and show the result in a third Textfield Flutter
Automatically Add Two Numbers In Textfield and show the result in a third Textfield Flutter

Time:09-23

I have two textfields that accept number inputs. I want to calculate the sum of the two textfields while the user input the numbers in the textfields and in realtime show the results in a third textfield. This is what I have tried so far.

void _calculation() {
    setState((){
      _total = int.parse(_quantityController.text) * int.parse(feedPrice.text);
    },
    );
    print(_total);
  }

And show the result in the third textfield

TextField(
                readOnly: true,
                textAlign: TextAlign.center,
                decoration: InputDecoration(
                    hintText: _total.toString(),
                ),
              ),

I pass the total as a string to Textfield hint field. What am I missing or what am I doing wrong?

CodePudding user response:

you are doing wrong at setting hintText .Because hint Text that suggests what sort of input the field accepts.

you should set text to text field instead of hinttext like this

text:_total.toString()

CodePudding user response:

Demo Widget


class ApplicantsX extends StatefulWidget {
  const ApplicantsX({Key? key}) : super(key: key);

  @override
  State<ApplicantsX> createState() => _ApplicantsXState();
}

class _ApplicantsXState extends State<ApplicantsX> {
  double a = 0;
  double b = 0;

  final TextEditingController controller = TextEditingController();

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        TextField(onChanged: (value) {
          final x = double.tryParse(value);

          setState(() {
            a = x ?? 0; // handle null and String

            controller.text = (a   b).toStringAsFixed(2);
          });
        }),
        TextField(onChanged: (value) {
          final x = double.tryParse(value);

          setState(() {
            b = x ?? 0;

            controller.text = (a   b).toStringAsFixed(2);
          });
        }),
        TextField(
          controller: controller,
          readOnly: true,
        )
      ],
    );
  }
}

CodePudding user response:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class _YourPageState extends State<YourPage> {
  int _age1 = 0, _age2 = 0, _totalAge = 0;
  final firstNumber = TextEditingController();
  final secondNumber = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Padding(
        padding: const EdgeInsets.all(32.0),
        child: Column(
          children: <Widget>[
            TextField(
              controller: firstNumber,
              textInputAction: TextInputAction.next,
              keyboardType: TextInputType.number,
              onSubmitted: (String value) {},
            ),
            TextField(
              controller: secondNumber,
              textInputAction: TextInputAction.next,
              keyboardType: TextInputType.number,
              onSubmitted: (String value) {},
            ),
            Text(
              'Sum is: ${firstNumber.text   secondNumber.text}',
              textAlign: TextAlign.center,
              style: const TextStyle(fontWeight: FontWeight.bold),
            )
          ],
        ),
      ),
    );
  }
}
  • Related