build method

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

Implementation

@override
Widget build(BuildContext context) {
  // Check if widget is visible before wasting resources on building it
  if (!widget.model.visible) return const Offstage();

  // build the prototype
  if (widget.model.size == null || widget.model.items.isEmpty) {
    Widget prototypeGrid = Container();
    try {
      // build model
      var model = GridItemModel.fromXml(widget.model, widget.model.prototype);
      if (model != null) {
        prototypeGrid = Offstage(
            child: MeasureView(UnconstrainedBox(child: GridItemView(model)),
                onMeasuredItem));
      }
    } catch (e) {
      prototypeGrid = const Text('Error Prototyping GridModel');
    }
    return prototypeGrid;
  }

  gridWidth = widget.model.width ?? widget.model.myMaxWidthOrDefault;
  gridHeight = widget.model.height ?? widget.model.myMaxHeightOrDefault;

  if (widget.model.items.isNotEmpty) {
    prototypeWidth = widget.model.items.entries.first.value.width ??
        widget.model.myMaxWidthOrDefault /
            (sqrt(widget.model.items.length) + 1);
    prototypeHeight = widget.model.items.entries.first.value.height ??
        widget.model.myMaxHeightOrDefault /
            (sqrt(widget.model.items.length) + 1);
  } else {
    prototypeWidth = widget.model.myMaxWidthOrDefault /
        (sqrt(widget.model.items.length) + 1);
    prototypeHeight = widget.model.myMaxHeightOrDefault /
        (sqrt(widget.model.items.length) + 1);
  }

  widget.model.direction == 'horizontal'
      ? direction = Axis.horizontal
      : direction = Axis.vertical;

  // Protect against infinity calculations when screen is smaller than the grid item in the none expanding direction
  if (direction == Axis.vertical && gridWidth < prototypeWidth) {
    gridWidth = prototypeWidth;
  } else if (direction == Axis.horizontal && gridHeight < prototypeHeight) {
    gridHeight = prototypeHeight;
  }

  if (direction == Axis.vertical) {
    double cellWidth = prototypeWidth;
    if (cellWidth == 0) cellWidth = 160;
    count = (gridWidth / cellWidth).floor();
  } else {
    double cellHeight = prototypeHeight;
    if (cellHeight == 0) cellHeight = 160;
    count = (gridHeight / cellHeight).floor();
  }

  /// Busy / Loading Indicator
  busy ??= BusyModel(widget.model,
          visible: widget.model.busy, observable: widget.model.busyObservable)
      .getView();

  // Build the Grid Rows
  Widget view = ListView.builder(
      scrollDirection: direction,
      physics: widget.model.onpulldown != null
          ? const AlwaysScrollableScrollPhysics()
          : null,
      controller: controller,
      itemBuilder: itemBuilder);

  if (widget.model.onpulldown != null) {
    view = RefreshIndicator(
        onRefresh: () => widget.model.onPull(context), child: view);
  }

  if (widget.model.onpulldown != null || widget.model.allowDrag) {
    view = ScrollConfiguration(
      behavior: ProperScrollBehavior().copyWith(
        dragDevices: {
          PointerDeviceKind.touch,
          PointerDeviceKind.mouse,
        },
      ),
      child: view,
    );
  } else {
    view = ScrollConfiguration(behavior: ProperScrollBehavior(), child: view);
  }

  // add margins
  view = addMargins(view);

  // apply user defined constraints
  view = applyConstraints(view, widget.model.tightestOrDefault);

  // apply visual transforms
  view = applyTransforms(view);

  List<Widget> children = [];

  children.add(view);

  // Initialize scroll shadows to controller after building
  if (widget.model.scrollShadows == true) {
    scrollShadow = ScrollShadowModel(widget.model);
    children.add(ScrollShadowView(scrollShadow));
  }

  children.add(Center(child: busy));

  view = Stack(children: children);

  return view;
}