lab4 НЕКРАСИВАЯ

This commit is contained in:
antoc0der 2024-09-23 20:37:45 +04:00
parent 2532f84f4b
commit e585d8bdf2
5 changed files with 307 additions and 135 deletions

View File

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

View File

@ -1,4 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:pmu/presentation/home_page/home_page.dart';
void main() { void main() {
runApp(const MyApp()); runApp(const MyApp());
@ -12,149 +14,16 @@ class MyApp extends StatelessWidget {
return MaterialApp( return MaterialApp(
title: 'Flutter Demo', title: 'Flutter Demo',
theme: ThemeData( theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), colorScheme: ColorScheme.fromSeed(seedColor: Colors.lightBlueAccent),
useMaterial3: true, useMaterial3: true,
), ),
home: const MyHomePage(title: 'Nasyrov Arthur PIbd-33'), home: const MyHomePage(title: 'New.sru'),
); );
} }
} }
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.deepPurple;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: _color,
title: Text(widget.title),
),
body: const MyWidget(),
);
}
}
class MyWidget extends StatelessWidget {
const MyWidget({super.key});
@override
Widget build(BuildContext context) {
final data = [
_CardData(text: 'text', descText: 'descText', imageUrl: 'https://sun9-7.userapi.com/impg/5Lbmz05PKIjm3vWMa91N-nD2Zrx1IYHxhLSiKg/N9rlBPsRwPM.jpg?size=872x703&quality=96&sign=8e317544d6f5b07c225172ab73f80a81&type=album'),
_CardData(
text: 'text1', descText: 'descText1', icon: Icons.account_balance, imageUrl: 'https://sun9-29.userapi.com/impg/qonYc1ab9zsuUPghZ8fkk0JNfo5aDPO-kV6yOw/pYmeYWPQ9Mw.jpg?size=749x776&quality=96&sign=6363bea3e48e498a84011827b157cdf3&type=album'),
_CardData(
text: 'text2',
descText: 'descText2',
icon: Icons.add_call,
imageUrl: 'https://cdn.culture.ru/images/63e764eb-458c-53f6-b161-79521a602d0c',)
];
return Center(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: data.map((e) => _HelloWidget.fromData(e)).toList(),
),
),
);
}
}
class _HelloWidget extends StatelessWidget {
final String text;
final IconData icon;
final String descText;
final String? imageUrl;
const _HelloWidget(this.text,
{this.icon = Icons.ac_unit_sharp, required this.descText, this.imageUrl});
factory _HelloWidget.fromData(_CardData data) => _HelloWidget(
data.text,
descText: data.descText,
icon: data.icon,
imageUrl: data.imageUrl,
);
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(left: 8.0, right: 8.0),
child: Container(
margin: const EdgeInsets.only(top: 10),
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: Colors.deepPurple,
borderRadius: BorderRadius.circular(15),
border: Border.all(
color: Colors.pinkAccent,
width: 3,
)),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Flexible(
child: SizedBox(
height: 140,
width: 100,
child: Image.network(
imageUrl ?? '',
fit: BoxFit.cover,
errorBuilder: (_, __, ___) => const Placeholder(),
),
),
),
Flexible(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
selectionColor: Colors.white,
text,
style: Theme.of(context).textTheme.headlineLarge,
),
Text(
descText,
style: Theme.of(context).textTheme.headlineSmall,
)
],
),
),
),
const Spacer(),
Padding(
padding: const EdgeInsets.all(8.0),
child: Icon(icon),
)
],
),
),
);
}
}
class _CardData {
final String text;
final IconData icon;
final String descText;
final String? imageUrl;
_CardData({
required this.text,
this.icon = Icons.accessibility_new,
required this.descText,
this.imageUrl,
});
}

View File

@ -0,0 +1,40 @@
import 'package:flutter/material.dart';
import 'package:pmu/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: TextStyle(
color: Colors.black,
fontWeight: FontWeight.w500,
fontSize: 26),
),
),
Text(
data.descText,
style: Theme.of(context).textTheme.bodySmall,
)
],
),
);
}
}

