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<String>? paragraphs =
      content?.replaceAll('/p><p', '/p>\n<p').split('\n');

  // Set proper sizes for content text and remove breaks.
  // this can be used to also pre-process and remove the justification of text
  for (var i = 0; i < (paragraphs?.length ?? 0); i++) {
    paragraphs?[i] = paragraphs[i].replaceAll('<br>', '');
  }

  // Content without Image Header + Blog Title
  final contentNoHeaderTitle = paragraphs?.join("</p>");
  return HtmlWidget(
    contentNoHeaderTitle ?? '',
    customStylesBuilder: (element) {
      if (element.localName == 'h3') {
        //? header H3
        return {
          'font-weight': '500',
          'font-size': '24px',
          'line-height': '140%',
          'color': '#212124;',
        };
      } else if (element.localName == 'h4') {
        //? header H4
        return {
          'font-weight': '500',
          'font-size': '20px',
          'line-height': '140%',
          'color': '#212124;',
        };
      } else if (element.localName == 'h2') {
        //? header H2
        return {
          'font-weight': '500',
          'font-size': '26px',
          'line-height': '140%',
          'color': '#212124;',
        };
      } else if (element.localName == 'h1') {
        //? header H1
        return {
          'font-weight': '1000',
          'font-size': '28px',
          'line-height': '140%',
          'color': '#212124;',
        };
      } else if (element.localName == 'a') {
        //? link a href
        return {
          'font-weight': '500',
          'color': '#67C1BF',
          'text-decoration': 'none',
        };
      } else if (element.localName == 'strong') {
        //? bold strong
        return {
          'font-weight': '1000',
        };
      } else if (element.localName == 'arabic' ||
          element.localName == 'sub') {
        //? sub was used to denote arabic in CS by the content-creators
        //? arabic
        return {
          'font-family': 'me_quran',
          'font-weight': '500',
          'font-size': '24px',
        };
      } else {
        //? regular text style
        return {
          'font-weight': 'normal',
          'font-size': '16px',
          'line-height': '150%',
          'color': '#4C4C50',
        };
      }
    },
    customWidgetBuilder: (element) {
      //? image element
      if (element.outerHtml.contains('figure') &&
          element.innerHtml.contains('img')) {
        final caption = element.nodes.firstWhereOrNull(
          (node) => node.text?.isNotEmpty ?? false,
        );

        final imageSrc = element.querySelector('img')?.attributes['src'];

        return Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            ClipRRect(
              borderRadius: BorderRadius.circular(4),
              child: CachedNetworkImage(
                imageUrl: imageSrc ?? '',
                fit: BoxFit.cover,
                errorWidget: (_, __, ___) => AspectRatio(
                  aspectRatio: 16 / 9,
                  child: Text(
                    'Image: ${element.attributes['alt'] ?? '...'}',
                  ),
                ),
              ),
            ),
            if (caption != null) ...[
              const SizedBox(height: 4),
              Text(
                caption.text ?? '...',
              ),
            ],
            const SizedBox(
              height: 8,
            ),
          ],
        );
      }
      // Display WebView video for each URL
      if (element.innerHtml.contains('iframe')) {
        final divElements = element.querySelectorAll('.item');
        final List<Map<String, String>> videosDetails = [];

        for (final divElement in divElements) {
          final videoTitle = divElement.text.trim();
          final videoSrc =
              divElement.querySelector('iframe')?.attributes['src'] ?? '';
          videosDetails.add({'videoTitle': videoTitle, 'videoSrc': videoSrc});
        }

        return Padding(
          padding: const EdgeInsets.symmetric(horizontal: 16),
          child: Column(
            children: [
              for (final Map<String, String> videoDetails
                  in videosDetails) ...[
                Align(
                  alignment: Alignment.topLeft,
                  child: Text(
                    videoDetails['videoTitle'] ?? '',
                    style: Theme.of(context).textTheme.bodyLarge,
                    textAlign: TextAlign.start,
                  ),
                ),
                SizedBox(
                  height: 390,
                  child: PhoenixWebview(
                    title: videoDetails['videoTitle'] ?? '',
                    url: videoDetails['videoSrc'] ?? '',
                    showAppBar: false,
                  ),
                ),
                const SizedBox(height: 16),
              ],
            ],
          ),
        );
      } else {
        return null;
      }
    },
    onErrorBuilder: (context, element, error) {
      if (kDebugMode) {
        log(error);
        return Text('$element error: $error');
      }
      return null;
    },
    onTapUrl: (url) async {
      return launchUrlString(url);
    },
  );
}