This commit is contained in:
GokaPek 2024-09-09 23:40:09 +04:00
parent 7e0f9d930a
commit 20fde8a65e
2 changed files with 115 additions and 17 deletions

View File

@ -1,5 +1,8 @@
import 'dart:math';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'year.dart';
void main() { void main() {
runApp(const MyApp()); runApp(const MyApp());
} }
@ -7,15 +10,15 @@ void main() {
class MyApp extends StatelessWidget { class MyApp extends StatelessWidget {
const MyApp({super.key}); const MyApp({super.key});
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return MaterialApp(
title: 'Flutter Demo', title: 'Petrushin demo',
theme: ThemeData( theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), colorScheme: ColorScheme.fromSeed(seedColor: Colors.red),
useMaterial3: true, useMaterial3: true,
), ),
home: const MyHomePage(title: 'Flutter Demo Home Page'), home: const MyHomePage(title: 'Petrushin Egor Alexandrovich'),
); );
} }
} }
@ -23,8 +26,6 @@ class MyApp extends StatelessWidget {
class MyHomePage extends StatefulWidget { class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title}); const MyHomePage({super.key, required this.title});
final String title; final String title;
@override @override
@ -33,29 +34,63 @@ class MyHomePage extends StatefulWidget {
class _MyHomePageState extends State<MyHomePage> { class _MyHomePageState extends State<MyHomePage> {
int _counter = 0; int _counter = 0;
int _year = 0;
int _month = 0;
List<Year> years = [];
@override
void initState() {
super.initState();
final Random random = Random();
for (int i = 0; i < 100; i++) {
int year = i;
String comment = generateRandomComment(random);
years.add(Year(comment, year: year));
}
}
void _incrementCounter() { void _incrementCounter() {
setState(() { setState(() {
_counter++; _counter++;
if (_month == 11) {
_month = 0;
_year++;
} else {
_month = _counter % 12;
}
}); });
() {
print('Current Month: ${DateUtils.getMonthName(_month)}');
print('Current Year: $_year');
}();
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary, backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title), title: Text(widget.title),
), ),
body: Center( body: Center(
child: Column( child: Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[ children: <Widget>[
const Text( const Text('Month:'),
'You have pushed the button this many times:', Text(
'${DateUtils.getMonthName(_month)}',
style: Theme.of(context).textTheme.headlineMedium,
),
const Text('Year:'),
Text(
'$_year',
style: Theme.of(context).textTheme.headlineMedium,
), ),
Text( Text(
'$_counter', '${years[_year].comment}',
style: Theme.of(context).textTheme.headlineMedium, style: Theme.of(context).textTheme.headlineMedium,
), ),
], ],
@ -64,7 +99,51 @@ class _MyHomePageState extends State<MyHomePage> {
floatingActionButton: FloatingActionButton( floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter, onPressed: _incrementCounter,
tooltip: 'Increment', tooltip: 'Increment',
child: const Icon(Icons.add), child: const Icon(Icons.access_time_filled),
), ); ),
);
} }
} }
// Enum
enum Month {
January,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November,
December,
}
// Расширение
extension DateUtils on Month {
static String getMonthName(int month) {
return Month.values[month].name;
}
static Future<String> getFutureMonthName(int month) async {
await Future.delayed(const Duration(seconds: 1));
return Month.values[month].name;
}
}
String generateRandomComment(Random random) {
const List<String> comments = [
'Interesting year',
'Historical year',
'Eventful year',
'Peaceful year',
'Challenging year',
'Successful year',
'Unforgettable year',
'Memorable year',
'Dynamic year',
'Stable year',
];
return comments[random.nextInt(comments.length)];
}

19
lib/year.dart Normal file
View File

@ -0,0 +1,19 @@
class Year {
final String comment;
final int year;
final bool isLeap;
Year(this.comment, {required this.year}) : isLeap = _isLeapYear(year);
static bool _isLeapYear(int year) {
if (year % 4 != 0) {
return false;
} else if (year % 100 != 0) {
return true;
} else if (year % 400 != 0) {
return false;
} else {
return true;
}
}
}