forked from eatvio/eatvio_app
fix(blogPost): create
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
import 'package:eatvio_app/old/feed/blog_post_create_screen/bloc/blog_post_create_bloc.dart';
|
||||
import 'package:eatvio_app/features/blog_post_create/bloc/blog_post_create_cubit/blog_post_create_cubit.dart';
|
||||
import 'package:eatvio_app/features/blog_post_create/bloc/blog_post_create_cubit/blog_post_create_state.dart';
|
||||
import 'package:eatvio_app/locator_service.dart';
|
||||
import 'package:eatvio_app/theme/app_decoration.dart';
|
||||
import 'package:eatvio_app/theme/theme_helper.dart';
|
||||
import 'package:eatvio_app/widgets/custom_text_form_field.dart';
|
||||
@@ -8,19 +10,27 @@ import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
|
||||
class BlogPostCreateScreen extends StatefulWidget {
|
||||
const BlogPostCreateScreen({Key? key}) : super(key: key);
|
||||
const BlogPostCreateScreen({
|
||||
Key? key,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<BlogPostCreateScreen> createState() => _BlogPostCreateScreenState();
|
||||
}
|
||||
|
||||
class _BlogPostCreateScreenState extends State<BlogPostCreateScreen> {
|
||||
final blogPostCreateBloc = BlogPostCreateBloc(BlogPostCreateState())..add(BlogPostCreateInitialEvent());
|
||||
final blogPostCubit = sl<BlogPostCreateCubit>();
|
||||
bool shrinkImage = false;
|
||||
late StreamSubscription<bool> keyboardSubscription;
|
||||
XFile? image;
|
||||
final titleController = TextEditingController();
|
||||
final descriptionController = TextEditingController();
|
||||
final youtubeUrlController = TextEditingController();
|
||||
bool isLoading = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -40,26 +50,15 @@ class _BlogPostCreateScreenState extends State<BlogPostCreateScreen> {
|
||||
}
|
||||
|
||||
Widget build(BuildContext context) {
|
||||
return BlocProvider<BlogPostCreateBloc>(
|
||||
create: (BuildContext context) => blogPostCreateBloc,
|
||||
child: BlocBuilder<BlogPostCreateBloc, BlogPostCreateState>(
|
||||
builder: (context, state) {
|
||||
if (state is BlogPostCreateInitialState) {
|
||||
// Это начальное состояние, данные ещё не загружены.
|
||||
return Container(
|
||||
color: Colors.white,
|
||||
);
|
||||
} else if (state is BlogPostCreateLoadedState) {
|
||||
// Данные загружены, можно строить экран.
|
||||
return _buildBlogPostCreate(context, state);
|
||||
} else if (state is BlogPostCreateErrorState) {
|
||||
// Произошла ошибка при загрузке данных.
|
||||
return Center(child: Text(state.errorMessage));
|
||||
} else {
|
||||
// Другие состояния, если они есть.
|
||||
return Container(); // Пустой контейнер для необработанных состояний.
|
||||
return BlocProvider<BlogPostCreateCubit>(
|
||||
create: (BuildContext context) => blogPostCubit,
|
||||
child: BlocListener<BlogPostCreateCubit, BlogPostCreateState>(
|
||||
listener: (BuildContext context, BlogPostCreateState state) {
|
||||
if (state is BlogPostCreateStateLoaded) {
|
||||
GoRouter.of(context).pop();
|
||||
}
|
||||
},
|
||||
child: _buildBlogPostCreate(),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -72,8 +71,8 @@ class _BlogPostCreateScreenState extends State<BlogPostCreateScreen> {
|
||||
CupertinoActionSheetAction(
|
||||
onPressed: () async {
|
||||
final picker = ImagePicker();
|
||||
XFile? image = await picker.pickImage(source: ImageSource.camera);
|
||||
if (image != null) blogPostCreateBloc.add(UploadImages(image));
|
||||
image = await picker.pickImage(source: ImageSource.camera);
|
||||
setState(() {});
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
child: const Row(
|
||||
@@ -87,8 +86,8 @@ class _BlogPostCreateScreenState extends State<BlogPostCreateScreen> {
|
||||
CupertinoActionSheetAction(
|
||||
onPressed: () async {
|
||||
final picker = ImagePicker();
|
||||
XFile? image = await picker.pickImage(source: ImageSource.gallery);
|
||||
if (image != null) blogPostCreateBloc.add(UploadImages(image));
|
||||
image = await picker.pickImage(source: ImageSource.gallery);
|
||||
setState(() {});
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
child: const Row(
|
||||
@@ -109,7 +108,7 @@ class _BlogPostCreateScreenState extends State<BlogPostCreateScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
_buildBlogPostCreate(BuildContext context, BlogPostCreateLoadedState state) {
|
||||
_buildBlogPostCreate() {
|
||||
return Scaffold(
|
||||
resizeToAvoidBottomInset: true,
|
||||
appBar: AppBar(),
|
||||
@@ -125,13 +124,27 @@ class _BlogPostCreateScreenState extends State<BlogPostCreateScreen> {
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
),
|
||||
backgroundColor: appTheme.blueA400,
|
||||
onPressed: () => _submitForm(context, state.title!.text, state.description!.text, state.youtubeURL!.text),
|
||||
label: Text(
|
||||
'Создать',
|
||||
style: TextStyle(
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
label: isLoading
|
||||
? CircularProgressIndicator()
|
||||
: Text(
|
||||
'Создать',
|
||||
style: TextStyle(
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
onPressed: isLoading
|
||||
? null
|
||||
: () {
|
||||
setState(() {
|
||||
isLoading = true;
|
||||
});
|
||||
blogPostCubit.createBlogPost(
|
||||
title: titleController.text,
|
||||
description: descriptionController.text,
|
||||
youtubeURL: youtubeUrlController.text,
|
||||
image: image,
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
body: SingleChildScrollView(
|
||||
@@ -143,26 +156,26 @@ class _BlogPostCreateScreenState extends State<BlogPostCreateScreen> {
|
||||
Text('Заголовок'),
|
||||
SizedBox(height: 8),
|
||||
CustomTextFormField(
|
||||
controller: state.title,
|
||||
controller: titleController,
|
||||
),
|
||||
SizedBox(height: 16),
|
||||
Text('Описание'),
|
||||
SizedBox(height: 8),
|
||||
CustomTextFormField(
|
||||
controller: state.description,
|
||||
controller: descriptionController,
|
||||
maxLines: 6,
|
||||
minLines: 4,
|
||||
),
|
||||
SizedBox(height: 16),
|
||||
if (state.youtubeURL!.text.isEmpty) Text('Изображение поста'),
|
||||
if (state.youtubeURL!.text.isEmpty) SizedBox(height: 8),
|
||||
if (state.youtubeURL!.text.isEmpty) _buildAddPhoto(state),
|
||||
if (youtubeUrlController.text.isEmpty) Text('Изображение поста'),
|
||||
if (youtubeUrlController.text.isEmpty) SizedBox(height: 8),
|
||||
if (youtubeUrlController.text.isEmpty) _buildAddPhoto(),
|
||||
SizedBox(height: 16),
|
||||
if (state.image == null) Text('Ссылка на видео'),
|
||||
if (state.image == null) SizedBox(height: 8),
|
||||
if (state.image == null)
|
||||
if (image == null) Text('Ссылка на видео'),
|
||||
if (image == null) SizedBox(height: 8),
|
||||
if (image == null)
|
||||
CustomTextFormField(
|
||||
controller: state.youtubeURL,
|
||||
controller: youtubeUrlController,
|
||||
),
|
||||
SizedBox(height: 80),
|
||||
],
|
||||
@@ -171,14 +184,14 @@ class _BlogPostCreateScreenState extends State<BlogPostCreateScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
_buildAddPhoto(BlogPostCreateLoadedState state) {
|
||||
bool isLoading = state.isLoading;
|
||||
|
||||
_buildAddPhoto() {
|
||||
return GestureDetector(
|
||||
onTap: addImage(context, isLoading),
|
||||
onTap: () {
|
||||
_showImagePicker();
|
||||
},
|
||||
child: Stack(
|
||||
children: [
|
||||
if (state.image == null)
|
||||
if (image == null)
|
||||
AnimatedContainer(
|
||||
height: shrinkImage ? 52 : 128,
|
||||
curve: Curves.easeIn,
|
||||
@@ -202,7 +215,7 @@ class _BlogPostCreateScreenState extends State<BlogPostCreateScreen> {
|
||||
],
|
||||
),
|
||||
),
|
||||
if (state.image != null)
|
||||
if (image != null)
|
||||
Container(
|
||||
width: shrinkImage ? 52 : MediaQuery.of(context).size.width - 32,
|
||||
child: ClipRRect(
|
||||
@@ -210,7 +223,7 @@ class _BlogPostCreateScreenState extends State<BlogPostCreateScreen> {
|
||||
child: Image(
|
||||
image: FileImage(
|
||||
File(
|
||||
state.image!.path,
|
||||
image!.path,
|
||||
),
|
||||
),
|
||||
),
|
||||
@@ -220,21 +233,4 @@ class _BlogPostCreateScreenState extends State<BlogPostCreateScreen> {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
addImage(BuildContext context, bool isLoading) {
|
||||
if (!isLoading) {
|
||||
return () {
|
||||
_showImagePicker();
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_submitForm(
|
||||
BuildContext context,
|
||||
String title,
|
||||
String description,
|
||||
String youtubeURL,
|
||||
) async {
|
||||
context.read<BlogPostCreateBloc>().add(CreateBlogPost());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user