skip to Main Content

i am new to flutter, in learning phase.
in process of creating a simple flutter app which calculate the value of text/number entered in textformfield to single digit number by assigning A=1 to Z=26

Ex: if input is Hello then output should be 7
if input is hello 2 then output should be 9

i tried below code, but

  1. it is not calculating to single digit,
  2. it is not calculating for numbers,
  3. when text box is empty and if i click calculate button, it is displaying old value which is calculated
class _ValueCalculatorState extends State<ValueCalculator> {
  final TextEditingController _inputText = TextEditingController();
  final TextEditingController _resultsText = TextEditingController();

  String results = "0";
  String value = "";
  String inputText = "";

  void clearTextField() {
    _inputText.clear();
    _resultsText.clear();
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => FocusManager.instance.primaryFocus?.unfocus(),
      child: Scaffold(
        body: SingleChildScrollView(
          child: SafeArea(
            child: Padding(
              padding: const EdgeInsets.all(10.0),
              child: Column(
                children: [
                  TextFormField(
                    controller: _inputText,
                    minLines: 5,
                    maxLines: 9,
                    keyboardType: TextInputType.multiline,
                    onChanged: (name) {
                      inputText = name;
                      //value1 = name;
                    },
                    decoration: InputDecoration(
                      labelText: 'Enter Text ',
                      border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(10.0),
                      ),
                    ),
                  ),
                  const Padding(
                    padding: EdgeInsets.symmetric(
                      vertical: 20.0,
                      horizontal: 20.0,
                    ),
                  ),
                  Text(
                    results,
                    style: TextStyle(fontSize: 50),
                  ),
                  const Padding(
                    padding: EdgeInsets.symmetric(
                      vertical: 20.0,
                      horizontal: 20.0,
                    ),
                  ),
                  ElevatedButton(
                    onPressed: () {
// calculation logic
                      value = inputText.replaceAll(RegExp('[^A-Za-z0-9]'), '');
                      int sum = 0;
                      if (value.trim().isNotEmpty) {
                        for (int i = 0; i < value.length; i++)
                          (value[i].toUpperCase() == value[i])
                              ? sum += (value.codeUnitAt(i) - 64)
                              : sum += (value.codeUnitAt(i) - 96);

                        setState(() {
                          results = sum.toString();
                        });
                      }
                      print(sum);
                    },
                    style: ElevatedButton.styleFrom(
                      padding: const EdgeInsets.symmetric(
                        vertical: 25,
                        horizontal: 60,
                      ),
                    ),
                    child: const Text("Calculate"),
                  ),
                  const Padding(
                    padding: EdgeInsets.symmetric(
                      vertical: 20.0,
                      horizontal: 20.0,
                    ),
                  ),
                  ElevatedButton(
                    onPressed: () {
                      clearTextField();
                      results = "0";
                    },
                    child: Text("Reset"),
                    style: ElevatedButton.styleFrom(
                      padding: const EdgeInsets.symmetric(
                        vertical: 15,
                        horizontal: 30,
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );

2

Answers


  1. I hope you will find this helpful

    class ValueCalculator extends StatefulWidget {
      const ValueCalculator({super.key});
    
      @override
      State<StatefulWidget> createState() => _ValueCalculatorState();
    }
    
    class _ValueCalculatorState extends State<ValueCalculator> {
      final TextEditingController _inputText = TextEditingController();
    
      final Map<int, int> map = {};
    
      String results = "0";
      String inputText = "";
    
      _ValueCalculatorState() {
        for (var i = 1, j = 'a'.codeUnits.first;
            j <= 'z'.codeUnits.first;
            i++, j++) {
          map[j] = i;
        }
    
        for (var i = 0, j = '0'.codeUnits.first;
            j <= '9'.codeUnits.first;
            i++, j++) {
          map[j] = i;
        }
      }
    
      @override
      void initState() {
        super.initState();
        _inputText.addListener(_onInputTextChanged);
      }
    
      void _onInputTextChanged() => inputText = _inputText.text;
    
      void _clearTextField() {
        _inputText.clear();
        setState(() {});
      }
    
      int _calculate(String input) {
        int result = 0;
    
        for (var i = 0; i < input.codeUnits.length; i++) {
          final key = input.codeUnitAt(i);
          final value = map[key];
          if (value != null) {
            result += value;
          }
        }
    
        while (result > 9) {
          int sumOfDigits = 0;
          for (int i = result; i > 0; i ~/= 10) {
            sumOfDigits += i.remainder(10);
          }
    
          result = sumOfDigits;
        }
    
        return result;
      }
    
      @override
      Widget build(BuildContext context) {
        return GestureDetector(
          onTap: () => FocusManager.instance.primaryFocus?.unfocus(),
          child: Scaffold(
            body: SingleChildScrollView(
              child: SafeArea(
                child: Padding(
                  padding: const EdgeInsets.all(10.0),
                  child: Column(
                    children: [
                      TextFormField(
                        controller: _inputText,
                        minLines: 5,
                        maxLines: 9,
                        keyboardType: TextInputType.multiline,
                        decoration: InputDecoration(
                          labelText: 'Enter Text ',
                          border: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(10.0),
                          ),
                        ),
                      ),
                      const Padding(
                        padding: EdgeInsets.symmetric(
                          vertical: 20.0,
                          horizontal: 20.0,
                        ),
                      ),
                      Text(
                        results,
                        style: const TextStyle(fontSize: 50),
                      ),
                      const Padding(
                        padding: EdgeInsets.symmetric(
                          vertical: 20.0,
                          horizontal: 20.0,
                        ),
                      ),
                      ElevatedButton(
                        onPressed: () {
                          setState(() {
                            results =
                                _calculate(inputText.toLowerCase()).toString();
                          });
                        },
                        style: ElevatedButton.styleFrom(
                          padding: const EdgeInsets.symmetric(
                            vertical: 25,
                            horizontal: 60,
                          ),
                        ),
                        child: const Text("Calculate"),
                      ),
                      const Padding(
                        padding: EdgeInsets.symmetric(
                          vertical: 20.0,
                          horizontal: 20.0,
                        ),
                      ),
                      ElevatedButton(
                        onPressed: () {
                          results = "0";
                          _clearTextField();
                        },
                        style: ElevatedButton.styleFrom(
                          padding: const EdgeInsets.symmetric(
                            vertical: 15,
                            horizontal: 30,
                          ),
                        ),
                        child: const Text("Reset"),
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ),
        );
      }
    
      @override
      void dispose() {
        _inputText.removeListener(_onInputTextChanged);
        _inputText.dispose();
        super.dispose();
      }
    }
    
    
    
    Login or Signup to reply.
  2. Update your onPressed() method of calculate button as below:
    it will solve both issues, consider number and for empty string show 0

     onPressed: () {
    // calculation logic
       value = inputText.replaceAll(RegExp('[^A-Za-z0-9]'), '');
       int sum = 0;
       if (value.trim().isNotEmpty) {
           for (int i = 0; i < value.length; i++) {
                if (_isNumeric(value[i]) == true) {
                    sum += int.parse(value[i]);
                } else {
                  (value[i].toUpperCase() == value[i])
                         ? sum += (value.codeUnitAt(i) - 64)
                          : sum += (value.codeUnitAt(i) - 96);
               }
           }
        }
       setState(() {
          results = sum.toString() != "" ? sum.toString() : "0";
       });
    }
    
    //isNumeric func
     bool _isNumeric(String result) {
        return int.tryParse(result) != null;
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search