update calculator to use MihSignlewScroll

This commit is contained in:
2025-03-17 10:44:45 +02:00
parent aa0472afe5
commit 20e02169d4
2 changed files with 304 additions and 286 deletions

View File

@@ -1,5 +1,6 @@
import 'package:Mzansi_Innovation_Hub/main.dart';
import 'package:Mzansi_Innovation_Hub/mih_components/mih_inputs_and_buttons/mih_button.dart';
import 'package:Mzansi_Innovation_Hub/mih_components/mih_layout/mih_single_child_scroll.dart';
import 'package:Mzansi_Innovation_Hub/mih_components/mih_package/mih-app_tool_body.dart';
import 'package:flutter/material.dart';
import 'package:math_expressions/math_expressions.dart';
@@ -73,219 +74,231 @@ class _SimpleCalcState extends State<SimpleCalc> {
calcWidth = 300;
}
}
return Column(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(
"Simple Calculator",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold,
color: MzanziInnovationHub.of(context)!.theme.secondaryColor(),
),
),
Divider(color: MzanziInnovationHub.of(context)!.theme.secondaryColor()),
const SizedBox(height: 10),
Container(
//color: Colors.white,
padding: const EdgeInsets.all(20),
alignment: Alignment.centerRight,
child: Text(
userInput,
return MihSingleChildScroll(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(
"Simple Calculator",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 18,
fontSize: 25,
fontWeight: FontWeight.bold,
color: MzanziInnovationHub.of(context)!.theme.secondaryColor(),
),
),
),
Container(
width: double.infinity,
//color: Colors.white,
padding: const EdgeInsets.all(15),
alignment: Alignment.centerRight,
child: Text(
answer,
style: TextStyle(
fontSize: 30,
Divider(
color: MzanziInnovationHub.of(context)!.theme.secondaryColor()),
const SizedBox(height: 10),
Container(
//color: Colors.white,
padding: const EdgeInsets.all(20),
alignment: Alignment.centerRight,
child: Text(
userInput,
style: TextStyle(
fontSize: 18,
color: MzanziInnovationHub.of(context)!.theme.secondaryColor(),
fontWeight: FontWeight.bold),
),
),
Container(
alignment: Alignment.centerRight,
child: SizedBox(
width: calcWidth,
child: GridView.builder(
physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true,
// padding: EdgeInsets.only(
// left: width / 10,
// right: width / 10,
// bottom: height / 15,
// //top: 20,
// ),
// shrinkWrap: true,
itemCount: buttons.length,
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 4,
//mainAxisExtent: 150,
),
itemBuilder: (context, index) {
// Clear Button
if (index == 0) {
return Padding(
padding: const EdgeInsets.all(4.0),
child: MIHButton(
onTap: () {
setState(() {
userInput = '';
answer = '0';
});
},
buttonText: buttons[index],
buttonColor: MzanziInnovationHub.of(context)!
.theme
.messageTextColor(),
textColor:
MzanziInnovationHub.of(context)!.theme.primaryColor(),
),
);
}
// +/- button
else if (index == 1) {
return Padding(
padding: const EdgeInsets.all(4.0),
child: MIHButton(
onTap: () {
setState(() {
userInput += buttons[index];
});
},
buttonText: buttons[index],
buttonColor: MzanziInnovationHub.of(context)!
.theme
.messageTextColor(),
textColor:
MzanziInnovationHub.of(context)!.theme.primaryColor(),
),
);
}
// % Button
else if (index == 2) {
return Padding(
padding: const EdgeInsets.all(4.0),
child: MIHButton(
onTap: () {
setState(() {
userInput += buttons[index];
});
},
buttonText: buttons[index],
buttonColor: MzanziInnovationHub.of(context)!
.theme
.messageTextColor(),
textColor:
MzanziInnovationHub.of(context)!.theme.primaryColor(),
),
);
}
// Delete Button
else if (index == 3) {
return Padding(
padding: const EdgeInsets.all(4.0),
child: MIHButton(
onTap: () {
setState(() {
userInput =
userInput.substring(0, userInput.length - 1);
});
},
buttonText: buttons[index],
buttonColor:
MzanziInnovationHub.of(context)!.theme.errorColor(),
textColor:
MzanziInnovationHub.of(context)!.theme.primaryColor(),
),
);
}
// Equal_to Button
else if (index == 18) {
return Padding(
padding: const EdgeInsets.all(4.0),
child: MIHButton(
onTap: () {
setState(() {
equalPressed();
});
},
buttonText: buttons[index],
buttonColor:
MzanziInnovationHub.of(context)!.theme.successColor(),
textColor:
MzanziInnovationHub.of(context)!.theme.primaryColor(),
),
);
}
// +, -, / x buttons
else if (index == 7 ||
index == 11 ||
index == 15 ||
index == 19) {
return Padding(
padding: const EdgeInsets.all(4.0),
child: MIHButton(
onTap: () {
if (answer == "0") {
setState(() {
userInput += buttons[index];
});
} else {
setState(() {
userInput = answer;
answer = "0";
userInput += buttons[index];
});
}
// setState(() {
// userInput += buttons[index];
// });
},
buttonText: buttons[index],
buttonColor: MzanziInnovationHub.of(context)!
.theme
.messageTextColor(),
textColor:
MzanziInnovationHub.of(context)!.theme.primaryColor(),
),
);
}
// other buttons
else {
return Padding(
padding: const EdgeInsets.all(4.0),
child: MIHButton(
onTap: () {
setState(() {
userInput += buttons[index];
});
},
buttonText: buttons[index],
buttonColor: MzanziInnovationHub.of(context)!
.theme
.secondaryColor(),
textColor:
MzanziInnovationHub.of(context)!.theme.primaryColor(),
),
);
}
},
),
),
),
],
Container(
width: double.infinity,
//color: Colors.white,
padding: const EdgeInsets.all(15),
alignment: Alignment.centerRight,
child: Text(
answer,
style: TextStyle(
fontSize: 30,
color:
MzanziInnovationHub.of(context)!.theme.secondaryColor(),
fontWeight: FontWeight.bold),
),
),
Container(
alignment: Alignment.centerRight,
child: SizedBox(
width: calcWidth,
child: GridView.builder(
physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true,
// padding: EdgeInsets.only(
// left: width / 10,
// right: width / 10,
// bottom: height / 15,
// //top: 20,
// ),
// shrinkWrap: true,
itemCount: buttons.length,
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 4,
//mainAxisExtent: 150,
),
itemBuilder: (context, index) {
// Clear Button
if (index == 0) {
return Padding(
padding: const EdgeInsets.all(4.0),
child: MIHButton(
onTap: () {
setState(() {
userInput = '';
answer = '0';
});
},
buttonText: buttons[index],
buttonColor: MzanziInnovationHub.of(context)!
.theme
.messageTextColor(),
textColor: MzanziInnovationHub.of(context)!
.theme
.primaryColor(),
),
);
}
// +/- button
else if (index == 1) {
return Padding(
padding: const EdgeInsets.all(4.0),
child: MIHButton(
onTap: () {
setState(() {
userInput += buttons[index];
});
},
buttonText: buttons[index],
buttonColor: MzanziInnovationHub.of(context)!
.theme
.messageTextColor(),
textColor: MzanziInnovationHub.of(context)!
.theme
.primaryColor(),
),
);
}
// % Button
else if (index == 2) {
return Padding(
padding: const EdgeInsets.all(4.0),
child: MIHButton(
onTap: () {
setState(() {
userInput += buttons[index];
});
},
buttonText: buttons[index],
buttonColor: MzanziInnovationHub.of(context)!
.theme
.messageTextColor(),
textColor: MzanziInnovationHub.of(context)!
.theme
.primaryColor(),
),
);
}
// Delete Button
else if (index == 3) {
return Padding(
padding: const EdgeInsets.all(4.0),
child: MIHButton(
onTap: () {
setState(() {
userInput =
userInput.substring(0, userInput.length - 1);
});
},
buttonText: buttons[index],
buttonColor:
MzanziInnovationHub.of(context)!.theme.errorColor(),
textColor: MzanziInnovationHub.of(context)!
.theme
.primaryColor(),
),
);
}
// Equal_to Button
else if (index == 18) {
return Padding(
padding: const EdgeInsets.all(4.0),
child: MIHButton(
onTap: () {
setState(() {
equalPressed();
});
},
buttonText: buttons[index],
buttonColor: MzanziInnovationHub.of(context)!
.theme
.successColor(),
textColor: MzanziInnovationHub.of(context)!
.theme
.primaryColor(),
),
);
}
// +, -, / x buttons
else if (index == 7 ||
index == 11 ||
index == 15 ||
index == 19) {
return Padding(
padding: const EdgeInsets.all(4.0),
child: MIHButton(
onTap: () {
if (answer == "0") {
setState(() {
userInput += buttons[index];
});
} else {
setState(() {
userInput = answer;
answer = "0";
userInput += buttons[index];
});
}
// setState(() {
// userInput += buttons[index];
// });
},
buttonText: buttons[index],
buttonColor: MzanziInnovationHub.of(context)!
.theme
.messageTextColor(),
textColor: MzanziInnovationHub.of(context)!
.theme
.primaryColor(),
),
);
}
// other buttons
else {
return Padding(
padding: const EdgeInsets.all(4.0),
child: MIHButton(
onTap: () {
setState(() {
userInput += buttons[index];
});
},
buttonText: buttons[index],
buttonColor: MzanziInnovationHub.of(context)!
.theme
.secondaryColor(),
textColor: MzanziInnovationHub.of(context)!
.theme
.primaryColor(),
),
);
}
},
),
),
),
],
),
);
}
}

