lab2
This commit is contained in:
parent
3e9e7eb0ac
commit
98882d98b4
7
lib/CourseStatus.dart
Normal file
7
lib/CourseStatus.dart
Normal file
@ -0,0 +1,7 @@
|
||||
enum CourseStatus{
|
||||
Math,
|
||||
Physics,
|
||||
Chemistry,
|
||||
Biology,
|
||||
ComputerScience,
|
||||
}
|
12
lib/ExtensionStudent.dart
Normal file
12
lib/ExtensionStudent.dart
Normal file
@ -0,0 +1,12 @@
|
||||
import 'CourseStatus.dart';
|
||||
import 'Student.dart';
|
||||
|
||||
extension StudentCourseStatus on Student {
|
||||
String getCourseStatus(String course) {
|
||||
if (courses.contains(course)) {
|
||||
return "Студент присутствует на этих курсах";
|
||||
} else {
|
||||
return "Студент отсутствует на этих курсах";
|
||||
}
|
||||
}
|
||||
}
|
12
lib/Student.dart
Normal file
12
lib/Student.dart
Normal file
@ -0,0 +1,12 @@
|
||||
class Student {
|
||||
String name;
|
||||
int age;
|
||||
List<String> courses;
|
||||
|
||||
Student(this.name, this.age, this.courses);
|
||||
|
||||
void displayInfo() {
|
||||
print('Name: $name, Age: $age');
|
||||
print('Courses: ${courses.join(", ")}');
|
||||
}
|
||||
}
|
19
lib/University.dart
Normal file
19
lib/University.dart
Normal file
@ -0,0 +1,19 @@
|
||||
import 'student.dart';
|
||||
|
||||
class University {
|
||||
List<Student> students = [];
|
||||
|
||||
void addStudent(Student student) {
|
||||
students.add(student);
|
||||
}
|
||||
|
||||
void displayAllStudents() {
|
||||
for (var student in students) {
|
||||
student.displayInfo();
|
||||
}
|
||||
}
|
||||
|
||||
List<Student> getStudentsByCourse(String course) {
|
||||
return students.where((student) => student.courses.contains(course)).toList();
|
||||
}
|
||||
}
|
138
lib/main.dart
138
lib/main.dart
@ -1,70 +1,126 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'CourseStatus.dart';
|
||||
import 'University.dart';
|
||||
import 'student.dart';
|
||||
|
||||
void main() {
|
||||
runApp(const MyApp());
|
||||
runApp(MyApp());
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
@override
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: 'Flutter Demo',
|
||||
title: 'University App',
|
||||
theme: ThemeData(
|
||||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
|
||||
useMaterial3: true,
|
||||
primarySwatch: Colors.blue,
|
||||
),
|
||||
home: const MyHomePage(title: 'Flutter Demo Home Page'),
|
||||
home: UniversityScreen(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class MyHomePage extends StatefulWidget {
|
||||
const MyHomePage({super.key, required this.title});
|
||||
|
||||
|
||||
|
||||
final String title;
|
||||
|
||||
class UniversityScreen extends StatefulWidget {
|
||||
@override
|
||||
State<MyHomePage> createState() => _MyHomePageState();
|
||||
_UniversityScreenState createState() => _UniversityScreenState();
|
||||
}
|
||||
|
||||
class _MyHomePageState extends State<MyHomePage> {
|
||||
int _counter = 0;
|
||||
class _UniversityScreenState extends State<UniversityScreen> {
|
||||
final University university = University();
|
||||
final TextEditingController nameController = TextEditingController();
|
||||
final TextEditingController ageController = TextEditingController();
|
||||
List<String> selectedCourses = [];
|
||||
|
||||
void _incrementCounter() {
|
||||
setState(() {
|
||||
_counter++;
|
||||
});
|
||||
void _addStudent() {
|
||||
String name = nameController.text;
|
||||
int age = int.tryParse(ageController.text) ?? 0;
|
||||
|
||||
if (name.isNotEmpty && age > 0 && selectedCourses.isNotEmpty) {
|
||||
setState(() {
|
||||
// Создаем новый список курсов для каждого студента
|
||||
List<String> studentCourses = List.from(selectedCourses);
|
||||
university.addStudent(Student(name, age, studentCourses));
|
||||
nameController.clear();
|
||||
ageController.clear();
|
||||
selectedCourses.clear();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
||||
title: Text(widget.title),
|
||||
title: Text('Строев Владимир, ПИбд-32'),
|
||||
),
|
||||
body: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
const Text(
|
||||
'You have pushed the button this many times:',
|
||||
body: Column(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Column(
|
||||
children: [
|
||||
Text('Университет', style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold)),
|
||||
SizedBox(height: 20),
|
||||
TextField(
|
||||
controller: nameController,
|
||||
decoration: InputDecoration(labelText: 'Name'),
|
||||
),
|
||||
TextField(
|
||||
controller: ageController,
|
||||
decoration: InputDecoration(labelText: 'Age'),
|
||||
keyboardType: TextInputType.number,
|
||||
),
|
||||
DropdownButton<String>(
|
||||
value: selectedCourses.isNotEmpty ? selectedCourses.first : null,
|
||||
hint: Text('Select Courses'),
|
||||
onChanged: (String? newValue) {
|
||||
setState(() {
|
||||
if (newValue != null && !selectedCourses.contains(newValue)) {
|
||||
selectedCourses.add(newValue);
|
||||
}
|
||||
});
|
||||
},
|
||||
items: CourseStatus.values.map<DropdownMenuItem<String>>((CourseStatus value) {
|
||||
return DropdownMenuItem<String>(
|
||||
value: value.name,
|
||||
child: Text(value.name),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
Wrap(
|
||||
spacing: 8.0,
|
||||
children: selectedCourses.map((course) {
|
||||
return Chip(
|
||||
label: Text(course),
|
||||
onDeleted: () {
|
||||
setState(() {
|
||||
selectedCourses.remove(course);
|
||||
});
|
||||
},
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: _addStudent,
|
||||
child: Text('Add Student'),
|
||||
),
|
||||
],
|
||||
),
|
||||
Text(
|
||||
'$_counter',
|
||||
style: Theme.of(context).textTheme.headlineMedium,
|
||||
),
|
||||
Expanded(
|
||||
child: ListView.builder(
|
||||
itemCount: university.students.length,
|
||||
itemBuilder: (context, index) {
|
||||
Student student = university.students[index];
|
||||
return ListTile(
|
||||
title: Text(student.name),
|
||||
subtitle: Text('Age: ${student.age}, Courses: ${student.courses.join(", ")}'),
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: _incrementCounter,
|
||||
tooltip: 'Increment',
|
||||
child: const Icon(Icons.add),
|
||||
), );
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user