View File

@ -0,0 +1,138 @@
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 descText;
final String? imageUrl;
final OnLikeCallBack onLike;
final VoidCallback? onTap;
const _Card(this.text,
{this.icon = Icons.ac_unit_sharp,
required this.descText,
this.imageUrl,
this.onLike,
this.onTap});
factory _Card.fromData(
CardData data, {
OnLikeCallBack onLike,
VoidCallback? onTap,
}) =>
_Card(
data.text,
descText: data.descText,
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 Padding(
padding: const EdgeInsets.only(left: 8.0, right: 8.0),
child: GestureDetector(
onTap: widget.onTap,
child: SizedBox(
height: 150,
child: Container(
margin: const EdgeInsets.only(top: 10),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(13),
border: Border.all(
color: Colors.lightBlue,
width: 3,
)),
child: IntrinsicHeight(
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ClipRRect(
borderRadius: const BorderRadius.only(
bottomLeft: Radius.circular(10),
topLeft: Radius.circular(10),
),
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: 8.0, top: 4.0, right: 4.0, bottom: 4.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
widget.text,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.w500,
fontSize: 26),
),
Text(
widget.descText,
maxLines: 5,
overflow: TextOverflow.ellipsis,
style: Theme.of(context).textTheme.bodySmall,
)
],
),
),
),
//const Spacer(),
Align(
child: Padding(
padding: const EdgeInsets.only(
left: 8.0, top: 4.0, right: 8.0, bottom: 4.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.red,
key: ValueKey<int>(0),
)
: const Icon(
Icons.favorite_border,
key: ValueKey<int>(1),
)),
),
),
)
],
),
),
),
),
),
);
}
}

View File

@ -0,0 +1,110 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pmu/domain/models/card.dart';
import 'package:pmu/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> {
final Color _color = Colors.white;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.blue,
title: Text(
widget.title,
style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
),
),
body: const Body(),
);
}
}
class Body extends StatelessWidget {
const Body({super.key});
@override
Widget build(BuildContext context) {
final data = [
CardData(
text: 'Капибара',
descText: 'Противопехотная мина' * 10,
imageUrl:
'https://sun9-7.userapi.com/impg/5Lbmz05PKIjm3vWMa91N-nD2Zrx1IYHxhLSiKg/N9rlBPsRwPM.jpg?size=872x703&quality=96&sign=8e317544d6f5b07c225172ab73f80a81&type=album'),
CardData(
text: 'text1',
descText: 'descText1',
icon: Icons.account_balance,
imageUrl:
'https://sun9-29.userapi.com/impg/qonYc1ab9zsuUPghZ8fkk0JNfo5aDPO-kV6yOw/pYmeYWPQ9Mw.jpg?size=749x776&quality=96&sign=6363bea3e48e498a84011827b157cdf3&type=album'),
CardData(
text: 'text2',
descText: 'descText2',
icon: Icons.add_call,
imageUrl:
'https://cdn.culture.ru/images/63e764eb-458c-53f6-b161-79521a602d0c',
),
CardData(
text: 'text2',
descText: 'descText2',
icon: Icons.add_call,
imageUrl:
'https://cdn.culture.ru/images/63e764eb-458c-53f6-b161-79521a602d0c',
),
CardData(
text: 'text2',
descText: 'descText2',
icon: Icons.add_call,
imageUrl:
'https://cdn.culture.ru/images/63e764eb-458c-53f6-b161-79521a602d0c',
),
];
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 _showSnackBar(BuildContext context, String title, bool isLiked) {
WidgetsBinding.instance.addPostFrameCallback((_) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(
'The New $title ${isLiked ? 'liked!' : 'disliked'}',
style: Theme.of(context).textTheme.bodyLarge,
),
backgroundColor: Colors.lightBlue,
duration: const Duration(seconds: 1),
));
});
}
void _navToDetails(BuildContext context, CardData data) {
Navigator.push(
context,
CupertinoPageRoute(builder: (context) => DetailsPage(data)),
);
}
}