Skip to content

Instantly share code, notes, and snippets.

@isaacadariku
Last active April 24, 2022 14:26
Show Gist options
  • Save isaacadariku/96c723c62e5bc31af6e3c67243257ee4 to your computer and use it in GitHub Desktop.
Save isaacadariku/96c723c62e5bc31af6e3c67243257ee4 to your computer and use it in GitHub Desktop.
Grading system
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Grading System'),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
const MyHomePage({
Key? key,
required this.title,
}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final TextEditingController _subjectController = TextEditingController();
final TextEditingController _gradeController = TextEditingController();
String output = "";
String error = "";
Text displayOutput() {
if (error.isNotEmpty) {
return Text(
error,
style: const TextStyle(fontSize: 20.0, color: Colors.red),
);
} else {
return Text(
output,
style: const TextStyle(fontSize: 20.0, color: Colors.green),
);
}
}
void _calculateGrade() {
print('running');
// Check if the user has entered a subject and grade
if (_subjectController.text.isEmpty || _gradeController.text.isEmpty) {
error = "Please enter both grade and subject";
return;
}
// Check if the user has entered a valid grade
if (_gradeController.text.contains(".") ||
_gradeController.text.contains("-") ||
_gradeController.text.contains("+")) {
error = "Please enter a valid grade";
return;
}
// Check if the user has entered a valid subject
final gradeValue = int.parse(_gradeController.text);
final subjectValue = _subjectController.text;
// Calculate the grade and set the output
if (gradeValue >= 80) {
output = "Your grade for $subjectValue is A";
} else if (gradeValue >= 60) {
output = "Your grade for $subjectValue is B";
} else if (gradeValue >= 50) {
output = "Your grade for $subjectValue is C";
} else if (gradeValue >= 40) {
output = "Your grade for $subjectValue is D";
} else {
output = "Your grade for $subjectValue is F";
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(15.0),
child: Row(
children: [
Expanded(
child: TextField(
controller: _subjectController,
keyboardType: TextInputType.text,
decoration: const InputDecoration(
hintText: 'Enter Subject',
border: OutlineInputBorder(),
contentPadding: EdgeInsets.symmetric(
horizontal: 10.0,
vertical: 10.0,
),
),
onChanged: (value) {
// Clear the output and error
output = "";
error = "";
setState(() {});
},
),
),
const SizedBox(width: 10),
Expanded(
child: TextField(
controller: _gradeController,
maxLength: 3,
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly
],
decoration: const InputDecoration(
hintText: 'Enter Score',
border: OutlineInputBorder(),
counterText: "",
contentPadding: EdgeInsets.symmetric(
horizontal: 10.0,
vertical: 10.0,
),
),
onChanged: (value) {
// Clear the output and error
output = "";
error = "";
setState(() {});
},
),
),
],
),
),
const SizedBox(height: 20.0),
displayOutput(),
const SizedBox(height: 20.0),
ElevatedButton(
onPressed: () {
_calculateGrade();
setState(() {});
},
child: const Text('Calculate'),
),
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment