To make the GPA calculator functional, you can follow these steps in Flutter:
Create a class called “Course” with attributes such as “name”, “grade”, and “credit hours”.
Create a list of type “Course” to store every inputted course.
Create a form where the user can input the course name, grade, and credit hours.
When the form is submitted, create a new “Course” object and append it to the list of courses.
Calculate the total credit hours and quality points by iterating through the list of courses.
Display the calculated GPA to the user.
Here is an example implementation of the above steps:
class Course {
String name;
String grade;
int creditHours;
Course({this.name, this.grade, this.creditHours});
}
List<Course> courses = [];
class GPAcalculator extends StatefulWidget {
@override
_GPAcalculatorState createState() => _GPAcalculatorState();
}
class _GPAcalculatorState extends State<GPAcalculator> {
final _formKey = GlobalKey<FormState>();
String _courseName;
String _grade;
int _creditHours;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("GPA Calculator")),
body: Padding(
padding: EdgeInsets.all(16.0),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
TextFormField(
decoration: InputDecoration(labelText: 'Course Name'),
validator: (value) {
if (value.isEmpty) {
return 'Please enter course name';
}
return null;
},
onSaved: (value) {
_courseName = value;
},
),
TextFormField(
decoration: InputDecoration(labelText: 'Grade'),
validator: (value) {
if (value.isEmpty) {
return 'Please enter grade';
}
return null;
},
onSaved: (value) {
_grade = value;
},
),
TextFormField(
decoration: InputDecoration(labelText: 'Credit Hours'),
keyboardType: TextInputType.number,
validator: (value) {
if (value.isEmpty) {
return 'Please enter credit hours';
}
return null;
},
onSaved: (value) {
_creditHours = int.parse(value);
},
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: RaisedButton(
onPressed: () {
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
Course newCourse = Course(
name: _courseName,
grade: _grade,
creditHours: _creditHours);
courses.add(newCourse);
_formKey.currentState.reset();
setState(() {});
}
},
child: Text('Submit'),
),
),
Divider(),
Text('Results:'),
Text('Total Credit Hours: ${_totalCreditHours()}'),
Text('Quality Points: ${_qualityPoints()}'),
Text('GPA: ${_calculateGPA()}')
],
),
),
),
);
}
int _totalCreditHours() {
int total = 0;
for (var course in courses) {
total += course.creditHours;
}
return total;
}
double _qualityPoints() {
double total = 0.0;
for (var course in courses) {
switch (course.grade) {
case 'A':
total += (4.0 * course.creditHours);
break;
case 'B':
total += (3.0 * course.creditHours);
break;
case 'C':
total += (2.0 * course.creditHours);
break;
case 'D':
total += (1.0 * course.creditHours);
break;
default:
break;
}
}
return total;
}
double _calculateGPA() {
return (_qualityPoints() / _totalCreditHours());
}
}
Note: This is just an example implementation and can be modified according to your specific requirements.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-07-16 11:00:00 +0000
Seen: 6 times
Last updated: Oct 16 '21
How can I deal with Expression.Error related to a column in Power Query?
How can you implement pagination in Oracle for the LISTAGG() function?
What is the process for implementing a FutureBuilder on an OnTap function in Flutter?
How can we require users to be logged in before they can access the root folders in WordPress?
In SCSS, what is the method for grouping and reusing a set of classes and styles?
How can popen() be used to direct streaming data to TAR?
How does iOS retrieve information from a BLE device?
How can Django Admin accommodate a variety of formats and locales for its input fields?