Skip to content

Instantly share code, notes, and snippets.

@mahanmarwat
Created February 5, 2022 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mahanmarwat/415ef681389331de9ec39aa31daafba1 to your computer and use it in GitHub Desktop.
Save mahanmarwat/415ef681389331de9ec39aa31daafba1 to your computer and use it in GitHub Desktop.
How to stretch this equal button in flutter?
import "package:flutter/material.dart";
class MainScreen extends StatefulWidget {
@override
State<MainScreen> createState() => _MainScreenState();
}
class _MainScreenState extends State<MainScreen> {
TextEditingController input_controller = TextEditingController();
var input_value = "";
var operator = "";
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Container(
padding: EdgeInsets.all(8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Center(
child: Container(
child: Image.asset("assets/images/bunny.png"),
height: 300,
),
),
// SizedBox(height: 180),
Text(
"Kalculator",
style: TextStyle(
fontSize: 60,
color: Colors.grey,
fontWeight: FontWeight.bold,
),
),
// SizedBox(height: 80),
TextField(
controller: input_controller,
cursorColor: Colors.black,
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.grey, width: 2)),
border: OutlineInputBorder(),
),
),
Row(
// mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Column(
// ! Can't stretch button.
children: [
ElevatedButton(
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(Colors.grey.shade600),
),
child: Text(
"=",
style: TextStyle(fontSize: 26),
),
onPressed: () {
setState(() {
if (input_controller.text.isNotEmpty) {
if (operator.isNotEmpty) {
input_controller.text = calculate(
int.parse(input_controller.text));
input_value = "";
operator = "";
}
}
});
}),
],
),
SizedBox(
width: 8,
),
Column(
children: [
ElevatedButton(
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(Colors.grey.shade600),
),
child: Icon(Icons.add),
onPressed: () {
setState(() {
if (input_controller.text.isNotEmpty) {
input_value = input_controller.text;
input_controller.text = "";
operator = "+";
}
});
}),
ElevatedButton(
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(Colors.grey.shade600),
),
child: Icon(Icons.remove),
onPressed: () {
if (input_controller.text.isNotEmpty) {
input_value = input_controller.text;
input_controller.text = "";
operator = "-";
}
}),
ElevatedButton(
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(Colors.grey.shade600),
),
child: Icon(Icons.close),
onPressed: () {
if (input_controller.text.isNotEmpty) {
input_value = input_controller.text;
input_controller.text = "";
operator = "x";
}
}),
ElevatedButton(
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(Colors.grey.shade600),
),
child: Text(
"/",
style: TextStyle(fontSize: 26),
),
onPressed: () {
if (input_controller.text.isNotEmpty) {
input_value = input_controller.text;
input_controller.text = "";
operator = "/";
}
}),
],
)
],
)
],
),
),
),
);
}
String calculate(int num2) {
if (input_value.isEmpty) {
return "";
}
var num1 = int.parse(input_value);
if (operator == "+") {
return "${num1 + num2}";
} else if (operator == "-") {
return "${num1 - num2}";
} else if (operator == "x") {
return "${num1 * num2}";
} else if (operator == "/") {
return "${num1 ~/ num2}";
} else {
return "";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment