Lab2
This commit is contained in:
parent
35a3ba708e
commit
b3f16e9c47
@ -1,4 +1,5 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'weekManager.dart';
|
||||
|
||||
void main() {
|
||||
runApp(const MyApp());
|
||||
@ -12,10 +13,10 @@ class MyApp extends StatelessWidget {
|
||||
return MaterialApp(
|
||||
title: 'Flutter Demo',
|
||||
theme: ThemeData(
|
||||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
|
||||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
|
||||
useMaterial3: true,
|
||||
),
|
||||
home: const MyHomePage(title: 'Иванов Вячеслав Николаевич'),
|
||||
home: const MyHomePage(title: 'Dart Flutter Demo'),
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -30,11 +31,35 @@ class MyHomePage extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _MyHomePageState extends State<MyHomePage> {
|
||||
int _counter = 0;
|
||||
late List<WeekManager> _weekManagers;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_weekManagers = List.generate(5, (index) {
|
||||
DateTime deadline = DateTime.now().add(Duration(days: (index + 1) * 7));
|
||||
return WeekManager(deadline);
|
||||
});
|
||||
_updateDeadlines();
|
||||
}
|
||||
|
||||
Future<void> _updateDeadlines() async {
|
||||
for (var weekManager in _weekManagers) {
|
||||
await Future.delayed(const Duration(seconds: 1), () {
|
||||
setState(() {
|
||||
weekManager.updateCurrentDate();
|
||||
weekManager.updateCurrentDay();
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void _incrementCounter() {
|
||||
setState(() {
|
||||
_counter++;
|
||||
for (var weekManager in _weekManagers) {
|
||||
weekManager.incrementCurrentDate();
|
||||
weekManager.updateCurrentDay();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@ -49,13 +74,18 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
const Text(
|
||||
'You have pushed the button this many times:',
|
||||
),
|
||||
Text(
|
||||
'$_counter',
|
||||
style: Theme.of(context).textTheme.headlineMedium,
|
||||
),
|
||||
Text('Дедлайнер',
|
||||
style: Theme.of(context).textTheme.headlineMedium),
|
||||
const SizedBox(height: 20),
|
||||
..._weekManagers.map((weekManager) => Column(
|
||||
children: [
|
||||
Text('Текущий день недели: ${weekManager.currentDay.name}'),
|
||||
Text('Текущая дата: ${weekManager.formattedCurrentDate}'),
|
||||
Text('Дней до дедлайна: ${weekManager.daysUntilDeadline}'),
|
||||
Text('Дата дедлайна: ${weekManager.formattedDeadline}'),
|
||||
const SizedBox(height: 20),
|
||||
],
|
||||
)),
|
||||
],
|
||||
),
|
||||
),
|
||||
@ -67,3 +97,34 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
enum Weekday {
|
||||
Monday,
|
||||
Tuesday,
|
||||
Wednesday,
|
||||
Thursday,
|
||||
Friday,
|
||||
Saturday,
|
||||
Sunday
|
||||
}
|
||||
|
||||
extension WeekdayExtension on Weekday {
|
||||
String get name {
|
||||
switch (this) {
|
||||
case Weekday.Monday:
|
||||
return 'Понедельник';
|
||||
case Weekday.Tuesday:
|
||||
return 'Вторник';
|
||||
case Weekday.Wednesday:
|
||||
return 'Среда';
|
||||
case Weekday.Thursday:
|
||||
return 'Четверг';
|
||||
case Weekday.Friday:
|
||||
return 'Пятница';
|
||||
case Weekday.Saturday:
|
||||
return 'Суббота';
|
||||
case Weekday.Sunday:
|
||||
return 'Воскресенье';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
65
lib/weekManager.dart
Normal file
65
lib/weekManager.dart
Normal file
@ -0,0 +1,65 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
import 'main.dart';
|
||||
|
||||
class WeekManager {
|
||||
DateTime deadline;
|
||||
DateTime currentDate;
|
||||
Weekday _currentDay;
|
||||
|
||||
WeekManager(this.deadline)
|
||||
: currentDate = DateTime.now(),
|
||||
_currentDay = _getCurrentDayOfWeek(DateTime.now());
|
||||
|
||||
Weekday get currentDay => _currentDay;
|
||||
|
||||
static Weekday _getCurrentDayOfWeek(DateTime date) {
|
||||
switch (date.weekday) {
|
||||
case 1:
|
||||
return Weekday.Monday;
|
||||
case 2:
|
||||
return Weekday.Tuesday;
|
||||
case 3:
|
||||
return Weekday.Wednesday;
|
||||
case 4:
|
||||
return Weekday.Thursday;
|
||||
case 5:
|
||||
return Weekday.Friday;
|
||||
case 6:
|
||||
return Weekday.Saturday;
|
||||
case 7:
|
||||
return Weekday.Sunday;
|
||||
default:
|
||||
return Weekday.Monday;
|
||||
}
|
||||
}
|
||||
|
||||
int get daysUntilDeadline {
|
||||
return deadline.difference(currentDate).inDays;
|
||||
}
|
||||
|
||||
String get formattedDeadline {
|
||||
DateFormat formatter = DateFormat('dd-MM-yyyy');
|
||||
return formatter.format(deadline);
|
||||
}
|
||||
|
||||
String get formattedCurrentDate {
|
||||
DateFormat formatter = DateFormat('dd-MM-yyyy');
|
||||
return formatter.format(currentDate);
|
||||
}
|
||||
|
||||
void updateCurrentDay() {
|
||||
_currentDay = _getCurrentDayOfWeek(currentDate);
|
||||
}
|
||||
|
||||
void updateCurrentDate() {
|
||||
final random = Random();
|
||||
currentDate = DateTime.now().add(Duration(days: random.nextInt(365)));
|
||||
}
|
||||
|
||||
void incrementCurrentDate() {
|
||||
currentDate = currentDate.add(const Duration(days: 1));
|
||||
}
|
||||
}
|
@ -75,6 +75,14 @@ packages:
|
||||
description: flutter
|
||||
source: sdk
|
||||
version: "0.0.0"
|
||||
intl:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: intl
|
||||
sha256: "3bc132a9dbce73a7e4a21a17d06e1878839ffbf975568bc875c60537824b0c4d"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "0.18.1"
|
||||
leak_tracker:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
@ -30,6 +30,7 @@ environment:
|
||||
dependencies:
|
||||
flutter:
|
||||
sdk: flutter
|
||||
intl: ^0.18.0
|
||||
|
||||
|
||||
# The following adds the Cupertino Icons font to your application.
|
||||
|
Loading…
Reference in New Issue
Block a user