import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(home: Calc()));
class Calc extends StatefulWidget {
@override
_CalcState createState() => _CalcState();
}
class _CalcState extends State<Calc> {
String d = '0', o = '';
double n1 = 0, n2 = 0;
void p(String v) {
setState(() {
if (v == 'C') {
d = '0'; o = ''; n1 = 0; n2 = 0;
} else if (v == '=') {
n2 = double.parse(d);
if (o == '+') d = (n1 + n2).toString();
if (o == '-') d = (n1 - n2).toString();
if (o == '×') d = (n1 * n2).toString();
if (o == '÷') d = (n1 / n2).toString();
o = '';
} else if (['+', '-', '×', '÷'].contains(v)) {
n1 = double.parse(d);
o = v;
d = '0';
} else {
d = d == '0' ? v : d + v;
}
});
}
Widget b(String t) => Expanded(
child: Padding(
padding: EdgeInsets.all(4),
child: ElevatedButton(
onPressed: () => p(t),
child: Text(t, style: TextStyle(fontSize: 24)),
style: ElevatedButton.styleFrom(padding: EdgeInsets.all(20)),
),
),
);
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: Text('Калькулятор')),
body: Column(
children: [
Expanded(
child: Container(
alignment: Alignment.bottomRight,
padding: EdgeInsets.all(24),
child: Text(d, style: TextStyle(fontSize: 48)),
),
),
Row(children: ['7', '8', '9', '÷'].map(b).toList()),
Row(children: ['4', '5', '6', '×'].map(b).toList()),
Row(children: ['1', '2', '3', '-'].map(b).toList()),
Row(children: ['C', '0', '=', '+'].map(b).toList()),
],
),
);
}