View File

@@ -2,6 +2,7 @@ import 'package:Mzansi_Innovation_Hub/main.dart';
import 'package:Mzansi_Innovation_Hub/mih_components/mih_inputs_and_buttons/mih_button.dart';
import 'package:Mzansi_Innovation_Hub/mih_components/mih_inputs_and_buttons/mih_dropdown_input.dart';
import 'package:Mzansi_Innovation_Hub/mih_components/mih_inputs_and_buttons/mih_number_input.dart';
import 'package:Mzansi_Innovation_Hub/mih_components/mih_layout/mih_single_child_scroll.dart';
import 'package:Mzansi_Innovation_Hub/mih_components/mih_layout/mih_window.dart';
import 'package:Mzansi_Innovation_Hub/mih_components/mih_package/mih-app_tool_body.dart';
import 'package:Mzansi_Innovation_Hub/mih_components/mih_pop_up_messages/mih_error_message.dart';
@@ -268,91 +269,95 @@ class _TipCalcState extends State<TipCalc> {
}
Widget getBody() {
return Column(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Text(
"Tip Calculator",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold,
color: MzanziInnovationHub.of(context)!.theme.secondaryColor(),
return MihSingleChildScroll(
child: Column(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Text(
"Tip Calculator",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold,
color: MzanziInnovationHub.of(context)!.theme.secondaryColor(),
),
),
),
Divider(color: MzanziInnovationHub.of(context)!.theme.secondaryColor()),
const SizedBox(height: 10),
MIHNumberField(
controller: billAmountController,
hintText: "Bill Amount",
editable: true,
required: true,
enableDecimal: true,
),
const SizedBox(height: 10),
MIHNumberField(
controller: tipPercentageController,
hintText: "Tip %",
editable: true,
required: true,
enableDecimal: false,
),
const SizedBox(height: 10),
MIHDropdownField(
controller: splitBillController,
hintText: "Split Bill",
dropdownOptions: const ["Yes", "No"],
required: true,
editable: true,
enableSearch: false,
),
const SizedBox(height: 10),
ValueListenableBuilder(
valueListenable: splitValue,
builder: (BuildContext context, String value, Widget? child) {
return Visibility(
visible: value == "Yes",
child: Column(
children: [
MIHNumberField(
controller: noPeopleController,
hintText: "No. of People",
editable: true,
required: true,
enableDecimal: false,
),
const SizedBox(height: 10),
],
),
);
},
),
SizedBox(
width: double.infinity,
height: 50,
child: MIHButton(
onTap: () {
validateInput();
Divider(
color: MzanziInnovationHub.of(context)!.theme.secondaryColor()),
const SizedBox(height: 10),
MIHNumberField(
controller: billAmountController,
hintText: "Bill Amount",
editable: true,
required: true,
enableDecimal: true,
),
const SizedBox(height: 10),
MIHNumberField(
controller: tipPercentageController,
hintText: "Tip %",
editable: true,
required: true,
enableDecimal: false,
),
const SizedBox(height: 10),
MIHDropdownField(
controller: splitBillController,
hintText: "Split Bill",
dropdownOptions: const ["Yes", "No"],
required: true,
editable: true,
enableSearch: false,
),
const SizedBox(height: 10),
ValueListenableBuilder(
valueListenable: splitValue,
builder: (BuildContext context, String value, Widget? child) {
return Visibility(
visible: value == "Yes",
child: Column(
children: [
MIHNumberField(
controller: noPeopleController,
hintText: "No. of People",
editable: true,
required: true,
enableDecimal: false,
),
const SizedBox(height: 10),
],
),
);
},
buttonText: "Calculate",
buttonColor: MzanziInnovationHub.of(context)!.theme.successColor(),
textColor: MzanziInnovationHub.of(context)!.theme.primaryColor(),
),
),
const SizedBox(height: 10),
SizedBox(
width: double.infinity,
height: 50,
child: MIHButton(
onTap: () {
clearInput();
},
buttonText: "Clear",
buttonColor: MzanziInnovationHub.of(context)!.theme.errorColor(),
textColor: MzanziInnovationHub.of(context)!.theme.primaryColor(),
SizedBox(
width: double.infinity,
height: 50,
child: MIHButton(
onTap: () {
validateInput();
},
buttonText: "Calculate",
buttonColor:
MzanziInnovationHub.of(context)!.theme.successColor(),
textColor: MzanziInnovationHub.of(context)!.theme.primaryColor(),
),
),
),
],
const SizedBox(height: 10),
SizedBox(
width: double.infinity,
height: 50,
child: MIHButton(
onTap: () {
clearInput();
},
buttonText: "Clear",
buttonColor: MzanziInnovationHub.of(context)!.theme.errorColor(),
textColor: MzanziInnovationHub.of(context)!.theme.primaryColor(),
),
),
],
),
);
}
}