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 List<int> pages =
      List<int>.generate(options.totalPages + 1, (index) => index);

  final BoxDecoration activeDecoration = BoxDecoration(
    color: options.activePageColor,
    boxShadow: [
      BoxShadow(
        color: Colors.grey.withAlpha((0.5 * 255).toInt()),
        spreadRadius: 1,
        blurRadius: 2,
        offset: const Offset(0, 1),
      ),
    ],
  );

  final BoxDecoration inactiveDecoration = BoxDecoration(
    color: options.inactivePageColor,
    border: Border.all(
      color: Colors.black,
      width: 1.0,
    ),
    boxShadow: [
      BoxShadow(
        color: Colors.grey.withAlpha((0.5 * 255).toInt()),
        spreadRadius: 1,
        blurRadius: 2,
        offset: const Offset(0, 1),
      ),
    ],
  );

  Widget buildPageButton(int page, BuildContext itemContext) {
    final bool isActive = page == options.currentUserPage;
    final bool isHomePage = page == 0;
    bool isLocked = false;

    String displayLabel = page.toString();
    final int mainRoomsLength = options.parameters.mainRoomsLength;
    final int targetPage = options.parameters.memberRoom;

    if (options.parameters.breakOutRoomStarted &&
        !options.parameters.breakOutRoomEnded &&
        page >= mainRoomsLength) {
      final int roomNumber = page - (mainRoomsLength - 1);

      if (targetPage + 1 != roomNumber) {
        final bool isHost = options.parameters.islevel == '2';
        if (!isHost) {
          displayLabel = 'Room $roomNumber 🔒';
          isLocked = true;
        } else {
          displayLabel = 'Room $roomNumber';
        }
      } else {
        displayLabel = 'Room $roomNumber';
      }
    } else {
      displayLabel = page.toString();
    }

    Widget baseContent;
    if (isHomePage) {
      baseContent = Icon(
        Icons.star,
        size: 18,
        color: isActive ? Colors.yellow : Colors.grey,
      );
    } else {
      baseContent = Padding(
        padding: const EdgeInsets.symmetric(horizontal: 2.0),
        child: Text(
          displayLabel,
          style: const TextStyle(
            fontSize: 16,
            fontWeight: FontWeight.w600,
          ),
        ),
      );
    }

    final Widget resolvedContent = options.pageContentBuilder != null
        ? options.pageContentBuilder!(
            PaginationPageContentContext(
              context: itemContext,
              options: options,
              page: page,
              isActive: isActive,
              isHomePage: isHomePage,
              isLocked: isLocked,
              label: displayLabel,
              defaultContent: baseContent,
            ),
          )
        : baseContent;

    Future<void> onSelect() => handleClick(page, mainRoomsLength);

    final Widget defaultButton = GestureDetector(
      onTap: () async {
        if (!isActive) {
          await onSelect();
        }
      },
      child: Container(
        margin: const EdgeInsets.symmetric(horizontal: 5, vertical: 5),
        constraints: options.buttonsContainerStyle,
        decoration: isActive ? activeDecoration : inactiveDecoration,
        child: Center(child: resolvedContent),
      ),
    );

    if (options.pageButtonBuilder != null) {
      return options.pageButtonBuilder!(
        PaginationPageButtonContext(
          context: itemContext,
          options: options,
          page: page,
          isActive: isActive,
          isHomePage: isHomePage,
          isLocked: isLocked,
          content: resolvedContent,
          defaultButton: defaultButton,
          onSelect: onSelect,
        ),
      );
    }

    return defaultButton;
  }

  final Widget listView = ListView.builder(
    shrinkWrap: true,
    scrollDirection:
        options.direction == 'vertical' ? Axis.vertical : Axis.horizontal,
    itemCount: pages.length,
    itemBuilder: (itemContext, index) {
      final int page = pages[index];
      return buildPageButton(page, itemContext);
    },
  );

  final Widget defaultContainer = Container(
    color: options.backgroundColor,
    constraints: BoxConstraints(
      maxHeight: options.direction == 'vertical'
          ? double.infinity
          : options.paginationHeight,
      maxWidth: options.direction == 'horizontal'
          ? double.infinity
          : options.paginationHeight,
    ),
    child: Center(child: listView),
  );

  final Widget resolvedContainer = options.containerBuilder != null
      ? options.containerBuilder!(
          PaginationContainerContext(
            context: context,
            options: options,
            pages: pages,
            listView: listView,
            defaultContainer: defaultContainer,
          ),
        )
      : defaultContainer;

  return Visibility(
    visible: options.showAspect,
    child: resolvedContainer,
  );
}