Skip to content

Overview

  • File: lib/views/messages_page.dart

The MessagesPage is a Flutter widget that manages the chat list in the HVC Mobile application. It provides functionality for viewing, searching, and interacting with chat conversations, including navigation to chat details and chat selection.

Class Structure

Class Definition

dart
class MessagesPage extends StatefulWidget {
  static const String routeName = '/MessagesPage';
  MessagesPage({Key? key}) : super(key: key);

  @override
  _MessagesPageState createState() => _MessagesPageState();
}

Controllers

dart
RealTimeController realTimeController = Get.find();
final MessagesController messagesController = Get.find();

State Variables

dart
final TextEditingController searchCtrl = TextEditingController();

Dependencies

Required Packages

  • flutter/material.dart - Core Flutter UI components
  • get/get.dart - State management and routing
  • infinite_scroll_pagination - Infinite scrolling functionality

Internal Dependencies

  • messages_controller.dart - Chat management logic
  • real_time_controller.dart - Real-time communication
  • hlk_helpers/ - Custom helper utilities
  • models/ - Data models
  • shared/ - Shared constants and configurations
  • widgets/ - Custom widgets

State Management

Initialization

dart
@override
void initState() {
  super.initState();
  messagesController.getChatListOnline();
}

Reactive State

dart
Obx(() => messagesController.loadingChat.isTrue &&
        messagesController.chatList.isEmpty
    ? const Center(child: CircularProgressIndicator())
    : Column(...))

Chat Management

Chat Selection

dart
onSelected: (bool? value) {
  setState(() {
    chat.isSelected = value!;
    if (value) {
      messagesController.selectedChat.add(chat);
    } else {
      messagesController.selectedChat.remove(chat);
    }
  });
}

Chat Navigation

dart
onTapHandler: () async {
  if (!hasPermission(HvcPermissions.Chat)) {
    HlkDialog.showErrorSnackBar('Not permitted to perform action');
    return;
  }
  messagesController.selectedGroupId.value = chat.id;
  await messagesController.reloadMessages();
  Get.to(ContactMessageDetailsPage(
    contact: chat.getContact(),
    isForMessage: true,
  ));
  Future.delayed(
    const Duration(seconds: 2),
    () => messagesController.getMessagesOnline()
  );
}

UI Components

dart
if (messagesController.showSearchBox.value)
  Padding(
    padding: const EdgeInsets.only(left: 24, right: 12, top: 10),
    child: Row(
      children: [
        Expanded(
          child: TextFormField(
            controller: searchCtrl,
            validator: requiredValidator,
            decoration: getInputDecoration('Search Chat', false),
          ),
        ),
        IconButton(
          onPressed: () async {
            setState(() {
              messagesController.showSearchBox.value = false;
              messagesController.name.value = searchCtrl.text;
            });
            await messagesController.reloadChats();
            searchCtrl.clear();
          },
          icon: const Icon(Icons.search),
        ),
      ],
    ),
  )

Chat List

dart
PagedListView<int, ChatModel>(
  pagingController: messagesController.chatPagingController,
  builderDelegate: PagedChildBuilderDelegate<ChatModel>(
    itemBuilder: (context, chat, index) => ChatItem(...),
    noItemsFoundIndicatorBuilder: (context) => NoItemLoader(...),
  ),
)

Chat Item

dart
ChatItem(
  chatModel: chat,
  onTapHandler: () async { ... },
  onSelected: (bool? value) { ... },
  showSelector: messagesController.showSelector,
)

Search Functionality

Search Implementation

dart
onPressed: () async {
  setState(() {
    messagesController.showSearchBox.value = false;
    messagesController.name.value = searchCtrl.text;
  });
  await messagesController.reloadChats();
  searchCtrl.clear();
}

Search Box Toggle

dart
if (messagesController.showSearchBox.value)
  // Show search box
else
  // Hide search box

Permission Handling

Permission Checks

dart
if (!hasPermission(HvcPermissions.Chat)) {
  HlkDialog.showErrorSnackBar('Not permitted to perform action');
  return;
}

Permission-Protected Actions

  1. Chat Navigation
  2. Chat List Refresh
  3. Chat List Loading

Released under the MIT License.