лабораторная 4 пажаласта!

This commit is contained in:
a.puchkina 2024-10-02 21:32:18 +04:00
parent 7701ec2236
commit cd9992c401
5 changed files with 302 additions and 169 deletions

View File

@ -0,0 +1,15 @@
import 'package:flutter/material.dart';
class CardData {
final String text;
final IconData icon;
final String descriptionText;
final String? imageUrl;
CardData(
this.text, {
this.icon = Icons.bookmark_border,
required this.descriptionText,
this.imageUrl,
});
}

View File

@ -1,3 +1,4 @@
import 'package:app/presentation/home_page/home_page.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
void main() { void main() {
@ -20,172 +21,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> {
final Color _color = const Color(0xFF7BA05B);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: _color,
title: Text(widget.title),
),
body: const MyWidget(),
);
}
}
class _CardData {
final String text;
final IconData icon;
final String descriptionText;
final String? imageUrl;
_CardData(this.text, {
this.icon = Icons.bookmark_border,
required this.descriptionText,
this.imageUrl,
});
}
class MyWidget extends StatelessWidget {
const MyWidget({super.key});
@override
Widget build(BuildContext context) {
final data = [
_CardData(
'Бенто-торт "Сумерки"',
descriptionText: 'И давно тебе 17?',
imageUrl: 'https://i.pinimg.com/736x/ef/5a/80/ef5a80844de4afb19750d67f1f5be04d.jpg',
),
_CardData(
'Бенто-торт "Material Girl"',
descriptionText: 'Главное какой fabric, какой material',
imageUrl: 'https://i.pinimg.com/736x/f5/88/0c/f5880cc5b09e602c1849d4093d17eb8e.jpg'
),
_CardData(
'Бенто-торт "Шрек"',
descriptionText: 'Somebody once told me the world is gonna roll me',
imageUrl: 'https://i.pinimg.com/736x/a9/0d/fb/a90dfb9dfe3b4f4e1c5ce3cb99847a1e.jpg',
),
_CardData(
'Бенто-торт "Дурак"',
descriptionText: 'Сам дурак',
imageUrl: 'https://i.pinimg.com/564x/11/87/8a/11878a2f6a0c5d26e38ce68cbbe21337.jpg',
),
_CardData(
'Бенто-торт "Совунья"',
descriptionText: 'Я просто смешариков люблю',
imageUrl: 'https://i.pinimg.com/564x/85/61/d2/8561d2b601fe9eb72c4e2031bbb6ba24.jpg',
),
_CardData(
'Бенто-торт "В смысле 28?"',
descriptionText: 'Напишем любой возраст (или срок)',
imageUrl: 'https://i.pinimg.com/564x/ae/76/0a/ae760af69d9a0c12938753a372112ee4.jpg',
),
];
return Center(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: data.map((e) => _Card.fromData(e)).toList()),
));
}
}
class _Card extends StatelessWidget {
final String text;
final IconData icon;
final String descriptionText;
final String? imageUrl;
const _Card(this.text, {
this.icon = Icons.bookmark_border,
required this.descriptionText,
this.imageUrl,
});
factory _Card.fromData(_CardData data) =>
_Card(
data.text,
descriptionText: data.descriptionText,
icon: data.icon,
imageUrl: data.imageUrl,
);
@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.all(16),
margin: const EdgeInsets.all(14),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(20),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(.5),
spreadRadius: 4,
offset: const Offset(0, 5),
blurRadius: 8,
)
]
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ClipRRect(
borderRadius: BorderRadius.circular(20),
child: SizedBox(
width: 120,
height: 120,
child: Image.network(
imageUrl ?? '',
fit: BoxFit.cover,
errorBuilder: (_, __, ___) => const Placeholder(),
)),
),
Expanded(
child: Padding(
padding: const EdgeInsets.only(left: 15.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
text,
style: Theme
.of(context)
.textTheme
.headlineSmall,
),
Text(
descriptionText,
style: Theme
.of(context)
.textTheme
.bodyLarge,
)
],
),
),
),
Padding(
padding: const EdgeInsets.only(left: 8.0),
child: Icon(icon),
),
],
),
);
}
}

View File

@ -0,0 +1,37 @@
import 'package:flutter/material.dart';
import '../../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.text,
style: Theme.of(context).textTheme.headlineLarge,
),
),
Text(
data.descriptionText,
style: Theme.of(context).textTheme.bodyLarge,
)
],
));
}
}

View File

