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 'package:flutter/material.dart';
|
||||||
|
import 'CourseStatus.dart';
|
||||||
|
import 'University.dart';
|
||||||
|
import 'student.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
runApp(const MyApp());
|
runApp(MyApp());
|
||||||
}
|
}
|
||||||
|
|
||||||
class MyApp extends StatelessWidget {
|
class MyApp extends StatelessWidget {
|
||||||
const MyApp({super.key});
|
@override
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return MaterialApp(
|
return MaterialApp(
|
||||||
title: 'Flutter Demo',
|
title: 'University App',
|
||||||
theme: ThemeData(
|
theme: ThemeData(
|
||||||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
|
primarySwatch: Colors.blue,
|
||||||
useMaterial3: true,
|
|
||||||
),
|
),
|
||||||
home: const MyHomePage(title: 'Flutter Demo Home Page'),
|
home: UniversityScreen(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class MyHomePage extends StatefulWidget {
|
class UniversityScreen extends StatefulWidget {
|
||||||
const MyHomePage({super.key, required this.title});
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
final String title;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<MyHomePage> createState() => _MyHomePageState();
|
_UniversityScreenState createState() => _UniversityScreenState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class _MyHomePageState extends State<MyHomePage> {
|
class _UniversityScreenState extends State<UniversityScreen> {
|
||||||
int _counter = 0;
|
final University university = University();
|
||||||
|
final TextEditingController nameController = TextEditingController();
|
||||||
|
final TextEditingController ageController = TextEditingController();
|
||||||
|
List<String> selectedCourses = [];
|
||||||
|
|
||||||
void _incrementCounter() {
|
void _addStudent() {
|
||||||
setState(() {
|
String name = nameController.text;
|
||||||
_counter++;
|
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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
title: Text('Строев Владимир, ПИбд-32'),
|
||||||
title: Text(widget.title),
|
|
||||||
),
|
),
|
||||||
body: Center(
|
body: Column(
|
||||||
child: Column(
|
children: [
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
Padding(
|
||||||
children: <Widget>[
|
padding: const EdgeInsets.all(8.0),
|
||||||
const Text(
|
child: Column(
|
||||||
'You have pushed the button this many times:',
|
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',
|
Expanded(
|
||||||
style: Theme.of(context).textTheme.headlineMedium,
|
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