вроде готовая лаба 2

This commit is contained in:
Полина Чубыкина 2024-09-23 15:47:27 +04:00
parent 601b3434c3
commit 6eb0107e03
3 changed files with 70 additions and 20 deletions

View File

@ -1,6 +1,7 @@
import 'dart:math';
import 'package:flutter/material.dart';
import 'task.dart';
import 'taskManager.dart';
void main() {
runApp(const MyApp());
@ -32,50 +33,73 @@ class MyHomePage extends StatefulWidget {
State<MyHomePage> createState() => _MyHomePageState();
}
enum TaskPriority {
low,
medium,
high,
}
extension TaskPriorityExtension on TaskPriority {
String get displayName {
switch (this) {
case TaskPriority.low:
return 'Low';
case TaskPriority.medium:
return 'Medium';
case TaskPriority.high:
return 'High';
}
}
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
Color _color = Colors.orangeAccent;
void _incrementCounter() {
final TaskManager _taskManager = TaskManager();
void _addRandomTask() {
const priorities = TaskPriority.values;
final randomPriority = priorities[Random().nextInt(priorities.length)];
final newTask = Task('Task ${_taskManager.getTasks().length + 1}', randomPriority);
_taskManager.addTask(newTask);
setState(() {
_counter++;
_color = Color((Random().nextDouble() * 0xFFFFFF).toInt()).withOpacity(1.0);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: _color,
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
Expanded(
child: SingleChildScrollView(
child: Column(
children: <Widget>[
for (var task in _taskManager.getTasks())
Text(
'${task.title} - Priority: ${task.priority.displayName}',
style: const TextStyle(fontSize: 18),
),
],
),
),
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
if (_counter > 10)
Text(
'Why are you clicking?',
style: Theme.of(context).textTheme.headlineMedium,
)
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
onPressed: () async {
_addRandomTask();
await _taskManager.simulateTaskProcessing();
},
backgroundColor: _color,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);

8
lib/task.dart Normal file
View File

@ -0,0 +1,8 @@
import 'main.dart';
class Task {
final String title;
final TaskPriority priority;
Task(this.title, this.priority);
}

18
lib/taskManager.dart Normal file
View File

@ -0,0 +1,18 @@
import 'task.dart';
class TaskManager {
final List<Task> _tasks = [];
void addTask(Task task) {
_tasks.add(task);
}
List<Task> getTasks() {
return _tasks;
}
Future<void> simulateTaskProcessing() async {
await Future.delayed(const Duration(seconds: 2));
print('Задание выполнено');
}
}