feat(chat): API data fetched and added to the screen

This commit is contained in:
2024-04-25 15:33:11 +03:00
parent f5ac02c020
commit c7c8a52786
4 changed files with 50 additions and 50 deletions

View File

@@ -1,7 +1,9 @@
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:hive/hive.dart';
import 'package:http/http.dart' as http;
import 'package:intl/intl.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'dart:convert';
import '/core/app_export.dart';
@@ -17,29 +19,29 @@ class ChatSelectBloc extends Bloc<ChatSelectEvent, ChatSelectState> {
ChatSelectBloc(ChatSelectState initialState) : super(initialState) {
on<ChatSelectInitialEvent>(_onInitialize);
}
List<ChatItemModel> fillChatEntryList() {
return [
ChatItemModel(
userImage: ImageConstant.imgLogo,
username: "Ксения Обрамова",
dateText: "9/29",
greetingText: "Привет, хорошая статья"),
ChatItemModel(
userImage: ImageConstant.imgLogo,
username: "Ксения Обрамова",
dateText: "9/29",
greetingText: "Привет, хорошая статья"),
ChatItemModel(
userImage: ImageConstant.imgLogo,
username: "Данил Иванов",
dateText: "9/29",
greetingText: "Посоветуй пожалуйста рецепт..."),
ChatItemModel(
userImage: ImageConstant.imgLogo,
username: "Данил Иванов",
dateText: "9/29",
greetingText: "Посоветуй пожалуйста рецепт...")
];
List<ChatItemModel> fillChatEntryList(List<dynamic> chatRooms) {
List<ChatItemModel> chats = [];
for (int i = 0; i < chatRooms.length; i++) {
String userName;
String userAvatarPath;
if (chatRooms[i]['last_message']['is_author_from_user']) {
userName = chatRooms[i]['users']['from_chat_user']['name'];
userAvatarPath =
chatRooms[i]['users']['from_chat_user']['profile_photo_path'] ?? '';
} else {
userName = chatRooms[i]['users']['to_chat_user']['name'];
userAvatarPath =
chatRooms[i]['users']['to_chat_user']['profile_photo_path'] ?? '';
}
chats.add(ChatItemModel(
userImage: userAvatarPath,
username: userName,
dateText: DateFormat('dd.MM').format(
DateTime.parse(chatRooms[i]['last_message']['updated_at'])),
greetingText: chatRooms[i]['last_message']['data'] ?? ''));
}
return chats;
}
Future<void> _onInitialize(
@@ -62,6 +64,7 @@ class ChatSelectBloc extends Bloc<ChatSelectEvent, ChatSelectState> {
);
if (response.statusCode == 200) {
final data = jsonDecode(response.body);
print(data['chatRooms']);
if (data is Map<String, dynamic> && data.containsKey('chatRooms')) {
if (state is ChatSelectLoadedState) {
var currentState = state as ChatSelectLoadedState;
@@ -71,7 +74,8 @@ class ChatSelectBloc extends Bloc<ChatSelectEvent, ChatSelectState> {
));
} else {
emit(ChatSelectLoadedState(
chatRooms: data['chatRooms'],
chatRooms: fillChatEntryList(data['chatRooms']),
//chatRooms: [],
));
}
} else {

View File

@@ -76,7 +76,7 @@ class ChatSelectScreen extends StatelessWidget {
hintText: "Поиск",
),
SizedBox(height: 20.v),
_buildChatList(context),
_buildChatList(context, state),
ElevatedButton(
onPressed: (() => print(state.chatRooms)),
child: Text("asdasd"))
@@ -90,25 +90,19 @@ class ChatSelectScreen extends StatelessWidget {
}
}
_buildChatList(BuildContext context) {
return BlocSelector<ChatSelectBloc, ChatSelectState, ChatSelectModel?>(
selector: (state) => state.chatSelectModelObj,
builder: (context, chatSelectModelObj) {
return ListView.separated(
shrinkWrap: true,
separatorBuilder: (context, index) {
return SizedBox(
height: 10.v,
);
},
itemCount: chatSelectModelObj?.userprofileItemList.length ?? 0,
itemBuilder: (context, index) {
ChatItemModel model =
chatSelectModelObj?.userprofileItemList[index] ?? ChatItemModel();
return UserprofileItemWidget(
model,
);
},
_buildChatList(BuildContext context, ChatSelectLoadedState state) {
return ListView.separated(
shrinkWrap: true,
separatorBuilder: (context, index) {
return SizedBox(
height: 10.v,
);
},
itemCount: state.chatRooms?.length ?? 0,
itemBuilder: (context, index) {
ChatItemModel model = state.chatRooms?[index] ?? ChatItemModel();
return UserprofileItemWidget(
model,
);
},
);

View File

@@ -6,15 +6,15 @@ import 'package:eatvio_app/presentation/chat/chat_select_screen/models/chat_entr
/// This class defines the variables used in the [chat_select_screen],
/// and is typically used to hold data that is passed between different parts of the application.
class ChatSelectModel extends Equatable {
ChatSelectModel({this.userprofileItemList = const []});
ChatSelectModel({this.chatList = const []});
List<ChatItemModel> userprofileItemList;
ChatSelectModel copyWith({List<ChatItemModel>? userprofileItemList}) {
List<ChatItemModel> chatList;
ChatSelectModel copyWith({List<ChatItemModel>? chatList}) {
return ChatSelectModel(
userprofileItemList: userprofileItemList ?? this.userprofileItemList,
chatList: chatList ?? this.chatList,
);
}
@override
List<Object?> get props => [userprofileItemList];
List<Object?> get props => [chatList];
}

View File

@@ -1,6 +1,7 @@
// ignore_for_file: must_be_immutable
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:eatvio_app/core/app_export.dart';
import 'package:eatvio_app/presentation/chat/chat_select_screen/models/chat_entry.dart';
@@ -24,7 +25,8 @@ class UserprofileItemWidget extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
CustomImageView(
imagePath: userprofileItemModelObj.userImage!,
imagePath:
dotenv.env['STORAGE_URL']! + userprofileItemModelObj.userImage!,
height: 60.adaptSize,
width: 60.adaptSize,
radius: BorderRadius.circular(