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
- it is not calculating to single digit,
- it is not calculating for numbers,
- 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
I hope you will find this helpful
Update your onPressed() method of calculate button as below:
it will solve both issues, consider number and for empty string show 0