Skip to content

Overview

  • File: lib/views/main_page.dart

The MainPage is a Flutter widget that serves as the main navigation hub for the HVC Mobile application. It implements a bottom navigation bar with multiple tabs for different sections of the app and handles various user interactions including contact selection, event management, and messaging.

Class Structure

Class Definition

dart
class MainPage extends StatefulWidget {
  static const String routeName = '/MainPage';
  const MainPage({super.key});

  @override
  State<MainPage> createState() => _MainPageState();
}

Controllers

dart
final AuthController authController = Get.find();
final ContactsController contactsController = Get.find();
final EventsController eventsController = Get.find();
final MessagesController messagesController = Get.find();
final RealTimeController realTimeController = Get.find();

State Variables

dart
final UserData user = UserData.fromMap(userData);
final RxInt currentPageIndex = 0.obs;
final List<String> page = ["Home", "Events", "Contacts", "Messages"];

Dependencies

Required Packages

  • flutter/material.dart - Core Flutter UI components
  • get/get.dart - State management and routing
  • onesignal_flutter/onesignal_flutter.dart - Push notifications
  • permission_handler/permission_handler.dart - Permission management

Internal Dependencies

  • auth_controller.dart - Authentication logic
  • contacts_controller.dart - Contact management
  • events_controller.dart - Event management
  • messages_controller.dart - Message management
  • real_time_controller.dart - Real-time communication
  • hlk_helpers/ - Custom helper utilities
  • models/ - Data models
  • shared/ - Shared constants and configurations

State Management

Page Index Management

dart
final RxInt currentPageIndex = 0.obs;

void handlePageChange(int pageIndex) {
  if (pageIndex != 3) {
    isInChat.value = false;
  }
}

Selection State

dart
// Contact selection
contactsController.selected.isNotEmpty

// Event selection
eventsController.selected.isNotEmpty

// Message selection
messagesController.selectedChat.isNotEmpty

Bottom Navigation Tabs

dart
final List<BottomTabItem> tabList = [
  BottomTabItem(
    widget: HomePage(),
    icon: const Icon(Icons.home),
    tabTitle: 'Home',
  ),
  BottomTabItem(
    widget: EventsPage(),
    icon: const Icon(Icons.event),
    tabTitle: 'Events',
  ),
  BottomTabItem(
    widget: ContactsPage(),
    icon: const Icon(Icons.perm_contact_cal_sharp),
    tabTitle: 'Contacts',
  ),
  BottomTabItem(
    widget: MessagesPage(),
    icon: const Icon(Icons.message),
    tabTitle: 'Messages',
  ),
];

Page Change Handler

dart
onPageChanged: (int pageIndex) {
  logInfo('onPageChanged = $pageIndex');
  currentPageIndex.value = pageIndex;
  if (pageIndex != 3) {
    isInChat.value = false;
  }
  handlePageChange(pageIndex);
}

UI Components

AppBar

The AppBar is dynamically generated based on the current selection state:

  1. Contact Selection AppBar
dart
AppBar(
  leading: IconButton(
    onPressed: () => contactsController.releaseSelections(),
    icon: const Icon(Icons.close),
  ),
  title: Obx(() => Text('${contactsController.selected.length} selected')),
  actions: [
    // Group creation
    // Call action
    // Message action
    // Event action
  ],
)
  1. Event Selection AppBar
dart
AppBar(
  leading: IconButton(
    onPressed: () => eventsController.releaseSelections(),
    icon: const Icon(Icons.close),
  ),
  title: Obx(() => Text('${eventsController.selected.length} selected')),
  actions: [
    // Delete action
  ],
)
  1. Message Selection AppBar
dart
AppBar(
  leading: IconButton(
    onPressed: () => messagesController.releaseSelections(),
    icon: const Icon(Icons.close),
  ),
  title: Obx(() => Text('${messagesController.selectedChat.length} selected')),
  actions: [
    // Delete action
  ],
)

Action Icons

dart
appBarActionIcons: [
  // Event selector checkbox
  // Message selector checkbox
  // QR scanner
  // Search
  // Settings
  // Logout
]

Features

1. Contact Management

  • Contact selection
  • Group creation
  • Call initiation
  • Message sending

2. Event Management

  • Event selection
  • Event cancellation
  • Event creation

3. Messaging

  • Chat selection
  • Message management
  • Real-time updates

4. Authentication

  • Push notification registration
  • User validation
  • Logout functionality

Event Handling

Initialization

dart
@override
void initState() {
  super.initState();

  if (isLoggedIn()) {
    OneSignal.login(user.externalId);
  }
  if (isLoggedIn() && !user.isRealUser) {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      Get.offAllNamed(LoginPage.routeName);
    });
  }

  final int defaultPageIndex = page.contains(user.mobileDefaultPage)
      ? page.indexOf(user.mobileDefaultPage)
      : 0;

  currentPageIndex.value = defaultPageIndex;
  handlePageChange(defaultPageIndex);
}

Call Handling

dart
onPressed: () async {
  if (cUser == null) {
    HlkDialog.showErrorSnackBar('You need to login to make call');
    return;
  }
  final callDto = InitCallDto(
    from: cUser.userId,
    fromName: cUser.name,
    targets: contactsController.selected.map((Contact c) => c.id).toList(),
    title: 'Meeting with ${cUser.name}',
    source: 'Mobile',
    platform: Platform.isIOS ? 'iOS' : 'Android',
  );

  await validatePermission(
    Permission.camera,
    () async => await validatePermission(
      Permission.microphone,
      () async {
        await realTimeController.makeCall(
          callDto,
          null,
          true,
          getNumber(callDto.targets?.length) > 1
        );
      }
    )
  );
  contactsController.releaseSelections();
}

Message Handling

dart
onPressed: () async {
  final res = await realTimeController.callSendMessage(
    content: '',
    contentType: 'text',
    user: user,
    contacts: contactsController.selected.map((Contact c) => c.id).toList(),
  );

  if (res != null) {
    currentPageIndex.value = 3;
    await messagesController.getChatListOnline();
    // Handle message sending success
  }
}

Released under the MIT License.