Skip to content

Instantly share code, notes, and snippets.

@FeezyHendrix
Created April 10, 2021 21:53
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 FeezyHendrix/112c41c673f08309f20eb05fff128ae4 to your computer and use it in GitHub Desktop.
Save FeezyHendrix/112c41c673f08309f20eb05fff128ae4 to your computer and use it in GitHub Desktop.
GP Calculator Flutter
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Gp Calculator',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Vibes and Inshallah GP Calculator'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<Map<String, dynamic>> gpCalculator = [];
Map<String, int> grades = {"A": 5, "B": 4, "C": 3, "D": 2, "E": 1, "F": 0};
final List gradesList = ['A', 'B', 'C', 'D', 'E', 'F'];
String currentGP = "0";
void add() {
setState(() {
gpCalculator.add({"course": '', 'grade': 'A', 'unit': 0});
});
}
void remove(index) {
setState(() {
gpCalculator.removeAt(index);
});
}
void calculate() {
int TQP = 0;
int units = 0;
gpCalculator.forEach((value) {
var grade = grades[value['grade'].toString()];
print(value);
print(grade);
TQP += int.parse(value['unit']) * grade;
units += value['unit'];
});
print(TQP);
print(units);
// setState(() {
// currentGP = (TQP / units).toString();
// });
}
Widget buildForm(int index) {
// dynamic currentItem = gpCalculator[index];
return Container(
height: 100,
width: double.infinity,
child: Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
key: Key('' + index.toString()),
children: [
SizedBox(
child: TextField(
onChanged: (value) => gpCalculator[index]['course'] = value,
decoration: InputDecoration(hintText: 'Input Course'),
),
width: 100,
),
SizedBox(
width: 80,
child: DropdownButton<dynamic>(
value: gpCalculator[index]['grade'],
hint: Text(
'Select grade',
style: TextStyle(fontSize: 10),
),
onChanged: (value) {
setState(() {
gpCalculator[index]['grade'] = value;
});
},
items: gradesList.map((value) {
return DropdownMenuItem<dynamic>(
child: Text(value.toString()),
value: value,
);
}).toList(),
),
),
SizedBox(
width: 100,
child: TextField(
keyboardType: TextInputType.number,
onChanged: (value) => gpCalculator[index]['unit'] = value,
decoration: InputDecoration(hintText: 'Input Unit'),
),
),
IconButton(
onPressed: () => remove(index),
icon: Icon(Icons.remove_circle),
color: Colors.red,
),
],
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Container(
padding: EdgeInsets.all(16),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(
currentGP,
style: TextStyle(fontSize: 20),
),
MaterialButton(
color: Colors.red,
child: Text('ADD'),
onPressed: () => add(),
),
Expanded(
child: Column(
children: List.generate(
gpCalculator.length, (index) => buildForm(index),
growable: true),
)),
MaterialButton(
color: Colors.red,
child: Text('Calculate!'),
onPressed: () => calculate(),
),
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment