diff --git a/lib/comments_widget.dart b/lib/comments_widget.dart deleted file mode 100644 index 07cb5bd..0000000 --- a/lib/comments_widget.dart +++ /dev/null @@ -1,147 +0,0 @@ -import 'package:flutter/material.dart'; - -import 'comment_model.dart'; -import 'user_model.dart'; - -class CommentsWidget extends StatelessWidget { - const CommentsWidget({super.key}); - - @override - Widget build(BuildContext context) { - final data = [ - CommentModel( - title: 'Oh my god', - text: 'this app is so cool ' * 10, - user: UserModel( - nickname: 'Steve', - avatarUrl: - 'https://preview.free3d.com/img/2016/03/1875481443430303321/hld8c0oa.jpg', - ), - ), - CommentModel( - title: 'Listen to me', - text: 'This app is like Half-Life 3 - it will never be released', - user: UserModel( - nickname: 'G-Man', - avatarUrl: - 'https://us-tuna-sounds-images.voicemod.net/356aeabd-18d5-40d8-af31-b479f4eff183-1704672174928.png', - ), - ), - CommentModel( - title: 'BREAKING!!!', - text: 'The next lab work will have DLC in the form of likes', - user: UserModel( - nickname: 'Bethesda', - avatarUrl: 'https://i.playground.ru/p/BWixorSTeZQfoPvdVL9lgA.jpeg', - ), - ), - CommentModel( - title: 'Test', - text: 'Test', - user: UserModel( - nickname: 'Noname', - ), - ), - CommentModel( - title: 'Test', - text: 'Test', - user: UserModel( - nickname: 'Noname', - ), - ), - ]; - - return SingleChildScrollView( - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: data.map((e) => _Comment.fromData(e)).toList(), - ), - ); - } -} - -class _Comment extends StatelessWidget { - final String title; - final String text; - final UserModel? user; - - const _Comment({ - required this.title, - required this.text, - this.user, - }); - - factory _Comment.fromData(CommentModel model) => _Comment( - title: model.title, - text: model.text, - user: model.user, - ); - - @override - Widget build(BuildContext context) { - return Container( - margin: const EdgeInsets.all(16), - padding: const EdgeInsets.all(16), - decoration: BoxDecoration( - color: Colors.white70, - borderRadius: BorderRadius.circular(16), - border: Border.all(color: Colors.grey), - boxShadow: [ - BoxShadow( - color: Colors.grey.withOpacity(.5), - spreadRadius: 4, - offset: const Offset(0, 5), - blurRadius: 8, - ), - ], - ), - child: Row( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Column( - children: [ - ClipRRect( - borderRadius: BorderRadius.circular(30), - child: SizedBox( - height: 80, - width: 80, - child: Image.network( - user!.avatarUrl != null && user!.avatarUrl!.isNotEmpty - ? user!.avatarUrl! - : '', - fit: BoxFit.cover, - errorBuilder: (_, __, ___) => const Placeholder(), - ), - ), - ), - const SizedBox(height: 8), - Text( - user!.nickname, - style: Theme.of(context).textTheme.bodyMedium, - ), - ], - ), - Expanded( - child: Padding( - padding: const EdgeInsets.only(left: 16.0), - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text( - title, - style: Theme.of(context).textTheme.headlineSmall, - ), - const SizedBox(height: 8), - Text( - text, - style: Theme.of(context).textTheme.bodyLarge, - ), - ], - ), - ), - ), - ], - ), - ); - } -} diff --git a/lib/comment_model.dart b/lib/domain/models/comment.dart similarity index 87% rename from lib/comment_model.dart rename to lib/domain/models/comment.dart index 4913e8e..b02b2a7 100644 --- a/lib/comment_model.dart +++ b/lib/domain/models/comment.dart @@ -1,4 +1,4 @@ -import 'user_model.dart'; +import 'user.dart'; class CommentModel { final String title; diff --git a/lib/user_model.dart b/lib/domain/models/user.dart similarity index 100% rename from lib/user_model.dart rename to lib/domain/models/user.dart diff --git a/lib/main.dart b/lib/main.dart index 45af0f2..357df43 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,8 +1,6 @@ -import 'dart:math'; - import 'package:flutter/material.dart'; -import 'comments_widget.dart'; +import 'package:pmu_labworks/view/home_page/home_page.dart'; void main() { runApp(const MyApp()); @@ -20,63 +18,7 @@ class MyApp extends StatelessWidget { colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue), useMaterial3: true, ), - home: const MyHomePage(title: 'Comments App'), + home: const HomePage(title: 'Comments App'), ); } } - -class MyHomePage extends StatefulWidget { - const MyHomePage({super.key, required this.title}); - - final String title; - - @override - State createState() => _MyHomePageState(); -} - -class _MyHomePageState extends State { - Color _color = Colors.transparent; - - @override - void initState() { - super.initState(); - _color = _generateColor(); - } - - @override - Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar( - backgroundColor: _color, - title: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text( - widget.title, - style: TextStyle( - fontSize: 20, - fontWeight: FontWeight.bold, - )), - Text( - 'made by Factorino', - style: TextStyle( - fontSize: 12, - fontStyle: FontStyle.italic, - ), - ), - ], - ), - ), - body: const CommentsWidget(), - ); - } - - Color _generateColor() { - final Random random = Random(); - final int red = (random.nextInt(106) + 150); - final int green = (random.nextInt(106) + 150); - final int blue = (random.nextInt(106) + 150); - - return Color.fromARGB(255, red, green, blue); - } -} diff --git a/lib/view/details_page/details_page.dart b/lib/view/details_page/details_page.dart new file mode 100644 index 0000000..f2d95ff --- /dev/null +++ b/lib/view/details_page/details_page.dart @@ -0,0 +1,78 @@ +import 'package:flutter/material.dart'; + +import 'package:pmu_labworks/domain/models/comment.dart'; + +class DetailsPage extends StatelessWidget { + final CommentModel model; + + const DetailsPage(this.model, {super.key}); + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: Text('${model.user.nickname}\'s comment'), + ), + body: Padding( + padding: const EdgeInsets.all(16.0), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Stack( + children: [ + ClipRRect( + borderRadius: BorderRadius.circular(8.0), + child: model.user.avatarUrl != null + ? Image.network( + model.user.avatarUrl!, + height: 150, + width: 150, + fit: BoxFit.cover, + ) + : Container( + height: 150, + width: 150, + color: Colors.grey, + child: const Icon(Icons.person, size: 50), + ), + ), + Positioned( + left: 8, + bottom: 8, + child: Container( + padding: const EdgeInsets.symmetric( + vertical: 4.0, horizontal: 8.0), + color: Colors.black.withOpacity(0.6), + child: Text( + model.user.nickname, + style: const TextStyle( + color: Colors.white, + fontWeight: FontWeight.bold, + fontSize: 16, + ), + ), + ), + ), + ], + ), + const SizedBox(height: 24), + Text( + model.title, + style: const TextStyle( + fontSize: 24, + fontWeight: FontWeight.bold, + ), + ), + const SizedBox(height: 16), + Text( + model.text, + style: const TextStyle( + fontSize: 16, + ), + ), + ], + ), + ), + ); + } +} diff --git a/lib/view/home_page/comment.dart b/lib/view/home_page/comment.dart new file mode 100644 index 0000000..9025810 --- /dev/null +++ b/lib/view/home_page/comment.dart @@ -0,0 +1,159 @@ +part of 'home_page.dart'; + +typedef OnActionCallback = void Function( + String nickname, bool isLiked, bool isDisliked)?; + +class _Comment extends StatefulWidget { + final String title; + final String text; + final UserModel? user; + final OnActionCallback onAction; + final VoidCallback? onTap; + + const _Comment({ + required this.title, + required this.text, + this.user, + this.onAction, + this.onTap, + }); + + factory _Comment.fromData( + CommentModel model, { + OnActionCallback onAction, + VoidCallback? onTap, + }) => + _Comment( + title: model.title, + text: model.text, + user: model.user, + onAction: onAction, + onTap: onTap, + ); + + @override + State<_Comment> createState() => _CommentState(); +} + +class _CommentState extends State<_Comment> { + bool isLiked = false; + bool isDisliked = false; + + void _onLike() { + setState(() { + isLiked = !isLiked; + if (isLiked && isDisliked) { + isDisliked = false; + } + }); + if (widget.onAction != null) { + widget.onAction?.call(widget.user!.nickname, isLiked, isDisliked); + } + } + + void _onDislike() { + setState(() { + isDisliked = !isDisliked; + if (isDisliked && isLiked) { + isLiked = false; + } + }); + if (widget.onAction != null) { + widget.onAction?.call(widget.user!.nickname, isLiked, isDisliked); + } + } + + @override + Widget build(BuildContext context) { + return GestureDetector( + onTap: widget.onTap, + child: Container( + margin: const EdgeInsets.all(16), + padding: const EdgeInsets.all(16), + decoration: BoxDecoration( + color: Colors.white70, + borderRadius: BorderRadius.circular(16), + border: Border.all(color: Colors.grey), + boxShadow: [ + BoxShadow( + color: Colors.grey.withOpacity(.5), + spreadRadius: 4, + offset: const Offset(0, 5), + blurRadius: 8, + ), + ], + ), + child: Row( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Column( + children: [ + ClipRRect( + borderRadius: BorderRadius.circular(30), + child: SizedBox( + height: 80, + width: 80, + child: Image.network( + widget.user!.avatarUrl != null && + widget.user!.avatarUrl!.isNotEmpty + ? widget.user!.avatarUrl! + : '', + fit: BoxFit.cover, + errorBuilder: (_, __, ___) => const Placeholder(), + ), + ), + ), + const SizedBox(height: 8), + Text( + widget.user!.nickname, + style: Theme.of(context).textTheme.bodyMedium, + ), + Row( + children: [ + IconButton( + iconSize: 20, + icon: Icon( + isLiked ? Icons.thumb_up : Icons.thumb_up_outlined, + color: isLiked ? Colors.blue : Colors.grey, + ), + onPressed: _onLike, + ), + IconButton( + iconSize: 20, + icon: Icon( + isDisliked + ? Icons.thumb_down + : Icons.thumb_down_outlined, + color: isDisliked ? Colors.red : Colors.grey, + ), + onPressed: _onDislike, + ), + ], + ), + ], + ), + Expanded( + child: Padding( + padding: const EdgeInsets.only(left: 16.0), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + widget.title, + style: Theme.of(context).textTheme.headlineSmall, + ), + const SizedBox(height: 8), + Text( + widget.text, + style: Theme.of(context).textTheme.bodyLarge, + ), + ], + ), + ), + ), + ], + ), + ), + ); + } +} diff --git a/lib/view/home_page/home_page.dart b/lib/view/home_page/home_page.dart new file mode 100644 index 0000000..58b0ba3 --- /dev/null +++ b/lib/view/home_page/home_page.dart @@ -0,0 +1,164 @@ +import 'dart:math'; + +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; + +import 'package:pmu_labworks/domain/models/comment.dart'; +import 'package:pmu_labworks/domain/models/user.dart'; + +import 'package:pmu_labworks/view/details_page/details_page.dart'; + +part 'comment.dart'; + +class HomePage extends StatefulWidget { + const HomePage({super.key, required this.title}); + + final String title; + + @override + State createState() => _HomePageState(); +} + +class _HomePageState extends State { + Color _color = Colors.transparent; + + @override + void initState() { + super.initState(); + _color = _generateColor(); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + backgroundColor: _color, + title: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text(widget.title, + style: TextStyle( + fontSize: 20, + fontWeight: FontWeight.bold, + )), + Text( + 'made by Factorino', + style: TextStyle( + fontSize: 12, + fontStyle: FontStyle.italic, + ), + ), + ], + ), + ), + body: const Body(), + ); + } + + Color _generateColor() { + final Random random = Random(); + final int red = (random.nextInt(106) + 150); + final int green = (random.nextInt(106) + 150); + final int blue = (random.nextInt(106) + 150); + + return Color.fromARGB(255, red, green, blue); + } +} + +class Body extends StatelessWidget { + const Body({super.key}); + + @override + Widget build(BuildContext context) { + final data = [ + CommentModel( + title: 'Oh my god', + text: 'this app is so cool ' * 10, + user: UserModel( + nickname: 'Steve', + avatarUrl: + 'https://preview.free3d.com/img/2016/03/1875481443430303321/hld8c0oa.jpg', + ), + ), + CommentModel( + title: 'Listen to me', + text: 'This app is like Half-Life 3 - it will never be released', + user: UserModel( + nickname: 'G-Man', + avatarUrl: + 'https://us-tuna-sounds-images.voicemod.net/356aeabd-18d5-40d8-af31-b479f4eff183-1704672174928.png', + ), + ), + CommentModel( + title: 'BREAKING!!!', + text: + 'ONLY NOW you can purchase the ability to put likes and dislikes for only 299\$', + user: UserModel( + nickname: 'Bethesda', + avatarUrl: 'https://i.playground.ru/p/BWixorSTeZQfoPvdVL9lgA.jpeg', + ), + ), + CommentModel( + title: 'Test', + text: 'Test', + user: UserModel( + nickname: 'Noname', + ), + ), + CommentModel( + title: 'Test', + text: 'Test', + user: UserModel( + nickname: 'Noname', + ), + ), + ]; + + return Center( + child: SingleChildScrollView( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: data.map((data) { + return _Comment.fromData( + data, + onAction: (String nickname, bool isLiked, bool isDisliked) => + _showSnackBar(context, nickname, isLiked, isDisliked), + onTap: () => _navToDetails(context, data), + ); + }).toList(), + ), + ), + ); + } + + void _navToDetails(BuildContext context, CommentModel data) { + Navigator.push( + context, + CupertinoPageRoute(builder: (context) => DetailsPage(data)), + ); + } + + void _showSnackBar( + BuildContext context, String nickname, bool isLiked, bool isDisliked) { + WidgetsBinding.instance.addPostFrameCallback((_) { + final String action; + + if (isLiked) { + action = 'liked'; + } else if (isDisliked) { + action = 'disliked'; + } else { + action = 'neutralized'; // Состояние без лайков/дизлайков + } + + ScaffoldMessenger.of(context).showSnackBar(SnackBar( + content: Text( + '$nickname\'s comment $action', + style: Theme.of(context).textTheme.bodyLarge, + ), + backgroundColor: Colors.white70, + duration: const Duration(seconds: 1), + )); + }); + } +}