This commit is contained in:
DyCTaTOR 2024-10-01 20:53:17 +04:00
parent 3e9e7eb0ac
commit 98882d98b4
5 changed files with 176 additions and 70 deletions

7
lib/CourseStatus.dart Normal file
View File

@ -0,0 +1,7 @@
enum CourseStatus{
Math,
Physics,
Chemistry,
Biology,
ComputerScience,
}

12
lib/ExtensionStudent.dart Normal file
View 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
View 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
View 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();
}
}

View File

@ -1,70 +1,126 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'CourseStatus.dart';
void main() { import 'University.dart';
runApp(const MyApp()); import 'student.dart';
}
void main() {
class MyApp extends StatelessWidget { runApp(MyApp());
const MyApp({super.key}); }
@override class MyApp extends StatelessWidget {
Widget build(BuildContext context) { @override
return MaterialApp( Widget build(BuildContext context) {
title: 'Flutter Demo', return MaterialApp(
theme: ThemeData( title: 'University App',
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), theme: ThemeData(
useMaterial3: true, primarySwatch: Colors.blue,
), ),
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}); @override
_UniversityScreenState createState() => _UniversityScreenState();
}
final String title; class _UniversityScreenState extends State<UniversityScreen> {
final University university = University();
@override final TextEditingController nameController = TextEditingController();
State<MyHomePage> createState() => _MyHomePageState(); final TextEditingController ageController = TextEditingController();
} List<String> selectedCourses = [];
class _MyHomePageState extends State<MyHomePage> { void _addStudent() {
int _counter = 0; String name = nameController.text;
int age = int.tryParse(ageController.text) ?? 0;
void _incrementCounter() {
setState(() { if (name.isNotEmpty && age > 0 && selectedCourses.isNotEmpty) {
_counter++; setState(() {
}); // Создаем новый список курсов для каждого студента
} List<String> studentCourses = List.from(selectedCourses);
university.addStudent(Student(name, age, studentCourses));
@override nameController.clear();
Widget build(BuildContext context) { ageController.clear();
return Scaffold( selectedCourses.clear();
appBar: AppBar( });
backgroundColor: Theme.of(context).colorScheme.inversePrimary, }
title: Text(widget.title), }
),
body: Center( @override
child: Column( Widget build(BuildContext context) {
mainAxisAlignment: MainAxisAlignment.center, return Scaffold(
children: <Widget>[ appBar: AppBar(
const Text( title: Text('Строев Владимир, ПИбд-32'),
'You have pushed the button this many times:', ),
), body: Column(
Text( children: [
'$_counter', Padding(
style: Theme.of(context).textTheme.headlineMedium, padding: const EdgeInsets.all(8.0),
), child: Column(
], children: [
), Text('Университет', style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold)),
), SizedBox(height: 20),
floatingActionButton: FloatingActionButton( TextField(
onPressed: _incrementCounter, controller: nameController,
tooltip: 'Increment', decoration: InputDecoration(labelText: 'Name'),
child: const Icon(Icons.add), ),
), ); 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'),
),
],
),
),
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(", ")}'),
);
},
),
),
],
),
);
}
}