Compare commits

...

4 Commits
main ... Lab_4

Author SHA1 Message Date
9e3be50899 implemented details page 2024-10-03 01:00:26 +04:00
e1e9ca4194 implemented SnackBar appearance 2024-10-03 00:44:31 +04:00
ff75a800ef refactored 2024-10-03 00:24:49 +04:00
256024775c finished card widget 2024-10-02 23:51:57 +04:00
5 changed files with 276 additions and 58 deletions

View File

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

View File

@ -1,5 +1,5 @@
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter_android_app/presentation/home_page/home_page.dart';
void main() {
runApp(const MyApp());
@ -16,63 +16,7 @@ class MyApp extends StatelessWidget {
colorScheme: ColorScheme.fromSeed(seedColor: Colors.green),
useMaterial3: true,
),
home: const MyHomePage(title: 'Шабунов Олег Андреевич 💯'),
);
}
}
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.deepPurpleAccent;
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(
'Кнопка нажата столько раз:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
if (_counter > 10)
Text(
'А теперь потапай хомяка',
style: Theme.of(context).textTheme.headlineMedium,
)
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
backgroundColor: _color,
tooltip: 'Increment',
child: const Icon(Icons.ac_unit),
),
home: const MyHomePage(title: 'Карточки'),
);
}
}

View File

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

View File

@ -0,0 +1,132 @@
part of 'home_page.dart';
typedef OnLikeCallback = void Function(String title, bool isLiked)?;
class _Card extends StatefulWidget {
final String title;
final String description;
final IconData icon;
final String? imageUrl;
final OnLikeCallback onLike;
final VoidCallback? onTap;
const _Card({
super.key,
required this.title,
required this.description,
this.icon = Icons.hail,
this.imageUrl,
this.onLike,
this.onTap,
});
factory _Card.fromData(
CardData data, {
final OnLikeCallback onLike,
VoidCallback? onTap,
}) => _Card(
title: data.title,
description: data.description,
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.fromLTRB(20, 8, 20, 8),
constraints: const BoxConstraints(minHeight: 140),
decoration: BoxDecoration(
color: Colors.deepPurple.shade200,
borderRadius: BorderRadius.circular(15),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.5),
spreadRadius: 4,
offset: const Offset(0, 5),
blurRadius: 6,
)
],
),
child: IntrinsicHeight(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
ClipRRect(
borderRadius: const BorderRadius.only(
bottomLeft: Radius.circular(15),
topLeft: Radius.circular(15),
),
child: SizedBox(
height: double.infinity,
width: 100,
child: Image.network(
widget.imageUrl ?? '',
fit: BoxFit.cover,
errorBuilder: (_, __, ___) => const Placeholder(),
),
),
),
Flexible(
child: Padding(
padding: const EdgeInsets.only(left: 12.0, top: 12, bottom: 12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
widget.title,
style: Theme.of(context).textTheme.headlineLarge,
),
Text(
widget.description,
style: Theme.of(context).textTheme.bodyLarge),
],
),
),
),
Align(
alignment: Alignment.bottomRight,
child: Padding(
padding: const EdgeInsets.fromLTRB(8, 0, 16, 16),
child: GestureDetector(
onTap: () {
setState(() {
isLiked = !isLiked;
});
widget.onLike?.call(widget.title, isLiked);
},
child: AnimatedSwitcher(
duration: const Duration(milliseconds: 100),
child: isLiked
? const Icon(
Icons.favorite,
color: Colors.redAccent,
key: ValueKey(0),
)
: const Icon(
Icons.favorite_border,
key: ValueKey(1),
),
),
),
),
),
],
),
),
),
);
}
}

View File

@ -0,0 +1,98 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_android_app/domain/models/card.dart';
import 'package:flutter_android_app/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('Hello', style: Theme.of(context).textTheme.bodyLarge),
backgroundColor: Colors.deepPurple.shade200,
duration: const Duration(seconds: 4),
));
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.deepPurple.shade200,
title: Text(widget.title),
),
body: const Body(),
);
}
}
class Body extends StatelessWidget {
const Body({super.key});
@override
Widget build(BuildContext context) {
final cardsData = [
CardData(
title: 'Title 1',
description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit',
imageUrl: 'https://i.imgur.com/a9WA68S.png',
),
CardData(
title: 'Title 2',
description: 'Lorem ipsum dolor sit amet',
icon: Icons.add_chart_outlined,
imageUrl: 'https://i.imgur.com/dAUcs6I.png',
),
CardData(
title: 'Title 3',
description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor',
imageUrl: 'https://i.imgur.com/m2FhVAK.png',
),
];
return Center(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: cardsData.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(
'Card $title ${isLiked ? 'liked' : 'disliked'}',
style: Theme.of(context).textTheme.bodyLarge
),
backgroundColor: Colors.deepPurple.shade200,
duration: const Duration(seconds: 2),
));
});
}
void _navToDetails(BuildContext context, CardData data) {
Navigator.push(context, CupertinoPageRoute(builder: (context) => DetailsPage(data)));
}
}