Compare commits

..

6 Commits

Author SHA1 Message Date
1d6193d8c1 LabWork04 is Done 2024-09-30 02:09:26 +04:00
40b9d1d9b6 LabWork03 Some additions 2024-09-13 01:19:44 +04:00
4913dc0fe7 LabWork03 Comments is done 2024-09-13 00:51:36 +04:00
3440713e50 LabWork02 ToDo List is done 2024-09-11 20:13:39 +04:00
3a8b49f9c9 LabWork01 remove the DEBUG banner 2024-09-10 02:00:11 +04:00
e73cee57a3 LabWork01 Done 2024-09-10 01:41:34 +04:00
6 changed files with 429 additions and 107 deletions

View File

@ -0,0 +1,13 @@
import 'user.dart';
class CommentModel {
final String title;
final String text;
final UserModel user;
CommentModel({
required this.title,
required this.text,
required this.user,
});
}

View File

@ -0,0 +1,9 @@
class UserModel {
final String nickname;
final String? avatarUrl;
UserModel({
required this.nickname,
this.avatarUrl,
});
}

View File

@ -1,5 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:pmu_labworks/view/home_page/home_page.dart';
void main() { void main() {
runApp(const MyApp()); runApp(const MyApp());
} }
@ -7,119 +9,16 @@ void main() {
class MyApp extends StatelessWidget { class MyApp extends StatelessWidget {
const MyApp({super.key}); const MyApp({super.key});
// This widget is the root of your application.
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return MaterialApp(
title: 'Flutter Demo', title: 'Comments App',
debugShowCheckedModeBanner: false,
theme: ThemeData( theme: ThemeData(
// This is the theme of your application. colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
//
// TRY THIS: Try running your application with "flutter run". You'll see
// the application has a purple toolbar. Then, without quitting the app,
// try changing the seedColor in the colorScheme below to Colors.green
// and then invoke "hot reload" (save your changes or press the "hot
// reload" button in a Flutter-supported IDE, or press "r" if you used
// the command line to start the app).
//
// Notice that the counter didn't reset back to zero; the application
// state is not lost during the reload. To reset the state, use hot
// restart instead.
//
// This works for code too, not just values: Most code changes can be
// tested with just a hot reload.
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true, useMaterial3: true,
), ),
home: const MyHomePage(title: 'Flutter Demo Home Page'), home: const HomePage(title: 'Comments App'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// TRY THIS: Try changing the color here to a specific color (to
// Colors.amber, perhaps?) and trigger a hot reload to see the AppBar
// change color while the other colors stay the same.
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
//
// TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint"
// action in the IDE, or press "p" in the console), to see the
// wireframe for each widget.
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
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),
), // This trailing comma makes auto-formatting nicer for build methods.
); );
} }
} }

View File

@ -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,
),
),
],
),
),
);
}
}

View File

@ -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,
),
],
),
),
),
],
),
),
);
}
}

View File

@ -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<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
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),
));
});
}
}