feat(feed): refactor creating post

This commit is contained in:
mso7nueh
2024-08-08 16:40:44 +03:00
parent f4c6aebdff
commit 6620ed5337
8 changed files with 908 additions and 56 deletions

View File

@@ -49,7 +49,7 @@ class _BlogPostCreateScreenState extends State<BlogPostCreateScreen> {
_buildBlogPostCreate(BuildContext context, BlogPostCreateLoadedState state) {
return Scaffold(
resizeToAvoidBottomInset: false,
resizeToAvoidBottomInset: true,
appBar: AppBar(),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
floatingActionButton: SizedBox(
@@ -62,8 +62,12 @@ class _BlogPostCreateScreenState extends State<BlogPostCreateScreen> {
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
backgroundColor: appTheme.blueA400,
onPressed: () => _submitForm(context, state.title!.text, state.description!.text, state.youtubeURL!.text),
backgroundColor: state.image != null || state.title!.text.isNotEmpty || state.description!.text.isNotEmpty || state.youtubeURL!.text.isNotEmpty
? appTheme.blueA400
: Colors.grey,
onPressed: state.image != null || state.title!.text.isNotEmpty || state.description!.text.isNotEmpty || state.youtubeURL!.text.isNotEmpty
? () => _submitForm(context, state.title!.text, state.description!.text, state.youtubeURL!.text)
: null,
label: Text(
'Создать',
style: theme.textTheme.titleSmall,
@@ -75,32 +79,29 @@ class _BlogPostCreateScreenState extends State<BlogPostCreateScreen> {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (state.youtubeURL!.text.isEmpty) Text('Изображение поста'),
if (state.youtubeURL!.text.isEmpty) SizedBox(height: 8),
if (state.youtubeURL!.text.isEmpty) _buildAddPhoto(context, state),
SizedBox(height: 16),
Text('Заголовок'),
SizedBox(height: 8),
CustomTextFormField(
controller: state.title,
validator: _validateTitle,
),
SizedBox(height: 16),
Text('Описание'),
SizedBox(height: 8),
CustomTextFormField(
controller: state.description,
validator: _validateDescription,
maxLines: 6,
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,
validator: _validateYouTubeLink,
),
SizedBox(height: 16),
Text('Изображение поста'),
SizedBox(height: 8),
_buildAddPhoto(context, state),
SizedBox(height: 80),
],
),
@@ -110,20 +111,7 @@ class _BlogPostCreateScreenState extends State<BlogPostCreateScreen> {
}
_submitForm(BuildContext context, String title, String description, String youtubeURL) async {
String? titleError = _validateTitle(title);
String? descriptionError = _validateDescription(description);
String? youtubeURLError = _validateYouTubeLink(youtubeURL);
if (titleError != null || descriptionError != null || youtubeURLError != null) {
// Display error messages or handle invalid input as needed
// For example, show a snackbar with the error message
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(titleError ?? descriptionError ?? youtubeURLError!)),
);
} else {
// Proceed with form submission
context.read<BlogPostCreateBloc>().add(CreateBlogPost());
}
context.read<BlogPostCreateBloc>().add(CreateBlogPost());
}
_buildAddPhoto(BuildContext context, BlogPostCreateLoadedState state) {
@@ -179,31 +167,3 @@ addImage(BuildContext context, bool isLoading) {
};
}
}
String? _validateTitle(value) {
if (value!.isEmpty) {
return 'Пожалуйста, введите заголовок';
}
return null;
}
String? _validateDescription(value) {
if (value!.isEmpty) {
return 'Пожалуйста, введите описание';
}
return null;
}
String? _validateYouTubeLink(value) {
if (value!.isEmpty) {
return 'Пожалуйста, введите ссылку';
} else if (!_isValidUrl(value)) {
return 'Пожалуйста, введите корректную ссылку';
}
return null;
}
bool _isValidUrl(String url) {
final RegExp urlRegExp = new RegExp(r"^(http|https):\/\/([\w-]+\.)+[\w-]+(\/[\w-./?%&=]*)?$");
return urlRegExp.hasMatch(url);
}