From ff75a800ef5ba80d3fcef051663147501613bb6b Mon Sep 17 00:00:00 2001 From: Oleg Shabunov Date: Thu, 3 Oct 2024 00:24:49 +0400 Subject: [PATCH] refactored --- lib/main.dart | 153 +--------------------- lib/presentation/home_page/card.dart | 130 ++++++++++++++++++ lib/presentation/home_page/home_page.dart | 60 +++++++++ 3 files changed, 191 insertions(+), 152 deletions(-) create mode 100644 lib/presentation/home_page/card.dart create mode 100644 lib/presentation/home_page/home_page.dart diff --git a/lib/main.dart b/lib/main.dart index 4506531..c9a3e0d 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:flutter_android_app/presentation/home_page/home_page.dart'; void main() { runApp(const MyApp()); @@ -19,155 +20,3 @@ class MyApp extends StatelessWidget { ); } } - -class MyHomePage extends StatefulWidget { - const MyHomePage({super.key, required this.title}); - - final String title; - - @override - State createState() => _MyHomePageState(); -} - -class _MyHomePageState extends State { - @override - Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar( - backgroundColor: Colors.deepPurple.shade200, - title: Text(widget.title), - ), - body: const MyWidget(), - ); - } -} - -class MyWidget extends StatelessWidget { - const MyWidget({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((e) => _Card.fromData(e)).toList(), - ), - ), - ); - } -} - -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, - }); -} - -class _Card extends StatelessWidget { - final String title; - final String description; - final IconData icon; - final String? imageUrl; - - const _Card({ - super.key, - required this.title, - required this.description, - this.icon = Icons.hail, - this.imageUrl, - }); - - factory _Card.fromData(_CardData data) => _Card( - title: data.title, - description: data.description, - icon: data.icon, - imageUrl: data.imageUrl, - ); - - @override - Widget build(BuildContext context) { - return Container( - margin: const EdgeInsets.fromLTRB(20, 8, 20, 8), - padding: const EdgeInsets.fromLTRB(20, 15, 20, 15), - 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: Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - ClipRRect( - borderRadius: BorderRadius.circular(20), - child: SizedBox( - height: 140, - width: 100, - child: Image.network( - imageUrl ?? '', - fit: BoxFit.cover, - errorBuilder: (_, __, ___) => const Placeholder(), - ), - ), - ), - Flexible( - child: Padding( - padding: const EdgeInsets.only(left: 15.0), - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text( - title, - style: Theme.of(context).textTheme.headlineLarge, - ), - Text( - description, - style: Theme.of(context).textTheme.bodyLarge), - ], - ), - ), - ), - Padding( - padding: const EdgeInsets.only(left: 16.0), - child: Icon(icon), - ), - ], - ), - ); - } -} diff --git a/lib/presentation/home_page/card.dart b/lib/presentation/home_page/card.dart new file mode 100644 index 0000000..10bc695 --- /dev/null +++ b/lib/presentation/home_page/card.dart @@ -0,0 +1,130 @@ +part of 'home_page.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, + }); +} + +class _Card extends StatefulWidget { + final String title; + final String description; + final IconData icon; + final String? imageUrl; + + const _Card({ + super.key, + required this.title, + required this.description, + this.icon = Icons.hail, + this.imageUrl, + }); + + factory _Card.fromData(_CardData data) => _Card( + title: data.title, + description: data.description, + icon: data.icon, + imageUrl: data.imageUrl, + ); + + @override + State<_Card> createState() => _CardState(); +} + +class _CardState extends State<_Card> { + bool isLiked = false; + + @override + Widget build(BuildContext context) { + return 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: 15.0), + 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; + }); + }, + 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), + ), + ), + ), + ), + ), + ], + ), + ), + ); + } +} diff --git a/lib/presentation/home_page/home_page.dart b/lib/presentation/home_page/home_page.dart new file mode 100644 index 0000000..0fdcae7 --- /dev/null +++ b/lib/presentation/home_page/home_page.dart @@ -0,0 +1,60 @@ +import 'package:flutter/material.dart'; + +part 'card.dart'; + +class MyHomePage extends StatefulWidget { + const MyHomePage({super.key, required this.title}); + + final String title; + + @override + State createState() => _MyHomePageState(); +} + +class _MyHomePageState extends State { + @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((e) => _Card.fromData(e)).toList(), + ), + ), + ); + } +}