diff --git a/lib/domain/models/card.dart b/lib/domain/models/card.dart new file mode 100644 index 0000000..39e24b7 --- /dev/null +++ b/lib/domain/models/card.dart @@ -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.ac_unit_outlined, + this.imageUrl, + }); +} \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index 5cb6899..2d08338 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:flutter_labs/presentation/home_page/home_page.dart'; void main() { runApp(const MyApp()); @@ -15,61 +16,7 @@ class MyApp extends StatelessWidget { colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), useMaterial3: true, ), - home: const MyHomePage(title: 'Разин Андрей Александрович'), + home: const HomePage(title: 'Marvel Heroes'), ); } -} - -class MyHomePage extends StatefulWidget { - const MyHomePage({super.key, required this.title}); - - - final String title; - - @override - State createState() => _MyHomePageState(); -} - -class _MyHomePageState extends State { - int _counter = 0; - - void _incrementCounter() { - setState(() { - _counter++; - }); - } - - @override - Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar( - backgroundColor: Theme - .of(context) - .colorScheme - .inversePrimary, - title: Text(widget.title), - ), - body: Center( - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - const Text( - 'You have pushed the button this many times:', - ), - Text( - '$_counter', - style: Theme - .of(context) - .textTheme - .headlineMedium, - ), - ], - ), - ), - floatingActionButton: FloatingActionButton( - onPressed: _incrementCounter, - tooltip: 'Increment', - child: const Icon(Icons.add), - ),); - } -} +} \ No newline at end of file diff --git a/lib/presentation/detail_page/detail_page.dart b/lib/presentation/detail_page/detail_page.dart new file mode 100644 index 0000000..c76fb2b --- /dev/null +++ b/lib/presentation/detail_page/detail_page.dart @@ -0,0 +1,37 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_labs/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, + ) + ], + ), + ); + } +} \ No newline at end of file diff --git a/lib/presentation/home_page/card.dart b/lib/presentation/home_page/card.dart new file mode 100644 index 0000000..8dc3d48 --- /dev/null +++ b/lib/presentation/home_page/card.dart @@ -0,0 +1,159 @@ +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.ac_unit_outlined, + 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.white, + borderRadius: BorderRadius.circular(20), + boxShadow: [ + BoxShadow( + color: Colors.grey, + 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: Stack( + children: [ + Positioned.fill( + child: Image.network( + widget.imageUrl ?? '', + fit: BoxFit.cover, + errorBuilder: (_, __, ___) => const Placeholder(), + ), + ), + Align( + alignment: Alignment.bottomLeft, + child: Container( + decoration: const BoxDecoration( + color: Colors.deepPurple, + borderRadius: BorderRadius.only( + topRight: Radius.circular(20), + // + )), + padding: const EdgeInsets.fromLTRB(8, 2, 8, 2), + child: Text( + 'Marvel studio', + style: Theme.of(context) + .textTheme + .bodyMedium + ?.copyWith(color: Colors.black), + ), + ), + ), + ], + ), + ), + ), + 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, + bottom: 16, + ), + child: GestureDetector( + onTap: () { + setState(() => isLiked = !isLiked); + widget.onLike?.call(widget.text, isLiked); + }, + child: AnimatedSwitcher( + duration: const Duration(microseconds: 200), + child: isLiked + ? const Icon( + Icons.favorite, + color: Colors.deepPurple, + key: ValueKey(0), + ) + : const Icon( + Icons.favorite_border, + key: ValueKey(1), + ), + ), + ), + ), + ) + ], + ), + ), + ), + ); + } +} \ No newline at end of file diff --git a/lib/presentation/home_page/home_page.dart b/lib/presentation/home_page/home_page.dart new file mode 100644 index 0000000..853114b --- /dev/null +++ b/lib/presentation/home_page/home_page.dart @@ -0,0 +1,90 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_labs/presentation/detail_page/detail_page.dart'; +import 'package:flutter/cupertino.dart'; +import 'package:flutter_labs/domain/models/card.dart'; + +part 'card.dart'; + +class HomePage extends StatefulWidget { + const HomePage({super.key, required this.title}); + + final String title; + + @override + State createState() => _HomePageState(); +} + +class _HomePageState extends State { + final Color _color = Colors.deepPurple; + + @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('Vision', + descriptionText: 'Просто крутой чел', + icon: Icons.abc, + imageUrl: + 'https://i.pinimg.com/originals/a0/18/ab/a018abf65f1f938e125d357f533f68da.png'), + CardData('Wolverine', + descriptionText: 'Менее крутой чел', + icon: Icons.access_alarm_outlined, + imageUrl:'https://i.pinimg.com/originals/1e/c7/24/1ec7240225d9cd6eecaf0c6653e3cdfc.png' + ), CardData('Deadpool', + descriptionText: 'Самый крутой чел', + icon: Icons.access_alarm_rounded, + imageUrl: + 'https://static.wikia.nocookie.net/p__/images/7/76/Deadpool_FB_Artwork_1.jpg/revision/latest/scale-to-width-down/1200?cb=20160211031703&path-prefix=protagonist'), + ]; + + 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( + '$title ${isLiked ? 'was liked!' : 'was disliked -_-'}', + style: Theme.of(context).textTheme.bodyLarge, + ), + backgroundColor: Colors.deepPurple, + duration: const Duration(seconds: 1), + )); + }); + } +} \ No newline at end of file