feat(blogPostCreate): при вводе данных в текстовое поле, изображение уменьшается

This commit is contained in:
mso7nueh
2024-08-14 13:09:15 +03:00
parent 7a88a7fffc
commit eee0282de0
3 changed files with 83 additions and 52 deletions

View File

@@ -1,8 +1,10 @@
import 'dart:async';
import 'dart:io';
import 'package:eatvio_app/core/app_export.dart';
import 'package:eatvio_app/widgets/custom_text_form_field.dart';
import 'package:flutter/material.dart';
import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';
import 'package:image_picker/image_picker.dart';
import '../../../../presentation/feed/blog_post_create_screen/bloc/blog_post_create_bloc.dart';
@@ -25,6 +27,27 @@ class BlogPostCreateScreen extends StatefulWidget {
}
class _BlogPostCreateScreenState extends State<BlogPostCreateScreen> {
bool shrinkImage = false;
late StreamSubscription<bool> keyboardSubscription;
@override
void initState() {
super.initState();
var keyboardVisibilityController = KeyboardVisibilityController();
keyboardSubscription = keyboardVisibilityController.onChange.listen((bool visible) {
setState(() {
shrinkImage = visible;
});
});
}
@override
void dispose() {
keyboardSubscription.cancel();
super.dispose();
}
Widget build(BuildContext context) {
return BlocBuilder<BlogPostCreateBloc, BlogPostCreateState>(
builder: (context, state) {
@@ -79,9 +102,9 @@ class _BlogPostCreateScreenState extends State<BlogPostCreateScreen> {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Изображение поста'),
SizedBox(height: 8),
_buildAddPhoto(context, state),
if (state.youtubeURL!.text.isEmpty) Text('Изображение поста'),
if (state.youtubeURL!.text.isEmpty) SizedBox(height: 8),
if (state.youtubeURL!.text.isEmpty) _buildAddPhoto(state),
SizedBox(height: 16),
Text('Заголовок'),
SizedBox(height: 8),
@@ -97,9 +120,9 @@ class _BlogPostCreateScreenState extends State<BlogPostCreateScreen> {
minLines: 1,
),
SizedBox(height: 16),
Text('Ссылка на видео'),
SizedBox(height: 8),
CustomTextFormField(
if (state.image == null) Text('Ссылка на видео'),
if (state.image == null) SizedBox(height: 8),
if (state.image == null) CustomTextFormField(
controller: state.youtubeURL,
),
SizedBox(height: 80),
@@ -108,55 +131,62 @@ class _BlogPostCreateScreenState extends State<BlogPostCreateScreen> {
),
);
}
_buildAddPhoto(BlogPostCreateLoadedState state) {
bool isLoading = state.isLoading;
return GestureDetector(
onTap: addImage(context, isLoading),
child: Stack(
children: [
if (state.image == null)
AnimatedContainer(
height: shrinkImage ? 52 : 128,
curve: Curves.easeIn,
duration: Duration(milliseconds: 150),
decoration: AppDecoration.fillGray.copyWith(
borderRadius: BorderRadius.circular(16),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Icon(
Icons.photo_library,
color: appTheme.blueGray300,
),
SizedBox(width: 8),
Text(
"Добавить фото",
selectionColor: appTheme.blueA400,
),
],
),
),
if (state.image != null)
AnimatedContainer(
width: shrinkImage ? 52 : MediaQuery.of(context).size.width - 32,
curve: Curves.easeIn,
duration: Duration(milliseconds: 150),
child: ClipRRect(
borderRadius: BorderRadius.circular(16),
child: Image(
image: FileImage(
File(state.image!.path),
),
),
),
),
],
),
);
}
}
_submitForm(BuildContext context, String title, String description, String youtubeURL) async {
context.read<BlogPostCreateBloc>().add(CreateBlogPost());
}
_buildAddPhoto(BuildContext context, BlogPostCreateLoadedState state) {
bool isLoading = state.isLoading;
return GestureDetector(
onTap: addImage(context, isLoading),
child: Stack(
children: [
if (state.image == null)
Container(
height: 128,
decoration: AppDecoration.fillGray.copyWith(
borderRadius: BorderRadius.circular(16),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Icon(
Icons.photo_library,
color: appTheme.blueGray300,
),
SizedBox(width: 8),
Text(
"Добавить фото",
selectionColor: appTheme.blueA400,
),
],
),
),
if (state.image != null)
ClipRRect(
borderRadius: BorderRadius.circular(16),
child: Image(
image: FileImage(
File(state.image!.path),
),
),
)
],
),
);
}
addImage(BuildContext context, bool isLoading) {
if (!isLoading) {
return () async {

View File

@@ -81,9 +81,9 @@ class BlogPostCreateBloc extends Bloc<BlogPostCreateEvent, BlogPostCreateState>
String url = baseUrl + '/api/blogPosts/create';
FormData formData = FormData.fromMap({
'title': title ?? '',
'description': description ?? '',
'youtube_url': youtubeURL ?? '',
'title': title,
'description': description,
'youtube_url': youtubeURL,
'state': 'active',
'image': await MultipartFile.fromFile(image!.path, filename: image.name),
});

View File

@@ -47,6 +47,7 @@ dependencies:
location: ^7.0.0
syncfusion_flutter_charts: ^26.2.8
table_calendar: ^3.1.2
flutter_keyboard_visibility: ^6.0.0
dev_dependencies:
build_runner: ^2.4.11