@ -0,0 +1,134 @@
part of 'home_page.dart';
typedef OnLikeCallBack = void Function(String title, bool isLiked)?;
class _Card extends StatefulWidget {
final String text;
final IconData icon;
final String descriptionText;
final String? imageUrl;
final OnLikeCallBack onLike;
final VoidCallback? onTap;
const _Card(
this.text, {
this.icon = Icons.bookmark_border,
required this.descriptionText,
this.imageUrl,
this.onLike,
this.onTap,
});
factory _Card.fromData(
CardData data, {
OnLikeCallBack onLike,
VoidCallback? onTap,
}) =>
_Card(
data.text,
descriptionText: data.descriptionText,
icon: data.icon,
imageUrl: data.imageUrl,
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(14),
constraints: const BoxConstraints(minHeight: 140),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(20),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(.5),
spreadRadius: 4,
offset: const Offset(0, 5),
blurRadius: 8,
)
]),
child: IntrinsicHeight(
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ClipRRect(
borderRadius: const BorderRadius.only(
bottomLeft: Radius.circular(20),
topLeft: Radius.circular(20),
),
child: SizedBox(
height: double.infinity,
width: 120,
child: Image.network(
widget.imageUrl ?? '',
fit: BoxFit.cover,
errorBuilder: (_, __, ___) => const Placeholder(),
)),
),
Expanded(
child: Padding(
padding: const EdgeInsets.only(left: 15.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
widget.text,
style: Theme.of(context).textTheme.headlineSmall,
),
Text(
widget.descriptionText,
style: Theme.of(context).textTheme.bodyLarge,
)
],
),
),
),
Align(
alignment: Alignment.bottomLeft,
child: Padding(
padding: const EdgeInsets.only(
left: 8.0,
right: 16,
bottom: 16,
),
child: GestureDetector(
onTap: () {
setState(() {
isLiked = !isLiked;
});
widget.onLike?.call(widget.text, isLiked);
},
child: AnimatedSwitcher(
duration: const Duration(milliseconds: 300),
child: isLiked
? const Icon(
Icons.favorite,
color: Colors.pink,
key: ValueKey<int>(0),
)
: const Icon(
Icons.favorite_border,
key: ValueKey<int>(1),
),
),
),
),
),
],
),
),
),
);
}
}

View File

@ -0,0 +1,115 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../domain/models/card.dart';
import '../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> {
final Color _color = const Color(0xFF7BA05B);
@override
void initState() {
// todo
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: _color,
title: Text(widget.title),
),
body: const Body(),
);
}
}
class Body extends StatelessWidget {
const Body({super.key});
@override
Widget build(BuildContext context) {
final data = [
CardData(
'Бенто-торт "Сумерки"',
descriptionText: 'И давно тебе 17?',
imageUrl:
'https://i.pinimg.com/736x/ef/5a/80/ef5a80844de4afb19750d67f1f5be04d.jpg',
),
CardData('Бенто-торт "Material Girl"',
descriptionText: 'Главное какой fabric, какой material',
imageUrl:
'https://i.pinimg.com/736x/f5/88/0c/f5880cc5b09e602c1849d4093d17eb8e.jpg'),
CardData(
'Бенто-торт "Шрек"',
descriptionText: 'Somebody once told me the world is gonna roll me',
imageUrl:
'https://i.pinimg.com/736x/a9/0d/fb/a90dfb9dfe3b4f4e1c5ce3cb99847a1e.jpg',
),
CardData(
'Бенто-торт "Дурак"',
descriptionText: 'Сам дурак',
imageUrl:
'https://i.pinimg.com/564x/11/87/8a/11878a2f6a0c5d26e38ce68cbbe21337.jpg',
),
CardData(
'Бенто-торт "Совунья"',
descriptionText: 'Я просто смешариков люблю',
imageUrl:
'https://i.pinimg.com/564x/85/61/d2/8561d2b601fe9eb72c4e2031bbb6ba24.jpg',
),
CardData(
'Бенто-торт "В смысле 28?"',
descriptionText: 'Напишем любой возраст (или срок)',
imageUrl:
'https://i.pinimg.com/564x/ae/76/0a/ae760af69d9a0c12938753a372112ee4.jpg',
),
];
return Center(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: data.map((data) {
return _Card.fromData(
data,
onLike: (String title, bool 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(
'Вы ${isLiked ? 'добавили в избранное' : 'убрали из избраных'} $title',
style: Theme.of(context).textTheme.bodyLarge,
),
backgroundColor: Color(0xFF7BA05B),
duration: const Duration(seconds: 1),
));
});
}
}