Skip to content

Instantly share code, notes, and snippets.

@davidmigloz
Created December 5, 2023 17:44
Show Gist options
  • Save davidmigloz/1e0754efec32deee939e4853857c1748 to your computer and use it in GitHub Desktop.
Save davidmigloz/1e0754efec32deee939e4853857c1748 to your computer and use it in GitHub Desktop.
Generated code from pixels2flutter.dev
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(CalculatorApp());
}
class CalculatorApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData.dark().copyWith(
scaffoldBackgroundColor: Colors.black,
),
home: CalculatorScreen(),
);
}
}
class CalculatorScreen extends StatefulWidget {
@override
_CalculatorScreenState createState() => _CalculatorScreenState();
}
class _CalculatorScreenState extends State<CalculatorScreen> {
String _display = '0';
double _firstOperand = 0.0;
double _secondOperand = 0.0;
String _operator = '';
bool _shouldCalculate = false;
void _onButtonPressed(String value) {
setState(() {
if (value == 'C') {
_display = '0';
_firstOperand = 0.0;
_secondOperand = 0.0;
_operator = '';
_shouldCalculate = false;
} else if ('+-×÷%'.contains(value)) {
_firstOperand = double.tryParse(_display) ?? 0.0;
_operator = value;
_shouldCalculate = true;
} else if (value == '=') {
_secondOperand = double.tryParse(_display) ?? 0.0;
double result = _calculateResult();
_display = result.toString();
_shouldCalculate = false;
} else {
if (_shouldCalculate) {
_display = '';
_shouldCalculate = false;
}
_display = _display == '0' ? value : _display + value;
}
});
}
double _calculateResult() {
switch (_operator) {
case '+':
return _firstOperand + _secondOperand;
case '-':
return _firstOperand - _secondOperand;
case '×':
return _firstOperand * _secondOperand;
case '÷':
return _firstOperand / _secondOperand;
case '%':
return _firstOperand % _secondOperand;
default:
return 0.0;
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Expanded(
child: Container(
padding: EdgeInsets.all(32),
alignment: Alignment.bottomRight,
child: Text(
_display,
style: TextStyle(
color: Colors.white,
fontSize: 100,
fontWeight: FontWeight.w200,
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
),
GridView.count(
shrinkWrap: true,
crossAxisCount: 4,
childAspectRatio: 1.0,
children: [
'C', '+/-', '%', '÷',
'7', '8', '9', '×',
'4', '5', '6', '-',
'1', '2', '3', '+',
'0', '.', '=',
].map((value) {
bool isOperator = '+-×÷=%'.contains(value);
bool isZeroButton = value == '0';
return GridTile(
child: GestureDetector(
onTap: () => _onButtonPressed(value),
child: Container(
margin: EdgeInsets.all(8),
decoration: BoxDecoration(
color: isOperator ? Colors.orange : Colors.grey[850],
shape: isZeroButton ? BoxShape.rectangle : BoxShape.circle,
borderRadius: isZeroButton ? BorderRadius.circular(50) : null,
),
child: Center(
child: Text(
value,
style: TextStyle(
color: isOperator ? Colors.white : Colors.grey[300],
fontSize: 36,
fontWeight: FontWeight.normal,
),
),
),
),
),
);
}).toList(),
),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment