forked from eatvio/eatvio_app
feat(app): Initial layouts for subscribers, feed display fix, feed photo upload front end
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
//import 'package:eatvio_app/presentation/auth/auth_screen/auth_screen.dart';
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:eatvio_app/app_router.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import '/core/app_export.dart';
|
||||
import 'package:eatvio_app/presentation/users/subscribers_screen/models/subscribers_model.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'dart:convert';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
part 'subscribers_event.dart';
|
||||
part 'subscribers_state.dart';
|
||||
|
||||
/// A bloc that manages the state of a Subscribers according to the event that is dispatched to it.
|
||||
class SubscribersBloc extends Bloc<SubscribersEvent, SubscribersState> {
|
||||
SubscribersBloc(SubscribersState initialState) : super(initialState) {
|
||||
on<SubscribersInitialEvent>(_onInitialize);
|
||||
}
|
||||
|
||||
Future<void> _onInitialize(
|
||||
SubscribersInitialEvent event,
|
||||
Emitter<SubscribersState> emit,
|
||||
) async {
|
||||
try {
|
||||
final box = await Hive.openBox('token');
|
||||
final token = box.get('token');
|
||||
if (token == null) {
|
||||
AppRouter.navigatorKey.currentContext?.goNamed('authScreen');
|
||||
}
|
||||
final response = await http.get(
|
||||
Uri.parse(dotenv.env['SERVER_ROOT']! + '/profile/subscribers'),
|
||||
headers: {
|
||||
'Authorization': 'Bearer $token',
|
||||
},
|
||||
);
|
||||
print('Got response');
|
||||
print(response.statusCode);
|
||||
print(response.body);
|
||||
if (response.statusCode == 200) {
|
||||
print('Got 200');
|
||||
final data = jsonDecode(response.body);
|
||||
if (data is List<dynamic>) {
|
||||
print('Got List');
|
||||
if (state is SubscribersLoadedState) {
|
||||
print('Got loaded');
|
||||
var currentState = state as SubscribersLoadedState;
|
||||
emit((currentState).copyWith(
|
||||
subscribers: data,
|
||||
myProfileModelObj: currentState.myProfileModelObj,
|
||||
));
|
||||
} else {
|
||||
print('Got unloaded');
|
||||
emit(SubscribersLoadedState(
|
||||
subscribers: data,
|
||||
));
|
||||
}
|
||||
} else {
|
||||
// Один или несколько ключевых полей отсутствуют, обрабатываем ошибку
|
||||
emit(SubscribersErrorState('Неверный формат ответа API.'));
|
||||
}
|
||||
} else {
|
||||
// Статус ответа не равен 200, обрабатываем ошибку
|
||||
emit(SubscribersErrorState('Ошибка сервера: ${response.statusCode}'));
|
||||
}
|
||||
} catch (e) {
|
||||
// Ошибка при декодировании JSON или другая ошибка выполнения
|
||||
emit(SubscribersErrorState('Ошибка при получении данных ленты: $e'));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// ignore_for_file: must_be_immutable
|
||||
|
||||
part of 'subscribers_bloc.dart';
|
||||
|
||||
/// Abstract class for all events that can be dispatched from the
|
||||
///Subscribers widget.
|
||||
///
|
||||
/// Events must be immutable and implement the [Equatable] interface.
|
||||
@immutable
|
||||
abstract class SubscribersEvent extends Equatable {}
|
||||
|
||||
/// Event that is dispatched when the Subscribers widget is first created.
|
||||
class SubscribersInitialEvent extends SubscribersEvent {
|
||||
@override
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
// ignore_for_file: must_be_immutable
|
||||
|
||||
part of 'subscribers_bloc.dart';
|
||||
|
||||
class SubscribersState extends Equatable {
|
||||
const SubscribersState({this.myProfileModelObj});
|
||||
|
||||
final SubscribersModel? myProfileModelObj;
|
||||
|
||||
@override
|
||||
List<Object?> get props => [myProfileModelObj];
|
||||
}
|
||||
|
||||
class SubscribersInitialState extends SubscribersState {
|
||||
SubscribersInitialState({required SubscribersModel myProfileModelObj})
|
||||
: super(myProfileModelObj: myProfileModelObj);
|
||||
}
|
||||
|
||||
/// Represents the state of Subscribers in the application.
|
||||
class SubscribersLoadedState extends SubscribersState {
|
||||
SubscribersLoadedState({this.myProfileModelObj, this.subscribers})
|
||||
: super(myProfileModelObj: null);
|
||||
|
||||
SubscribersModel? myProfileModelObj;
|
||||
dynamic subscribers;
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
myProfileModelObj,
|
||||
subscribers,
|
||||
];
|
||||
SubscribersLoadedState copyWith({
|
||||
SubscribersModel? myProfileModelObj,
|
||||
dynamic subscribers,
|
||||
}) {
|
||||
return SubscribersLoadedState(
|
||||
myProfileModelObj: myProfileModelObj ?? this.myProfileModelObj,
|
||||
subscribers: subscribers ?? this.subscribers,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class SubscribersErrorState extends SubscribersState {
|
||||
final String errorMessage;
|
||||
|
||||
const SubscribersErrorState(this.errorMessage);
|
||||
|
||||
@override
|
||||
List<Object> get props => [errorMessage];
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// ignore_for_file: must_be_immutable
|
||||
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
/// This class defines the variables used in the [k0_page],
|
||||
/// and is typically used to hold data that is passed between different parts of the application.
|
||||
class SubscribersModel extends Equatable {
|
||||
SubscribersModel();
|
||||
|
||||
SubscribersModel copyWith() {
|
||||
return SubscribersModel();
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
@@ -0,0 +1,213 @@
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
import 'bloc/subscribers_bloc.dart';
|
||||
import 'models/subscribers_model.dart';
|
||||
import 'package:eatvio_app/core/app_export.dart';
|
||||
import 'package:eatvio_app/presentation/feed/feed_screen/widgets/feed_search_view.dart';
|
||||
import 'package:eatvio_app/widgets/app_bar/appbar_subtitle.dart';
|
||||
import 'package:eatvio_app/widgets/app_bar/appbar_leading_image.dart';
|
||||
import 'package:eatvio_app/widgets/app_bar/custom_app_bar.dart';
|
||||
import 'package:eatvio_app/widgets/custom_outlined_button.dart';
|
||||
import 'package:cached_network_image/cached_network_image.dart';
|
||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class SubscribersScreen extends StatelessWidget {
|
||||
static Widget builder(BuildContext context) {
|
||||
return BlocProvider<SubscribersBloc>(
|
||||
create: (context) => SubscribersBloc(
|
||||
SubscribersState(myProfileModelObj: SubscribersModel()))
|
||||
..add(SubscribersInitialEvent()),
|
||||
child: SubscribersScreen());
|
||||
}
|
||||
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<SubscribersBloc, SubscribersState>(
|
||||
builder: (context, state) {
|
||||
if (state is SubscribersInitialState) {
|
||||
// Это начальное состояние, данные ещё не загружены.
|
||||
return CircularProgressIndicator(
|
||||
color: Colors.white,
|
||||
backgroundColor: Colors.white,
|
||||
);
|
||||
} else if (state is SubscribersLoadedState) {
|
||||
// Данные загружены, можно строить экран.
|
||||
return _buildSubscribersScreen(context, state);
|
||||
} else {
|
||||
// Другие состояния, если они есть.
|
||||
return Container(); // Пустой контейнер для необработанных состояний.
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
_buildSubscribersScreen(BuildContext context, SubscribersLoadedState state) {
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.grey[200],
|
||||
resizeToAvoidBottomInset: true,
|
||||
body: SingleChildScrollView(
|
||||
child: Container(
|
||||
width: double.maxFinite,
|
||||
decoration: BoxDecoration(color: Colors.grey[200]),
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
SizedBox(height: 10),
|
||||
Container(
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: const BorderRadius.all(Radius.circular(12)),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.grey.withOpacity(0.5),
|
||||
spreadRadius: 2,
|
||||
blurRadius: 5,
|
||||
offset: Offset(0, 3),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: CustomAppBar(
|
||||
height: 54.v,
|
||||
leadingWidth: 28.h,
|
||||
leading: AppbarLeadingImage(
|
||||
imagePath: ImageConstant.imgArrowLeft,
|
||||
margin:
|
||||
EdgeInsets.only(left: 16.h, top: 15.v, bottom: 15.v),
|
||||
onTap: () => GoRouter.of(context).pop(),
|
||||
),
|
||||
title: Padding(
|
||||
padding: EdgeInsets.only(left: 20.h),
|
||||
child: Row(
|
||||
children: [
|
||||
AppbarSubtitle(
|
||||
text: "Мои подписчики",
|
||||
margin: EdgeInsets.only(
|
||||
left: 80.h, top: 12.v, bottom: 9.v),
|
||||
),
|
||||
Spacer(),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(right: 20.0),
|
||||
child: GestureDetector(
|
||||
child: Icon(Icons.create_outlined)),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
_buildSearch(context),
|
||||
SizedBox(height: 16),
|
||||
_buildSubscribersList(context, state),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
_buildSearch(BuildContext context) {
|
||||
return Container(
|
||||
margin: EdgeInsets.only(bottom: 10),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: const BorderRadius.all(Radius.circular(12)),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.grey.withOpacity(0.5),
|
||||
spreadRadius: 2,
|
||||
blurRadius: 5,
|
||||
offset: Offset(0, 3),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(12.0),
|
||||
child: Column(
|
||||
children: [
|
||||
CustomSearchView(
|
||||
hintText: "Поиск",
|
||||
),
|
||||
SizedBox(height: 10),
|
||||
Row(
|
||||
children: [
|
||||
Flexible(
|
||||
flex: 5,
|
||||
child: CustomOutlinedButton(
|
||||
text: "Найти",
|
||||
buttonStyle: CustomButtonStyles.outlinePrimaryTL15,
|
||||
),
|
||||
),
|
||||
SizedBox(width: 8), // Add some spacing between the buttons
|
||||
Flexible(
|
||||
flex: 1,
|
||||
child: CustomOutlinedButton(
|
||||
text: "+",
|
||||
onPressed: () =>
|
||||
GoRouter.of(context).goNamed('blogPostCreateScreen'),
|
||||
buttonStyle: CustomButtonStyles.outlinePrimaryTL15,
|
||||
),
|
||||
),
|
||||
],
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
_buildSubscribersList(BuildContext context, SubscribersLoadedState state) {
|
||||
List<Widget> subscriberWidgets = [];
|
||||
|
||||
if (state.subscribers != null) {
|
||||
for (var subscriber in state.subscribers!) {
|
||||
subscriberWidgets.add(_buildSubscriberEntry(subscriber, context));
|
||||
}
|
||||
}
|
||||
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: subscriberWidgets,
|
||||
);
|
||||
}
|
||||
|
||||
_buildSubscriberEntry(BuildContext context, subscriber) {
|
||||
return Container(
|
||||
margin: EdgeInsets.only(bottom: 10),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: const BorderRadius.all(Radius.circular(12)),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.grey.withOpacity(0.5),
|
||||
spreadRadius: 2,
|
||||
blurRadius: 5,
|
||||
offset: Offset(0, 3),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: ListTile(
|
||||
leading: CachedNetworkImage(
|
||||
imageUrl: dotenv.env['STORAGE_URL']! + subscriber['image'],
|
||||
imageBuilder: (context, imageProvider) => CircleAvatar(
|
||||
backgroundImage: imageProvider,
|
||||
radius: 25,
|
||||
),
|
||||
placeholder: (context, url) => CircularProgressIndicator(),
|
||||
errorWidget: (context, url, error) => CircleAvatar(
|
||||
child: Icon(Icons.person),
|
||||
backgroundColor: Colors.white,
|
||||
radius: 25,
|
||||
),
|
||||
),
|
||||
title: Text(
|
||||
subscriber?['title'] ?? '',
|
||||
style: CustomTextStyles.titleMediumSemiBold,
|
||||
),
|
||||
subtitle: Text(subscriber?['description'] ?? ''),
|
||||
trailing: GestureDetector(child: Icon(Icons.edit)),
|
||||
));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user