Skip to content

Instantly share code, notes, and snippets.

@Abhilash-Chandran
Created February 6, 2020 12:25
Show Gist options
  • Save Abhilash-Chandran/9f7f87cb8e15fe1385c017d5fb884858 to your computer and use it in GitHub Desktop.
Save Abhilash-Chandran/9f7f87cb8e15fe1385c017d5fb884858 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
void main() => runApp(new CalculatorApp());
class CalculatorApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(title: 'Bending Calculator', home: Calculator());
}
}
class Calculator extends StatefulWidget {
@override
State<StatefulWidget> createState() => Calculatore();
}
class Calculatore extends State<Calculator> {
final a = TextEditingController();
final b = TextEditingController();
final c = TextEditingController();
// controller mentioned
String total = "";
void calculate() {
print('hello');
int numberA = int.tryParse(a.text);
int numberB = int.tryParse(b?.text);
int numberC = int.tryParse(c?.text);
int result;
// if numberA have value then answer will be a+c
// what condition to do here for between choosing between textfields a or b.
// i tried numberB ==null that does not work
// very much confused, no idea what to do please help
if (numberA != null && numberC != null) {
result = numberA + numberC;
} else if (numberB != null && numberC != null){
result = numberB + numberC;
}
setState(() {
total = "$result";
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Calculator")),
body: SafeArea(
child: ListView(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
// first textfield
Container(
width: MediaQuery.of(context).size.width * 0.45,
height: 50,
child: TextField(
controller: a,
decoration: InputDecoration(hintText: " Enter a value"),
keyboardType: TextInputType.number),
),
Container(
width: MediaQuery.of(context).size.width * 0.04,
height: 50,
),
// second textfield
Container(
width: MediaQuery.of(context).size.width * 0.45,
height: 50,
child: TextField(
controller: b,
decoration: InputDecoration(hintText: " Enter b value"),
keyboardType: TextInputType.number),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
// third textfield
Container(
width: MediaQuery.of(context).size.width * 0.9,
height: 50,
child: TextField(
controller: c,
decoration: InputDecoration(hintText: " Enter c value"),
keyboardType: TextInputType.number),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
//button
RaisedButton(
onPressed: calculate,
child: Text('Calculate'),
),
],
),
Text(
" Total : $total",
style: TextStyle(fontSize: 20.0),
),
],
)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment