PIbd-32_Stroev_V_M_PMD/lib/main.dart
2024-10-03 15:56:21 +04:00

209 lines
6.2 KiB
Dart

import 'package:flutter/material.dart';
import 'CourseStatus.dart';
import 'University.dart';
import 'student.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'University App',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: UniversityScreen(),
);
}
}
class UniversityScreen extends StatefulWidget {
@override
_UniversityScreenState createState() => _UniversityScreenState();
}
class _UniversityScreenState extends State<UniversityScreen> {
final University university = University();
final TextEditingController nameController = TextEditingController();
final TextEditingController ageController = TextEditingController();
List<String> selectedCourses = [];
void _addStudent() async {
await Future.delayed(Duration(seconds: 1));
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(
appBar: AppBar(
title: Text('Строев Владимир, ПИбд-32'),
),
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'),
),
],
),
),
Expanded(
child: MyWidget(students: university.students),
),
],
),
);
}
}
class MyWidget extends StatelessWidget {
final List<Student> students;
const MyWidget({super.key, required this.students});
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: students.map((e) => _Card.fromData(e)).toList(),
),
);
}
}
class _Card extends StatelessWidget {
final String name;
final int age;
final List<String> courses;
final String? imageUrl;
const _Card(this.name, {
required this.age,
required this.courses,
this.imageUrl = 'https://gryazoveckij-r19.gosweb.gosuslugi.ru/netcat_files/460/2008/net_foto_muzh.jpg',
});
String getYearWord(int age) {
if (age % 10 == 1) {
return " год";
} else if (age % 10 < 5 && age % 10 != 0) {
return " года";
} else {
return " лет";
}
}
factory _Card.fromData(Student data) => _Card(data.name,
age: data.age, courses: data.courses);
@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.only(top: 16, left: 16, right: 16, bottom: 16),
decoration: BoxDecoration(
color: Colors.blueAccent,
borderRadius: BorderRadius.circular(20),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.2),
spreadRadius: 2,
blurRadius: 5,
offset: Offset(0, 3),
),
],
),
padding: const EdgeInsets.all(16),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(width: 16),
Flexible(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(name, style: Theme.of(context).textTheme.headlineLarge),
SizedBox(height: 8),
Text(age.toString() + getYearWord(age), style: Theme.of(context).textTheme.bodyLarge),
SizedBox(height: 8),
Text('Courses: ${courses.join(", ")}', style: Theme.of(context).textTheme.bodyMedium),
],
),
),
ClipRRect(
borderRadius: BorderRadius.circular(20),
child: SizedBox(
height: 140,
width: 100,
child: Image.network(imageUrl ?? '',
fit: BoxFit.cover,
errorBuilder: (_, __, ___) => const Placeholder(),),
),
),
],
),
);
}
}