start
This commit is contained in:
parent
20fde8a65e
commit
d383b6149f
100
lib/main.dart
100
lib/main.dart
@ -33,39 +33,10 @@ class MyHomePage extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _MyHomePageState extends State<MyHomePage> {
|
||||
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() {
|
||||
setState(() {
|
||||
_counter++;
|
||||
|
||||
if (_month == 11) {
|
||||
_month = 0;
|
||||
_year++;
|
||||
} else {
|
||||
_month = _counter % 12;
|
||||
}
|
||||
});
|
||||
|
||||
() {
|
||||
print('Current Month: ${DateUtils.getMonthName(_month)}');
|
||||
print('Current Year: $_year');
|
||||
}();
|
||||
}
|
||||
|
||||
@override
|
||||
@ -75,75 +46,16 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
||||
title: Text(widget.title),
|
||||
),
|
||||
body: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
const Text('Month:'),
|
||||
Text(
|
||||
'${DateUtils.getMonthName(_month)}',
|
||||
style: Theme.of(context).textTheme.headlineMedium,
|
||||
),
|
||||
const Text('Year:'),
|
||||
Text(
|
||||
'$_year',
|
||||
style: Theme.of(context).textTheme.headlineMedium,
|
||||
),
|
||||
Text(
|
||||
'${years[_year].comment}',
|
||||
style: Theme.of(context).textTheme.headlineMedium,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: _incrementCounter,
|
||||
tooltip: 'Increment',
|
||||
child: const Icon(Icons.access_time_filled),
|
||||
),
|
||||
body: const MyWidget(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Enum
|
||||
enum Month {
|
||||
January,
|
||||
February,
|
||||
March,
|
||||
April,
|
||||
May,
|
||||
June,
|
||||
July,
|
||||
August,
|
||||
September,
|
||||
October,
|
||||
November,
|
||||
December,
|
||||
}
|
||||
class MyWidget extends StatelessWidget {
|
||||
const MyWidget({super.key});
|
||||
|
||||
// Расширение
|
||||
extension DateUtils on Month {
|
||||
static String getMonthName(int month) {
|
||||
return Month.values[month].name;
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const Placeholder();
|
||||
}
|
||||
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)];
|
||||
}
|
Loading…
Reference in New Issue
Block a user