This commit is contained in:
Вячеслав Иванов 2024-09-10 23:39:25 +04:00
parent 35a3ba708e
commit b3f16e9c47
4 changed files with 146 additions and 11 deletions

View File

@ -1,4 +1,5 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'weekManager.dart';
void main() { void main() {
runApp(const MyApp()); runApp(const MyApp());
@ -12,10 +13,10 @@ class MyApp extends StatelessWidget {
return MaterialApp( return MaterialApp(
title: 'Flutter Demo', title: 'Flutter Demo',
theme: ThemeData( theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
useMaterial3: true, 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> { 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() { void _incrementCounter() {
setState(() { setState(() {
_counter++; for (var weekManager in _weekManagers) {
weekManager.incrementCurrentDate();
weekManager.updateCurrentDay();
}
}); });
} }
@ -49,13 +74,18 @@ class _MyHomePageState extends State<MyHomePage> {
child: Column( child: Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[ children: <Widget>[
const Text( Text('Дедлайнер',
'You have pushed the button this many times:', style: Theme.of(context).textTheme.headlineMedium),
), const SizedBox(height: 20),
Text( ..._weekManagers.map((weekManager) => Column(
'$_counter', children: [
style: Theme.of(context).textTheme.headlineMedium, 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
View 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));
}
}

View File

@ -75,6 +75,14 @@ packages:
description: flutter description: flutter
source: sdk source: sdk
version: "0.0.0" 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: leak_tracker:
dependency: transitive dependency: transitive
description: description:

View File

@ -30,6 +30,7 @@ environment:
dependencies: dependencies:
flutter: flutter:
sdk: flutter sdk: flutter
intl: ^0.18.0
# The following adds the Cupertino Icons font to your application. # The following adds the Cupertino Icons font to your application.