Mid Term Solution
Mid Term Solution
Abbottabad Campus
1. Create a class Person with attributes: id, name, age. Derive a class from person, named
Student. The extra attributes of Student are cgpa, currently enrolled semester (e.g., FA22
or SP22, etc), admission date. Populate a list with at least 3 records of Student class. Print
list of students whose cgpa is greater than 3.7. [CLO-1, C2][Marks: 15]
class Person {
String? id;
String? name;
int? age;
void main() {
List<Student> students = [
Student('123', 'John Doe', 21, 3.1, 'Fa23', '2022-09-01'),
Student('456', 'Shahid Gul', 12, 2.8, 'Fa22', '2023-09-01'),
Student('432', 'Muneeza Malik', 21, 3.9, 'Sp22', '2023-10-01'),
Student('789', 'Javed Henry', 30, 2.9, 'Sp23', '2025-09-01'),
Student('728', 'Neelam Khan', 30, 3.7, 'Sp23', '2025-09-01'),
];
students.where((student) => student.cgpa! >= 3.7).forEach((student)=>print("${student.id}
${student.name}"));
1
2. Use flutter widgets to show this on screen. The screen background color is white, but you
are not allowed to use background color property. Here ONE, TWO, THREE, FOUR
represent Text() widgets. [CLO-2, C3][Marks: 15]
ONE TWO
THREE FOUR
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(
home: Scaffold(
body: Center(
child: InputDisplay(),
)
)
));
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[Text("ONE"),Text("TWO")]
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[Text("THREE"),Text("FOUR")]
),
],
);
}
3. Create the following app, when increment button is pressed, the value of counter increases.
You just need to write the code of the following class: [CLO-3, C3][Marks: 15]
2
class _CounterState extends State<Counter> {
Counter: 0
Increment
import 'package:flutter/material.dart';
void main() {
runApp(
const MaterialApp(
home: Scaffold(
body: Center(
child: Counter(),
),
),
),
);
}
const Counter({super.key});
@override
State<Counter> createState() => _CounterState();
}
void _increment() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children:<Widget>[
Row(
3
mainAxisAlignment: MainAxisAlignment.center,
children:<Widget>[
Text('Counter: $_counter'),
]
),
const SizedBox(height: 16),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: _increment,
child: const Text('Increment'),
)] ) ,
],
);
}
}
4
COMSATS University Islamabad
Abbottabad Campus
(NOTE: You need to write the solution on this same question paper)
Write code to create a GUI in flutter like this:. [CLO-4, C3][Marks: 10]
Hello World!
Solution:
import 'package:flutter/material.dart';
void main() {
runApp(
const MaterialApp(
title: 'Flutter Tutorial',
home: TutorialHome(),
),
);
}
@override
5
Widget build(BuildContext context) {
// Scaffold is a layout for
// the major Material Components.
return Scaffold(
appBar: AppBar(
title: const Center(child: Text('This is title bar')),
),
// body is the majority of the screen.
body: const Center(
child: Text('Hello, world!'),
),
);
}
}