build method

  1. @override
Widget build(
  1. BuildContext context
)
override

Describes the part of the user interface represented by this widget.

The framework calls this method when this widget is inserted into the tree in a given BuildContext and when the dependencies of this widget change (e.g., an InheritedWidget referenced by this widget changes). This method can potentially be called in every frame and should not have any side effects beyond building a widget.

The framework replaces the subtree below this widget with the widget returned by this method, either by updating the existing subtree or by removing the subtree and inflating a new subtree, depending on whether the widget returned by this method can update the root of the existing subtree, as determined by calling Widget.canUpdate.

Typically implementations return a newly created constellation of widgets that are configured with information from this widget's constructor and from the given BuildContext.

The given BuildContext contains information about the location in the tree at which this widget is being built. For example, the context provides the set of inherited widgets for this location in the tree. A given widget might be built with multiple different BuildContext arguments over time if the widget is moved around the tree or if the widget is inserted into the tree in multiple places at once.

The implementation of this method must only depend on:

If a widget's build method is to depend on anything else, use a StatefulWidget instead.

See also:

  • StatelessWidget, which contains the discussion on performance considerations.

Implementation

@override
Widget build(BuildContext context) {
  final styles = options.styles ?? const WaitingRoomModalStyleOptions();
  final mediaSize = MediaQuery.of(context).size;

  final defaultWidth = math.min(mediaSize.width * 0.8, 350.0);
  double modalWidth = styles.width ?? defaultWidth;
  if (styles.maxWidth != null) {
    modalWidth = math.min(modalWidth, styles.maxWidth!);
  }

  final defaultHeight = mediaSize.height * 0.65;
  double modalHeight = styles.height ?? defaultHeight;
  if (styles.maxHeight != null) {
    modalHeight = math.min(modalHeight, styles.maxHeight!);
  }

  final positionData = getModalPosition(
    GetModalPositionOptions(
      position: options.position,
      modalWidth: modalWidth,
      modalHeight: modalHeight,
      context: context,
    ),
  );

  final params = options.parameters.getUpdatedAllParams();
final baseList = params.filteredWaitingRoomList.isNotEmpty
      ? params.filteredWaitingRoomList
      : (params.waitingRoomList.isNotEmpty
          ? params.waitingRoomList
          : options.waitingRoomList);
  final waitingList = List<WaitingRoomParticipant>.from(baseList);
final fallbackCounter = params.waitingRoomCounter >
    options.waitingRoomCounter
  ? params.waitingRoomCounter
  : options.waitingRoomCounter;
final waitingCounter =
  waitingList.isNotEmpty ? waitingList.length : fallbackCounter;

  Future<void> handleResponse(
      WaitingRoomParticipant participant, bool accepted) async {
    await options.onWaitingRoomItemPress(
      options: RespondToWaitingOptions(
        participantId: participant.id,
        participantName: participant.name,
        updateWaitingList: params.updateWaitingRoomList,
        waitingList: params.waitingRoomList,
        roomName: params.roomName,
        socket: params.socket,
        type: accepted,
      ),
    );
  }

  final counterBadge = Container(
    padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
    decoration: styles.counterDecoration ??
        BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(10),
        ),
    child: Text(
      waitingCounter.toString(),
      style: styles.counterTextStyle ??
          const TextStyle(
            color: Colors.black,
            fontWeight: FontWeight.w600,
          ),
    ),
  );

  final headerTitle = options.title ??
      Text(
        'Waiting',
        style: styles.titleTextStyle ??
            const TextStyle(
              fontSize: 18,
              fontWeight: FontWeight.bold,
              color: Colors.black,
            ),
      );

  final header = Row(
    mainAxisAlignment: MainAxisAlignment.spaceBetween,
    crossAxisAlignment: CrossAxisAlignment.center,
    children: [
      Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          headerTitle,
          const SizedBox(width: 8),
          counterBadge,
        ],
      ),
      IconButton(
        onPressed: options.onWaitingRoomClose,
        icon: styles.closeIcon ??
            const Icon(
              Icons.close,
              size: 20,
              color: Colors.black,
            ),
        style: styles.closeButtonStyle,
      ),
    ],
  );

  final resolvedHeader = options.headerBuilder?.call(
        WaitingRoomModalHeaderContext(
          defaultHeader: header,
          counter: waitingCounter,
          onClose: options.onWaitingRoomClose,
        ),
      ) ??
      header;

  final searchField = TextField(
    decoration: styles.searchInputDecoration ??
        const InputDecoration(
          hintText: 'Search ...',
          border: OutlineInputBorder(),
        ),
    style: styles.searchInputTextStyle,
    onChanged: options.onWaitingRoomFilterChange,
  );

  final resolvedSearch = options.searchBuilder?.call(
        WaitingRoomModalSearchContext(
          defaultSearch: searchField,
          onFilter: options.onWaitingRoomFilterChange,
        ),
      ) ??
      searchField;

  Widget buildParticipantItem(WaitingRoomParticipant participant) {
    void onAccept() {
      handleResponse(participant, true);
    }

    void onReject() {
      handleResponse(participant, false);
    }

    final row = Row(
      children: [
        Expanded(
          child: Text(
            participant.name,
            style: styles.participantNameTextStyle ??
                const TextStyle(fontSize: 16, color: Colors.black),
          ),
        ),
        const SizedBox(width: 8),
        IconButton(
          onPressed: onAccept,
          style: styles.acceptButtonStyle,
          icon: styles.acceptIcon ??
              const Icon(
                Icons.check,
                color: Colors.green,
              ),
        ),
        const SizedBox(width: 4),
        IconButton(
          onPressed: onReject,
          style: styles.rejectButtonStyle,
          icon: styles.rejectIcon ??
              const Icon(
                Icons.close,
                color: Colors.red,
              ),
        ),
      ],
    );

    final defaultItem = Padding(
      padding: const EdgeInsets.symmetric(vertical: 4),
      child: row,
    );

    return options.itemBuilder?.call(
          WaitingRoomModalItemContext(
            defaultItem: defaultItem,
            participant: participant,
            onAccept: onAccept,
            onReject: onReject,
          ),
        ) ??
        defaultItem;
  }

  final listWidget = waitingList.isEmpty
      ? Center(
          child: options.emptyState ??
              Text(
                'No participants in waiting room',
                style: styles.bodyTextStyle ??
                    const TextStyle(
                      fontSize: 14,
                      color: Colors.black54,
                    ),
                textAlign: TextAlign.center,
              ),
        )
      : ListView.separated(
          padding: styles.listPadding ??
              const EdgeInsets.symmetric(vertical: 8, horizontal: 8),
          itemCount: waitingList.length,
          itemBuilder: (context, index) =>
              buildParticipantItem(waitingList[index]),
          separatorBuilder: (context, index) => const SizedBox(height: 8),
        );

  final resolvedList = options.listBuilder?.call(
        WaitingRoomModalListContext(
          defaultList: listWidget,
          participants: waitingList,
          counter: waitingCounter,
        ),
      ) ??
      listWidget;

  final body = Column(
    crossAxisAlignment: CrossAxisAlignment.stretch,
    children: [
      resolvedSearch,
      const SizedBox(height: 12),
      Expanded(child: resolvedList),
    ],
  );

  final resolvedBody = options.bodyBuilder?.call(
        WaitingRoomModalBodyContext(
          defaultBody: body,
          counter: waitingCounter,
        ),
      ) ??
      body;

  final content = Column(
    crossAxisAlignment: CrossAxisAlignment.stretch,
    children: [
      resolvedHeader,
      Divider(
        color: styles.dividerColor ?? Colors.black,
        height: styles.dividerHeight ?? 16,
        thickness: styles.dividerThickness ?? 1,
        indent: styles.dividerIndent,
        endIndent: styles.dividerEndIndent,
      ),
      const SizedBox(height: 8),
      Expanded(child: resolvedBody),
    ],
  );

  final resolvedContent = options.contentBuilder?.call(
        WaitingRoomModalContentContext(
          defaultContent: content,
          counter: waitingCounter,
        ),
      ) ??
      content;

  final outerDecoration = styles.outerContainerDecoration ??
      BoxDecoration(
        color: options.backgroundColor,
        borderRadius: BorderRadius.circular(10),
      );

  final innerDecoration = styles.contentDecoration ??
      BoxDecoration(
        color: options.backgroundColor,
        borderRadius: BorderRadius.circular(10),
      );

  return Visibility(
    visible: options.isWaitingModalVisible,
    child: Stack(
      children: [
        Positioned(
          top: positionData['top'],
          right: positionData['right'],
          child: Container(
            width: modalWidth,
            height: modalHeight,
            decoration: outerDecoration,
            padding: styles.outerPadding ?? const EdgeInsets.all(10),
            child: DecoratedBox(
              decoration: innerDecoration,
              child: Padding(
                padding:
                    styles.contentPadding ?? const EdgeInsets.all(16),
                child: resolvedContent,
              ),
            ),
          ),
        ),
      ],
    ),
  );
}