Ad Code

Calculator project by flutter

 Calculator project by flutter


Project's video clip




Free source code here

main.dart

import 'package:flutter/material.dart';

import 'home_page.dart';

void main() {
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false,
home: HomePage(),
);
}
}


home_page.dart

import 'package:flutter/material.dart';
import 'package:math_expressions/math_expressions.dart';

import 'buttons.dart';

class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);

@override
State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
var userQuestion = '';
var userAnswer = '';

isOperator(String x) {
if (x == '%' || x == '/' || x == 'X' || x == '-' || x == '+' || x == '=') {
return true;
}
return false;
}

void calculation() {
String finalQuestion = userQuestion.replaceAll('X', '*');

Parser p = Parser();
Expression exp = p.parse(finalQuestion);
ContextModel cm = ContextModel();
double eval = exp.evaluate(EvaluationType.REAL, cm);

userAnswer = eval.toInt().toString();
}

final List<String> buttons = [
'C',
'DEL',
'%',
'/',
'9',
'8',
'7',
'X',
'6',
'5',
'4',
'-',
'3',
'2',
'1',
'+',
'0',
'.',
'ANS',
'=',
];
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.deepPurple[100],
body: Column(
children: [
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Container(
padding: const EdgeInsets.only(top: 50.0, left: 20),
alignment: Alignment.centerLeft,
child: Text(
userQuestion,
style: const TextStyle(fontSize: 24),
),
),
Container(
padding: const EdgeInsets.only(top: 60.0, right: 20),
alignment: Alignment.centerRight,
child: Text(
userAnswer,
style: const TextStyle(fontSize: 24),
)),
],
)),
Expanded(
flex: 3,
child: GridView.builder(
physics: const NeverScrollableScrollPhysics(),
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 4),
itemCount: buttons.length,
itemBuilder: (BuildContext context, int index) {
// Clear button
if (index == 0) {
return MyButton(
buttonTap: () {
setState(() {
if (userQuestion.isNotEmpty ||
userAnswer.isNotEmpty) {
userQuestion = '';
userAnswer = '';
}
});
},
buttonColor: Colors.green,
textColor: Colors.white,
buttonText: buttons[index],
);
}
// del button
else if (index == 1) {
return MyButton(
buttonTap: () {
setState(() {
if (userQuestion.isNotEmpty) {
userQuestion = userQuestion.substring(
0, userQuestion.length - 1);
}
});
},
buttonColor: Colors.red,
textColor: Colors.white,
buttonText: buttons[index],
);
}
// Equal button
else if (index == buttons.length - 1) {
return MyButton(
buttonTap: () {
setState(() {
calculation();
});
},
buttonColor: Colors.deepPurple,
textColor: Colors.white,
buttonText: buttons[index],
);
}
// Others button
else {
return MyButton(
buttonTap: () {
setState(() {
userQuestion += buttons[index];
});
},
buttonColor: isOperator(buttons[index])
? Colors.deepPurple
: Colors.deepPurple[50],
textColor: isOperator(buttons[index])
? Colors.white
: Colors.deepPurple,
buttonText: buttons[index],
);
}
},
)),
],
),
);
}
}


buttons.dart

import 'package:flutter/material.dart';

class MyButton extends StatelessWidget {
final buttonColor;
final textColor;
final String buttonText;
final buttonTap;

const MyButton(
{Key? key,
this.buttonColor,
this.textColor,
required this.buttonText,
this.buttonTap})
: super(key: key);

@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: buttonTap,
child: Padding(
padding: const EdgeInsets.all(4.0),
child: ClipRRect(
borderRadius: BorderRadius.circular(30),
child: Container(
color: buttonColor,
child: Center(
child: Text(
buttonText,
style: TextStyle(color: textColor, fontSize: 24),
),
),
),
),
),
);
}
}







Post a Comment

0 Comments