вроде готовая лаба 4

This commit is contained in:
Полина Чубыкина 2024-10-02 16:37:43 +04:00
parent 170bdb62ab
commit 6e5023a91a
5 changed files with 308 additions and 180 deletions

View File

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

View File

@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:mobilki_lab1/presentation/home_page/home_page.dart';
void main() {
runApp(const MyApp());
@ -20,183 +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 = Colors.brown.shade300;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: _color,
title: Text(widget.title),
),
body: const MyWidget(),
);
}
}
class _CardData {
final String text;
final String descriptionText;
final IconData icon;
final String? imageUrl;
_CardData(
this.text, {
required this.descriptionText,
this.icon = Icons.abc,
this.imageUrl,
});
}
class MyWidget extends StatelessWidget {
const MyWidget({super.key});
@override
Widget build(BuildContext context) {
final data = [
_CardData(
'кот',
descriptionText: 'глупенький',
icon: Icons.favorite,
imageUrl:
'https://i.pinimg.com/564x/98/74/75/98747559040c03402a6d9ad4e47152c6.jpg',
),
_CardData(
'сырный кот',
descriptionText: 'он не виноват',
icon: Icons.favorite,
imageUrl:
'https://i.pinimg.com/564x/e7/c5/59/e7c559e449cd0bb5370d7740b57daa1f.jpg',
),
_CardData(
'злой кот',
descriptionText: 'я бы ему не верила',
icon: Icons.favorite,
imageUrl:
'https://i.pinimg.com/736x/27/cf/5e/27cf5e3abe1795ce04095941902c61ca.jpg',
),
_CardData(
'умный кот',
descriptionText: 'он только делает вид что умный',
icon: Icons.favorite,
imageUrl:
'https://i.pinimg.com/736x/e9/24/80/e924801430b813eee2635b40fe6e11d6.jpg',
),
_CardData(
'кот в шляпе',
descriptionText: '我喜欢橘子!',
icon: Icons.favorite,
imageUrl:
'https://i.pinimg.com/564x/3e/ed/11/3eed1103c548ed7c46a1e37a2695f53a.jpg',
),
_CardData(
'котенок',
descriptionText: 'я его очень понимаю',
icon: Icons.favorite,
imageUrl:
'https://i.pinimg.com/736x/38/9f/e6/389fe61133f036fc980a6b2e7abb2557.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 String descriptionText;
final IconData icon;
final String? imageUrl;
const _Card(
this.text, {
this.icon = Icons.abc,
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(
margin: const EdgeInsets.all(6),
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: Colors.white70,
borderRadius: BorderRadius.circular(20),
border: Border.all(color: Colors.grey.shade200),
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(10),
child: SizedBox(
height: 140,
width: 140,
child: Image.network(
imageUrl ?? '',
fit: BoxFit.cover,
errorBuilder: (_, __, ___) => const Placeholder(),
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.only(left: 8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
text,
style: Theme.of(context).textTheme.headlineLarge,
),
Text(
descriptionText,
style: Theme.of(context).textTheme.bodyLarge,
),
],
),
),
),
Padding(
padding: const EdgeInsets.only(left: 8.0, top: 10.0),
child: Icon(icon),
),
],
),
);
}
}

View File

@ -0,0 +1,37 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mobilki_lab1/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,137 @@
part of 'home_page.dart';
typedef OnLikeCallback = void Function(String title, bool isLiked)?;
class _Card extends StatefulWidget {
final String text;
final String descriptionText;
final IconData icon;
final String? imageUrl;
final OnLikeCallback onLike;
final VoidCallback? onTap;
const _Card(
this.text, {
this.icon = Icons.catching_pokemon,
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(16),
constraints: const BoxConstraints(minHeight: 140),
decoration: BoxDecoration(
color: Colors.white70,
borderRadius: BorderRadius.circular(20),
border: Border.all(color: Colors.grey.shade200),
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: 160,
child: Image.network(
widget.imageUrl ?? '',
fit: BoxFit.cover,
errorBuilder: (_, __, ___) => const Placeholder(),
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.only(left: 16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
widget.text,
style: Theme.of(context).textTheme.headlineLarge,
),
Text(
widget.descriptionText,
style: Theme.of(context).textTheme.bodyLarge,
),
],
),
),
),
Align(
alignment: Alignment.bottomRight,
child: Padding(
padding: const EdgeInsets.only(
left: 8.0,
right: 16.0,
bottom: 16.0,
),
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.redAccent,
key: ValueKey<int>(0),
)
: const Icon(
Icons.favorite_border,
key: ValueKey<int>(1),
),
),
),
),
),
],
),
),
),
);
}
}

View File

@ -0,0 +1,118 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mobilki_lab1/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 = Colors.brown.shade300;
@override
void initState() {
super.initState();
}
@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: 'глупенький',
imageUrl:
'https://i.pinimg.com/564x/98/74/75/98747559040c03402a6d9ad4e47152c6.jpg',
),
CardData(
'сырный кот',
descriptionText: 'он не виноват',
imageUrl:
'https://i.pinimg.com/564x/e7/c5/59/e7c559e449cd0bb5370d7740b57daa1f.jpg',
),
CardData(
'злой кот',
descriptionText: 'я бы ему не верила',
imageUrl:
'https://i.pinimg.com/736x/27/cf/5e/27cf5e3abe1795ce04095941902c61ca.jpg',
),
CardData(
'умный кот',
descriptionText: 'он только делает вид что умный',
imageUrl:
'https://i.pinimg.com/736x/e9/24/80/e924801430b813eee2635b40fe6e11d6.jpg',
),
CardData(
'кот в шляпе',
descriptionText: '我喜欢橘子!',
imageUrl:
'https://i.pinimg.com/564x/3e/ed/11/3eed1103c548ed7c46a1e37a2695f53a.jpg',
),
CardData(
'котенок',
descriptionText: 'я его очень понимаю',
imageUrl:
'https://i.pinimg.com/736x/38/9f/e6/389fe61133f036fc980a6b2e7abb2557.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: Colors.brown.shade400,
duration: const Duration(seconds: 1),
));
});
}
}