This commit is contained in:
Вячеслав Иванов 2024-09-18 02:09:56 +04:00
parent 4260c7e1a5
commit b56181b1c4
4 changed files with 126 additions and 133 deletions

BIN
images/pizza.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 669 KiB

View File

@ -1,6 +1,4 @@
import 'dart:math';
import 'package:flutter/material.dart';
import 'weekManager.dart';
void main() {
runApp(const MyApp());
@ -13,11 +11,12 @@ class MyApp extends StatelessWidget {
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
colorScheme: ColorScheme.fromSeed(seedColor: Colors.orangeAccent),
useMaterial3: true,
),
home: const MyHomePage(title: 'Deadliner'),
home: const MyHomePage(title: 'Cards'),
);
}
}
@ -32,92 +31,140 @@ class MyHomePage extends StatefulWidget {
}
class _MyHomePageState extends State<MyHomePage> {
late List<WeekManager> _weekManagers;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Padding(
padding: const EdgeInsets.all(20),
child: ListView.separated(
itemBuilder: (context, index) => GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailPage(
name: "Pizza $index",
price: 150.0,
),
),
);
},
child: CardWidget(name: "Pizza $index", price: 150.0),
),
separatorBuilder: (context, index) => const SizedBox(height: 20),
itemCount: 5,
)));
}
}
class CardWidget extends StatefulWidget {
const CardWidget({super.key, required this.name, required this.price});
final String name;
final double price;
@override
void initState() {
super.initState();
final random = Random();
_weekManagers = List.generate(5, (index) {
DateTime deadline = DateTime.now().add(Duration(days: random.nextInt(365)));
DateTime currentDate = DateTime.now().add(Duration(days: random.nextInt(365)));
return WeekManager(deadline, currentDate);
});
}
State<StatefulWidget> createState() => _CardWidgetState();
}
Future<void> _incrementCounter() async {
for (var weekManager in _weekManagers) {
await Future.delayed(const Duration(milliseconds: 500), () {
setState(() {
weekManager.incrementCurrentDate();
weekManager.updateCurrentDay();
});
});
}
class _CardWidgetState extends State<CardWidget> {
bool _isFavourite = false;
void toggleIsFavourite() {
setState(() {
_isFavourite = !_isFavourite;
final snackBar = SnackBar(
duration: const Duration(seconds: 1),
content: Text(
_isFavourite ? 'Added to favorites' : 'Removed from favorites'),
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
return Container(
padding: const EdgeInsets.all(4),
decoration: BoxDecoration(
border: Border.all(color: Colors.black12, width: 2),
borderRadius: BorderRadius.circular(20),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Дедлайнер',
style: Theme.of(context).textTheme.headlineMedium),
const SizedBox(height: 20),
..._weekManagers.map((weekManager) => Column(
children: [
Text('Текущий день недели: ${weekManager.currentDay.name}'),
Text('Текущая дата: ${weekManager.formatDate(weekManager.currentDate)}'),
Text('Дней до дедлайна: ${weekManager.daysUntilDeadline}'),
Text('Дата дедлайна: ${weekManager.formatDate(weekManager.deadline)}'),
const SizedBox(height: 20),
],
)),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
child: Row(
children: [
// Image
Expanded(
child: Image.asset(
"images/pizza.png",
fit: BoxFit.cover,
),
),
// Content
Expanded(
flex: 2,
child: Padding(
padding: const EdgeInsets.only(left: 10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(widget.name, style: const TextStyle(fontSize: 18)),
Text(
"${widget.price} Руб",
style: const TextStyle(fontSize: 17, color: Colors.orange),
),
],
),
),
),
// Like Button
IconButton(
icon: Icon(_isFavourite ? Icons.favorite : Icons.favorite_border),
color: Colors.red,
onPressed: toggleIsFavourite,
),
],
),
);
}
}
enum Weekday {
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
class DetailPage extends StatelessWidget {
final String name;
final double price;
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 'Воскресенье';
}
const DetailPage({super.key, required this.name, required this.price});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(name),
),
body: Padding(
padding: const EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Center(
child: Image.asset(
"images/pizza.png",
),
),
const SizedBox(height: 20),
Text(name,
style:
const TextStyle(fontSize: 24, fontWeight: FontWeight.bold)),
const SizedBox(height: 10),
Text("$price Руб",
style: const TextStyle(fontSize: 20, color: Colors.orange)),
// Add more details here as needed
],
),
),
);
}
}

View File

@ -1,53 +0,0 @@
import 'package:intl/intl.dart';
import 'main.dart';
class WeekManager {
final DateTime _deadline;
DateTime _currentDate;
Weekday _currentDay;
WeekManager(this._deadline, this._currentDate)
: _currentDay = _getCurrentDayOfWeek(_currentDate);
DateTime get deadline => _deadline;
DateTime get currentDate => _currentDate;
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 formatDate(DateTime date) {
DateFormat formatter = DateFormat('dd-MM-yyyy');
return formatter.format(date);
}
void incrementCurrentDate() {
_currentDate = _currentDate.add(const Duration(days: 1));
}
void updateCurrentDay() {
_currentDay = _getCurrentDayOfWeek(_currentDate);
}
}

View File

@ -60,10 +60,9 @@ flutter:
uses-material-design: true
# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
assets:
- images/pizza.png
# - images/a_dot_ham.jpeg
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/to/resolution-aware-images