LabWork4
This commit is contained in:
17
lib/domain/models/card.dart
Normal file
17
lib/domain/models/card.dart
Normal file
@@ -0,0 +1,17 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class CardData {
|
||||
final String name;
|
||||
final String description;
|
||||
final String? imageUrl;
|
||||
final IconData icon;
|
||||
final double price;
|
||||
|
||||
CardData(
|
||||
this.name, {
|
||||
required this.description,
|
||||
this.imageUrl,
|
||||
this.icon = Icons.ac_unit_outlined,
|
||||
required this.price,
|
||||
});
|
||||
}
|
||||
168
lib/main.dart
168
lib/main.dart
@@ -1,4 +1,5 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_project/presentation/home_page/home_page.dart';
|
||||
|
||||
void main() {
|
||||
runApp(const MyApp());
|
||||
@@ -18,170 +19,3 @@ class MyApp extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class MyHomePage extends StatefulWidget {
|
||||
const MyHomePage({super.key, required this.title});
|
||||
|
||||
final String title;
|
||||
|
||||
@override
|
||||
State<MyHomePage> createState() => _MyHomePageState();
|
||||
}
|
||||
|
||||
class _MyHomePageState extends State<MyHomePage> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
||||
title: Text(widget.title),
|
||||
),
|
||||
body: const MyWidget(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _CardData {
|
||||
final String name;
|
||||
final String description;
|
||||
final String? imageUrl;
|
||||
final IconData icon;
|
||||
|
||||
_CardData(this.name, {required this.description, this.imageUrl, this.icon = Icons.ac_unit_outlined,});
|
||||
}
|
||||
|
||||
class MyWidget extends StatelessWidget {
|
||||
const MyWidget({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final data = [
|
||||
_CardData(
|
||||
'Catrice',
|
||||
description: 'Лак для ногтей',
|
||||
imageUrl:
|
||||
'https://cdn1.ozone.ru/s3/multimedia-1-r/7082406639.jpg',
|
||||
icon: Icons.shopping_cart,
|
||||
),
|
||||
_CardData(
|
||||
'Darling',
|
||||
description: 'Тушь для ресниц',
|
||||
imageUrl:
|
||||
'https://avatars.mds.yandex.net/get-vertis-journal/4469561/7_Byuti_nabory.png_1757063983772/845x845',
|
||||
icon: Icons.shopping_cart,
|
||||
),
|
||||
_CardData(
|
||||
'Stelary',
|
||||
description: 'Карандаш для бровей',
|
||||
imageUrl:
|
||||
'https://ir.ozone.ru/s3/multimedia-v/c1000/6477525451.jpg',
|
||||
icon: Icons.shopping_cart,
|
||||
),
|
||||
_CardData(
|
||||
'Luxvisage',
|
||||
description: 'Блеск для губ',
|
||||
imageUrl:
|
||||
'https://ir.ozone.ru/s3/multimedia-1-2/7060667942.jpg',
|
||||
icon: Icons.shopping_cart,
|
||||
),
|
||||
_CardData(
|
||||
'ArtVisage',
|
||||
description: 'Гель для бровей',
|
||||
imageUrl:
|
||||
'https://avatars.mds.yandex.net/get-mpic/4947485/2a00000194da1f4326d6fcab70498955305b/orig',
|
||||
icon: Icons.shopping_cart,
|
||||
),
|
||||
_CardData(
|
||||
'Dove',
|
||||
description: 'Гель для душа',
|
||||
imageUrl:
|
||||
'https://avatars.mds.yandex.net/get-mpic/12365257/2a0000018dfa83803ca84397ed724feb489c/orig',
|
||||
icon: Icons.shopping_cart,
|
||||
),
|
||||
_CardData(
|
||||
'ManlyPro',
|
||||
description: 'Тональная основа',
|
||||
imageUrl:
|
||||
'https://s5-frame.ozstatic.by/1000/228/378/101/101378228_0.jpg',
|
||||
icon: Icons.shopping_cart,
|
||||
),
|
||||
];
|
||||
|
||||
return SingleChildScrollView(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: data.map((e) => _Card.fromData(e)).toList(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _Card extends StatelessWidget {
|
||||
final String name;
|
||||
final String description;
|
||||
final String? imageUrl;
|
||||
final IconData icon;
|
||||
|
||||
const _Card(this.name, {required this.description, this.imageUrl, this.icon = Icons.ac_unit_outlined,});
|
||||
|
||||
factory _Card.fromData(_CardData data) =>
|
||||
_Card(data.name, description: data.description, imageUrl: data.imageUrl, icon: data.icon,);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
margin: const EdgeInsets.only(bottom: 12),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
border: Border.all(color: Colors.deepPurple),
|
||||
),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
ClipRRect(
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
child: SizedBox(
|
||||
height: 120,
|
||||
width: 100,
|
||||
child: Image.network(
|
||||
imageUrl ?? '',
|
||||
fit: BoxFit.cover,
|
||||
errorBuilder: (_, __, ___) => const Placeholder(),
|
||||
),
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(12),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
name,
|
||||
style: Theme.of(context).textTheme.headlineLarge,
|
||||
),
|
||||
|
||||
Text(
|
||||
description,
|
||||
style: Theme.of(context).textTheme.bodyLarge,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Icon(icon),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
32
lib/presentation/details_page/details_page.dart
Normal file
32
lib/presentation/details_page/details_page.dart
Normal file
@@ -0,0 +1,32 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_project/domain/models/card.dart';
|
||||
|
||||
class DetailsPage extends StatelessWidget {
|
||||
final CardData data;
|
||||
|
||||
const DetailsPage(this.data, {super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(),
|
||||
body: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: 16.0),
|
||||
child: Image.network(data.imageUrl ?? ''),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: 4.0),
|
||||
child: Text(
|
||||
data.name,
|
||||
style: Theme.of(context).textTheme.headlineLarge,
|
||||
),
|
||||
),
|
||||
Text(data.description, style: Theme.of(context).textTheme.bodyLarge),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
163
lib/presentation/home_page/card.dart
Normal file
163
lib/presentation/home_page/card.dart
Normal file
@@ -0,0 +1,163 @@
|
||||
part of 'home_page.dart';
|
||||
|
||||
typedef OnLikeCallBack = void Function(String title, bool isLiked)?;
|
||||
|
||||
class _Card extends StatefulWidget {
|
||||
final String name;
|
||||
final String description;
|
||||
final String? imageUrl;
|
||||
final IconData icon;
|
||||
final double price;
|
||||
final OnLikeCallBack onLike;
|
||||
final VoidCallback? onTap;
|
||||
|
||||
const _Card(
|
||||
this.name, {
|
||||
required this.description,
|
||||
this.imageUrl,
|
||||
this.icon = Icons.ac_unit_outlined,
|
||||
required this.price,
|
||||
this.onLike,
|
||||
this.onTap,
|
||||
});
|
||||
|
||||
factory _Card.fromData(
|
||||
CardData data, {
|
||||
OnLikeCallBack onLike,
|
||||
VoidCallback? onTap,
|
||||
}) => _Card(
|
||||
data.name,
|
||||
description: data.description,
|
||||
imageUrl: data.imageUrl,
|
||||
icon: data.icon,
|
||||
price: data.price,
|
||||
onLike: onLike,
|
||||
onTap: onTap,
|
||||
);
|
||||
|
||||
@override
|
||||
State<_Card> createState() => _CardState();
|
||||
}
|
||||
|
||||
class _CardState extends State<_Card> {
|
||||
bool isLiked = false;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return GestureDetector(
|
||||
onTap: widget.onTap,
|
||||
child: Container(
|
||||
margin: const EdgeInsets.all(10),
|
||||
constraints: const BoxConstraints(minHeight: 150),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.deepPurple.shade50,
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
border: Border.all(color: Colors.deepPurple),
|
||||
),
|
||||
child: IntrinsicHeight(
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
ClipRRect(
|
||||
borderRadius: const BorderRadius.only(
|
||||
topLeft: Radius.circular(10),
|
||||
bottomLeft: Radius.circular(10),
|
||||
),
|
||||
child: SizedBox(
|
||||
height: double.infinity,
|
||||
width: 100,
|
||||
child: Stack(
|
||||
children: [
|
||||
Positioned.fill(
|
||||
child: Image.network(
|
||||
widget.imageUrl ?? '',
|
||||
fit: BoxFit.cover,
|
||||
errorBuilder: (_, __, ___) => const Placeholder(),
|
||||
),
|
||||
),
|
||||
Align(
|
||||
alignment: Alignment.bottomLeft,
|
||||
child: Container(
|
||||
decoration: const BoxDecoration(
|
||||
color: Colors.deepPurple,
|
||||
borderRadius: BorderRadius.only(
|
||||
topRight: Radius.circular(20),
|
||||
),
|
||||
),
|
||||
padding: const EdgeInsets.fromLTRB(8, 2, 8, 2),
|
||||
child: Text(
|
||||
'Cкидка 5%',
|
||||
style: Theme.of(context).textTheme.bodyMedium
|
||||
?.copyWith(color: Colors.white),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(12),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
widget.name,
|
||||
style: Theme.of(context).textTheme.headlineLarge,
|
||||
),
|
||||
Text(
|
||||
widget.description,
|
||||
style: Theme.of(context).textTheme.bodyLarge,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
Column(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 16.0, right: 8),
|
||||
child: Icon(widget.icon),
|
||||
),
|
||||
Align(
|
||||
alignment: Alignment.bottomRight,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 8,
|
||||
right: 16,
|
||||
bottom: 16,
|
||||
),
|
||||
child: GestureDetector(
|
||||
onTap: () {
|
||||
setState(() {
|
||||
isLiked = !isLiked;
|
||||
});
|
||||
widget.onLike?.call(widget.name, isLiked);
|
||||
},
|
||||
child: AnimatedSwitcher(
|
||||
duration: const Duration(milliseconds: 200),
|
||||
child: isLiked
|
||||
? const Icon(
|
||||
Icons.favorite,
|
||||
color: Colors.redAccent,
|
||||
key: ValueKey<int>(0),
|
||||
)
|
||||
: const Icon(
|
||||
Icons.favorite_border,
|
||||
key: ValueKey<int>(1),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
148
lib/presentation/home_page/home_page.dart
Normal file
148
lib/presentation/home_page/home_page.dart
Normal file
@@ -0,0 +1,148 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_project/domain/models/card.dart';
|
||||
import 'package:flutter_project/presentation/details_page/details_page.dart';
|
||||
|
||||
part 'card.dart';
|
||||
|
||||
class MyHomePage extends StatefulWidget {
|
||||
const MyHomePage({super.key, required this.title});
|
||||
|
||||
final String title;
|
||||
|
||||
@override
|
||||
State<MyHomePage> createState() => _MyHomePageState();
|
||||
}
|
||||
|
||||
class _MyHomePageState extends State<MyHomePage> {
|
||||
@override
|
||||
void initState() {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
'Приветствуем!',
|
||||
style: Theme.of(context).textTheme.bodyLarge,
|
||||
),
|
||||
backgroundColor: Colors.purple,
|
||||
duration: const Duration(seconds: 1),
|
||||
),
|
||||
);
|
||||
});
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
||||
title: Text(widget.title),
|
||||
),
|
||||
body: const Body(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class Body extends StatelessWidget {
|
||||
const Body({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final data = [
|
||||
CardData(
|
||||
'Catrice',
|
||||
description: 'Лак для ногтей',
|
||||
imageUrl: 'https://cdn1.ozone.ru/s3/multimedia-1-r/7082406639.jpg',
|
||||
icon: Icons.shopping_cart,
|
||||
price: 456,
|
||||
),
|
||||
CardData(
|
||||
'Darling',
|
||||
description: 'Тушь для ресниц',
|
||||
imageUrl:
|
||||
'https://avatars.mds.yandex.net/get-vertis-journal/4469561/7_Byuti_nabory.png_1757063983772/845x845',
|
||||
icon: Icons.shopping_cart,
|
||||
price: 1922,
|
||||
),
|
||||
CardData(
|
||||
'Stelary',
|
||||
description: 'Карандаш для бровей',
|
||||
imageUrl: 'https://ir.ozone.ru/s3/multimedia-v/c1000/6477525451.jpg',
|
||||
icon: Icons.shopping_cart,
|
||||
price: 1331,
|
||||
),
|
||||
CardData(
|
||||
'Luxvisage',
|
||||
description: 'Блеск для губ',
|
||||
imageUrl: 'https://ir.ozone.ru/s3/multimedia-1-2/7060667942.jpg',
|
||||
icon: Icons.shopping_cart,
|
||||
price: 312,
|
||||
),
|
||||
CardData(
|
||||
'ArtVisage',
|
||||
description: 'Гель для бровей',
|
||||
imageUrl:
|
||||
'https://avatars.mds.yandex.net/get-mpic/4947485/2a00000194da1f4326d6fcab70498955305b/orig',
|
||||
icon: Icons.shopping_cart,
|
||||
price: 200,
|
||||
),
|
||||
CardData(
|
||||
'Dove',
|
||||
description: 'Гель для душа',
|
||||
imageUrl:
|
||||
'https://avatars.mds.yandex.net/get-mpic/12365257/2a0000018dfa83803ca84397ed724feb489c/orig',
|
||||
icon: Icons.shopping_cart,
|
||||
price: 289,
|
||||
),
|
||||
CardData(
|
||||
'ManlyPro',
|
||||
description: 'Тональная основа',
|
||||
imageUrl:
|
||||
'https://s5-frame.ozstatic.by/1000/228/378/101/101378228_0.jpg',
|
||||
icon: Icons.shopping_cart,
|
||||
price: 4200,
|
||||
),
|
||||
];
|
||||
|
||||
return Center(
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: data
|
||||
.map(
|
||||
(data) => _Card.fromData(
|
||||
data,
|
||||
onLike: (title, isLiked) =>
|
||||
_showSnackBar(context, title, isLiked),
|
||||
onTap: () => _navToDetails(context, data),
|
||||
),
|
||||
)
|
||||
.toList(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _navToDetails(BuildContext context, CardData data) {
|
||||
Navigator.push(
|
||||
context,
|
||||
CupertinoPageRoute(builder: (context) => DetailsPage(data)),
|
||||
);
|
||||
}
|
||||
|
||||
void _showSnackBar(BuildContext context, String title, bool isLiked) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
'$title ${isLiked ? 'добавлен в избранное' : 'убран из избранного'}',
|
||||
style: Theme.of(context).textTheme.bodyLarge,
|
||||
),
|
||||
backgroundColor: Colors.deepPurple.shade100,
|
||||
duration: const Duration(seconds: 2),
|
||||
),
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user