Compare commits

..

2 Commits
master ... lab4

Author SHA1 Message Date
Serxiolog
fa12a20626 Four LABA 2024-10-16 16:13:47 +04:00
Serxiolog
e802b33fb2 Third Laba 2024-10-02 17:54:28 +04:00
5 changed files with 308 additions and 54 deletions

View File

@ -0,0 +1,36 @@
import 'package:first_project/presentation/home_page/home_page.dart';
import 'package:flutter/material.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),
child: Image.network(
data.imageUrl ?? '',
),
),
Padding(
padding: const EdgeInsets.only(bottom: 4),
child: Text(
data.text,
style: Theme.of(context).textTheme.headlineLarge,
)),
Text(
data.description,
style: Theme.of(context).textTheme.bodyLarge,
)
],
),
);
}
}

View File

@ -1,5 +1,6 @@
import 'dart:math';
import 'package:first_project/presentation/home_page/home_page.dart';
import 'package:flutter/material.dart';
void main() {
@ -9,67 +10,15 @@ void main() {
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'KozyrevSS'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
Color _color = Colors.white;
void _incrementCounter() {
setState(() {
_counter++;
_color = Color((Random().nextDouble() * 0xFFFFFF).toInt()).withOpacity(1.0);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: _color,
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
if (_counter > 10)
const Text("Why are you doing this?",)
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), );
}
}

View File

@ -0,0 +1 @@

View File

@ -0,0 +1,164 @@
part of 'home_page.dart';
typedef OnLikeCallback = void Function(String title, bool isLiked)?;
class CardData {
final String text;
final String description;
final IconData icon;
final String imageUrl;
const CardData(
this.text, {
required this.description,
this.icon = Icons.add_call,
this.imageUrl =
"https://i.pinimg.com/736x/5f/14/b3/5f14b3f14fcd157bc4dffa39085396cc.jpg",
});
}
class _Card extends StatefulWidget {
final String text;
final String description;
final String? imageUrl;
final OnLikeCallback onLike;
final VoidCallback? onTap;
const _Card(
this.text, {
required this.description,
required this.imageUrl,
this.onLike,
this.onTap,
});
factory _Card.fromData(
CardData data, {
OnLikeCallback onLike,
VoidCallback? onTap,
}) =>
_Card(
data.text,
description: data.description,
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(10),
constraints: const BoxConstraints(minHeight: 140),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(20),
border: Border.all(color: Colors.green, width: 2),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(.4),
offset: const Offset(0, 5),
blurRadius: 6,
spreadRadius: 3),
]),
child: IntrinsicHeight(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Stack(children: [
ClipRRect(
borderRadius: const BorderRadius.only(
bottomLeft: Radius.circular(18),
topLeft: Radius.circular(18),
),
child: SizedBox(
height: double.infinity,
width: 120,
child: Image.network(
widget.imageUrl ?? '',
fit: BoxFit.cover,
errorBuilder: (_, __, ___) => const Placeholder(),
),
),
),
Align(
alignment: Alignment.bottomLeft,
child: Container(
decoration: const BoxDecoration(
color: Colors.purple,
borderRadius: BorderRadius.only(
topRight: Radius.circular(20),
bottomLeft: Radius.circular(18))),
padding: const EdgeInsets.fromLTRB(8, 2, 8, 2),
child: Text('Новинка',
style: Theme.of(context)
.textTheme
.bodyMedium
?.copyWith(color: Colors.white)),
),
),
]),
Expanded(
child: Padding(
padding: const EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Text(
widget.text,
style: Theme.of(context).textTheme.headlineLarge,
),
Text(
widget.description,
style: Theme.of(context).textTheme.bodySmall,
)
],
),
),
),
Align(
alignment: Alignment.center,
child: Padding(
padding: const EdgeInsets.only(
left: 8,
right: 8,
bottom: 8,
),
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,104 @@
import 'package:first_project/domain/models/card.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.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(
'Hello',
style: Theme.of(context).textTheme.bodyMedium,
),
backgroundColor: Colors.purple,
duration: const Duration(seconds: 1),
));
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.purple,
title: Text(widget.title),
),
body: const Body(),
);
}
}
class Body extends StatelessWidget {
const Body({super.key});
@override
Widget build(BuildContext context) {
final data = [
const CardData(
"First",
description: "SomeText",
),
const CardData(
"Second",
icon: Icons.gamepad,
description: "ManyText",
imageUrl:
"https://i.pinimg.com/originals/21/73/24/217324138d1bbc91663d4943ebe5de60.jpg",
),
const CardData(
"Third",
icon: Icons.offline_bolt_outlined,
description: "Wow >_<",
),
];
return Center(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: data.map(
(e) {
return _Card.fromData(
e,
onLike: (String title, bool isLiked) =>
_showLiked(context, title, isLiked),
onTap: () => _navToDetails(context, e),
);
},
).toList(),
),
));
}
void _navToDetails(BuildContext context, CardData data) {
Navigator.push(
context,
CupertinoPageRoute(builder: (context) => DetailsPage(data)),
);
}
void _showLiked(BuildContext context, String title, bool isLiked) {
WidgetsBinding.instance.addPostFrameCallback((_) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(
'Аниме $title ${isLiked ? 'лайкнуто!' : 'дизлайнуто'}',
style: Theme.of(context).textTheme.bodyMedium,
),
backgroundColor: Colors.purple,
duration: const Duration(seconds: 1),
));
});
}
}