diff --git a/lib/controllers/gpacalculator_controller.dart b/lib/controllers/gpacalculator_controller.dart index c1cbd51..a9b93be 100644 --- a/lib/controllers/gpacalculator_controller.dart +++ b/lib/controllers/gpacalculator_controller.dart @@ -1,4 +1,5 @@ import 'package:academia/models/courses.dart'; +import 'package:flutter/widgets.dart'; import 'package:get/get.dart'; class GPACalculatorController extends GetxController { @@ -7,9 +8,18 @@ class GPACalculatorController extends GetxController { List get courseList => _courses; double get gpa => _calculateGPA(); - void addCourse(String name, String code, double creditHours, String grade) { + void addCourse(String name, String code, String creditHours, String grade) { + double tcreditHours = 0; + try { + tcreditHours = double.parse(creditHours); + } catch (e) { + Get.defaultDialog( + title: "Incorrect credit Hours", + content: const Text("Credit hours must be a number")); + throw Exception('Credit hours must be a number'); + } final course = - Unit(name: name, code: code, creditHours: creditHours, grade: grade); + Unit(name: name, code: code, creditHours: tcreditHours, grade: grade); _courses.add(course); } @@ -22,11 +32,18 @@ class GPACalculatorController extends GetxController { {required int index, required String newName, required String newCode, - required double newCreditHours, + required String newCreditHours, required String newGrade}) { _courses[index].name = newName; _courses[index].code = newCode; - _courses[index].creditHours = newCreditHours; + try { + _courses[index].creditHours = double.parse(newCreditHours); + } catch (e) { + Get.defaultDialog( + title: "Incorrect credit Hours", + content: const Text("Credit hours must be a number")); + throw Exception('Credit hours must be a number'); + } _courses[index].grade = newGrade; _courses.refresh(); } @@ -38,8 +55,6 @@ class GPACalculatorController extends GetxController { totalPoints += getGradePoints(course.grade) * course.creditHours; totalCreditHours += course.creditHours; } - print(totalPoints.toString()); - print(totalCreditHours.toString()); return (totalPoints / totalCreditHours).toPrecision(2); } diff --git a/lib/pages/gpacalculator_page.dart b/lib/pages/gpacalculator_page.dart index 2a1d248..39b57a5 100644 --- a/lib/pages/gpacalculator_page.dart +++ b/lib/pages/gpacalculator_page.dart @@ -43,9 +43,14 @@ class GpaCalculator extends StatelessWidget { const SizedBox(height: 20), ElevatedButton( onPressed: () { + if (unitController.courseList.length.isEqual(0)) { + Get.defaultDialog( + title: "List empty", + content: const Text("Input Grades!")); + } Get.defaultDialog( title: "Your GPA", - content: Text("Your GPA is ${unitController.gpa}"), + content: Text("Your GPA is ${unitController.gpa.toString()}"), ); }, child: const Text("Calculate GPA"), @@ -121,6 +126,8 @@ class GpaCalculator extends StatelessWidget { Padding( padding: const EdgeInsets.all(8.0), child: TextField( + keyboardType: + TextInputType.number, decoration: const InputDecoration( border: OutlineInputBorder(), labelText: 'Credit Hours', @@ -161,9 +168,8 @@ class GpaCalculator extends StatelessWidget { codeController .text, newCreditHours: - double.parse( - creditHoursController - .text), + creditHoursController + .text, newGrade: gradeController .text); @@ -262,6 +268,7 @@ class GpaCalculator extends StatelessWidget { Padding( padding: const EdgeInsets.all(8.0), child: TextField( + keyboardType: TextInputType.number, decoration: const InputDecoration( border: OutlineInputBorder(), labelText: 'Credit Hours', @@ -289,8 +296,13 @@ class GpaCalculator extends StatelessWidget { onPressed: () { final name = nameController.text; final code = codeController.text; - final creditHours = double.parse( - creditHoursController.text); + final creditHours = + creditHoursController.text; + + // if (creditHours is! double) { + // Get.snackbar("Invalid Credit hours", + // "Must be a number"); + // } final grade = gradeController.text; if (nameController.text != '' && codeController.text != '' && @@ -304,7 +316,7 @@ class GpaCalculator extends StatelessWidget { } else { Get.defaultDialog( title: "Invalid Grade", - content: Text( + content: const Text( "Please enter a valid grade"), ); } diff --git a/lib/pages/tool_page.dart b/lib/pages/tool_page.dart index 61d8b4a..3622fbb 100644 --- a/lib/pages/tool_page.dart +++ b/lib/pages/tool_page.dart @@ -25,7 +25,7 @@ class ToolsPage extends StatelessWidget { Get.to( const WebviewPage( title: "Daystar Elearning", - url: "https://student.daystar.ac.ke"), + url: "https://elearning.daystar.ac.ke"), ); }, icon: const Icon(